0

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+
Ken Casimiro
  • 85
  • 1
  • 8
  • 1
    I recommend you [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). It's a crucial and mandatory skill for anyone who want to do any kind of programming. – Some programmer dude Nov 08 '17 at 03:14
  • Not gotta go through all that code to find whats wrong with it. I suggest commenting out parts of your code to see where you are getting the problem or using the debugger and stepping through. Or submit the simplified piece of code so I can look at it :) – D.Haritheesan Nov 08 '17 at 03:31
  • thanks for replying, will work on this. pretty much there is something wrong with my mergeSort function. – Ken Casimiro Nov 08 '17 at 03:33
  • Unrelated: `while(fin.good())` is a close relative of [`while(!fin.eof())`](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). It is a bit betterbut it still allows for error because it tests if the stream is good before reading and possibly finding out that the stream was good, but after the read it's bad and you didn't get the data you asked for. It's always best to test after reading to make sure. – user4581301 Nov 08 '17 at 03:55

1 Answers1

2

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.

1201ProgramAlarm
  • 30,320
  • 7
  • 40
  • 49