簡體   English   中英

合並在雙鏈表C ++上的排序

[英]Merge Sort on a Doubly Linked List C++

我在嘗試合並我的雙重鏈接列表時遇到麻煩。 當我嘗試在合並排序后顯示所有節點時,遇到了分段錯誤。

我的頭文件帶有預處理器指令,structs,函數原型。

hw07.h

#ifndef HW07_H_
#define HW07_H_

#include <iostream>
#include <fstream>
using namespace std;

typedef struct slist slist;

struct stud
{
  string term;
  string title;
  string description;
  string tktNum;
  string location;
  string lecDay;
  string instructor;
  string labLoc;
  string labDay;
  string labInstruct;
  string units;
  string preReqs;
  string grade;
};
struct sentry
{
  slist *list;
  sentry *next;
  sentry *prev;
  stud *data;
};
struct slist
{
  int length;
  sentry *first;
  sentry *last;
};

void readFile(slist *&header);

void displayAll(list *header);

sentry *mergeSort(sentry *header);

sentry *merge(sentry *first, sentry *second);

sentry *split(sentry *header);  

#endif

我的主要

hw07.cpp

#include "hw07.h"

int main()
{
   slist *header = NULL;

   readFile(header);
   displayAll(header);
   mergeSort(header->first);
   displayAll(header);

   return 0;
}

我的readFile函數

readFile.cpp

#include "hw07.h"

void readFile(slist *&header)
{
  ifstream fin;
  sentry *node, *temp;
  node = NULL;
  temp = NULL;

  // opens the text file/database
  fin.open("sbclassdb.txt");

  while(fin.good())
  {
    if(header == NULL)
  {
  header = new slist;
  header->length = 0;
  header->first  = NULL;
  header->last   = NULL;

  node = new sentry;
  header->first = node;
  header->last  = node;
  node->prev    = NULL;
  node->next    = NULL;
  }
  else
  {
    node = new sentry;
    node->prev = header->last;
    node->next = NULL;
    temp = header->last;
    temp->next = node;
    header->last = node;
  }
  node->data = new stud;
  getline(fin, node->data->term);
  getline(fin, node->data->title);
  getline(fin, node->data->description);
  getline(fin, node->data->tktNum);
  getline(fin, node->data->location);
  getline(fin, node->data->lecDay);
  getline(fin, node->data->instructor);
  getline(fin, node->data->labLoc);
  getline(fin, node->data->labDay);
  getline(fin, node->data->labInstruct);
  getline(fin, node->data->units);
  getline(fin, node->data->preReqs);
  getline(fin, node->data->grade);
  ++header->length;

  // when there's a blank line
  string blankLine;
  if(!getline(fin, blankLine))
    break;
  }
  fin.close();
}

我的displayAll函數(顯示所有節點)

displayAll.cpp

#include "hw07.h"

void displayAll(slist *header)
{
   sentry *temp, *node;

   node = NULL;
   temp = NULL;
   node = header->first;
   cout << endl;

  for(int i=0; i<header->length; ++i)
  {
    cout << "Term: "                << node->data->term        << endl;
    cout << "Title: "               << node->data->title       << endl;
    cout << "Description: "         << node->data->description << endl;
    cout << "Ticket #: "            << node->data->tktNum      << endl;
    cout << "Lecture Location: "    << node->data->location    << endl;
    cout << "Lecture Time: "        << node->data->lecDay      << endl;
    cout << "Instructor: "          << node->data->instructor  << endl;
    cout << "Lab Location: "        << node->data->labLoc      << endl;
    cout << "Lab Time: "            << node->data->labDay      << endl;
    cout << "Lab Instructor: "      << node->data->labInstruct << endl;
    cout << "Units: "               << node->data->units       << endl;
    cout << "Pre-Requisites: "      << node->data->preReqs     << endl;
    cout << "Grade: "               << node->data->grade       << endl;

    temp = node;
    node = temp->next;
    cout << "\n";
  }
}

我的mergeSort函數

mergeSort.cpp

#include "hw07.h"

sentry *mergeSort(sentry *header)
{
  if (!header || !header->next)
  {
    return header;
  }
  sentry *second = split(header);

  header = mergeSort(header);
  second = mergeSort(second);

  return merge(header, second);
}

sentry *split(sentry *head)
{ 
  sentry *fase = head, *slow = head;
  while(fast->next && fast->next->next)
  {
    fast = fast->next->next;
    slow = slow->next;
  }
  sentry *temp = slow->next;
  slow->next = NULL;
  return temp;
}

sentry *merge(sentry *first, sentry *second)
{
  if (!first)
     return second;
  if (!second)
     return first;

  if(first->data->title < second->data->title)
  {
    first->next = merge(first->next, second);
    first->next->prev = first;
    first->prev = NULL;
    return first;
  }
  else
  {
    second->next = merge(first, second->next);
    second->next->prev = second;
    second->prev = NULL;
    return second;
   }
}

我的sbclassdb.txt

Fall 2017
CS1B
Intro to Computer Science 2
11111
SM101
TTH 100PM
John Doe
SM102
TTH 300PM
John Doe
5
NA
A

Spring 2018
CS1A
Intro to Computer Science 1
22222
SM101
TTH 200PM
Jane Doe
SM102
TTH 300PM
Jane Doe
5
CS1A
NA

Spring 2018
ANTHRO 1
Introduction to Anthropology
12345
BGS101
MWF 200PM
Bob Smith
BGS102
Bob Smith
4
CS1B
NA
NA

Spring 2015
MATH 2
Pre-Calculus
111213
SM405
MW 200PM
Rick Sanchez
NA
NA
NA
4
Math 124
A+

您的mergeSort函數返回一個值,您可以在main忽略該值。 這將導致列表的頭部指向錯誤的節點(位於中間的某個位置,而不是新的前端節點)。

顯示列表時,循環是基於列表中的條目數,而不是循環直到找到NULL指針。 通常可以,但是由於第一個問題,當您嘗試越過最后一個節點時,程序會崩潰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM