合并排序在双向链接列表C ++上

时间:2017-11-08 03:10:35

标签: c++ pointers mergesort doubly-linked-list

我在尝试合并排序我的双重链接列表时遇到了麻烦。当我尝试在合并排序后显示所有节点时,它会遇到分段错误。

我的头文件包含预处理器指令,结构,函数原型。

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+

1 个答案:

答案 0 :(得分:2)

您的mergeSort函数返回一个值,您在main中忽略该值。这会导致列表的头部指向错误的节点(位于中间的某个位置,而不是新的前节点)。

当您显示列表时,您将循环基于列表中的条目数,而不是循环直到找到NULL指针。这通常没问题,但是由于第一个问题,当您尝试移动到最后一个节点时程序崩溃。