简体   繁体   中英

Merge Sort on a Doubly Linked List C++

I'm having trouble trying to merge sort my doubly link list. When I tried to display all of my nodes after merge sorting, it hits a segmentation fault.

my header file with the pre-processor directives ,structs , function prototypes.

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

my main

hw07.cpp

#include "hw07.h"

int main()
{
   slist *header = NULL;

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

   return 0;
}

my readFile function

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();
}

my displayAll function (displays all nodes)

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";
  }
}

my mergeSort function

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;
   }
}

my 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+

Your mergeSort function returns a value, which you ignore in main . This results in the head of your list pointing at the wrong node (somewhere in the middle, instead of at the new front node).

When you display the list, you base your loop on the number of entries in your list, rather than looping until a NULL pointer is found. This is normally OK, but because of the first problem your program crashes when you try to move on past the last node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM