The code below is where I have tried implementing a graph function. I have put cout statements in various places to pinpoint the error location of the segmentation fault. It goes through main, and into the graphType class. In the createGraph function it gets lost around the while loop (perhaps insertLast). Does anyone have an idea of where the error is occuring? Thank you in advance.
MAIN:
Code:
#include <fstream> //ifstream, ofstream functions
#include <iostream> //cin, cout functions
#include "graphType.h" //graphType object file
using namespace std;
//Function prototypes
void OpenInputFile (ifstream&, char[]);
int main()
{
ifstream dataFileIn1;
char inputFile1[30] = "graphSet1.dat";
graphType graph;
OpenInputFile(dataFileIn, inputFile1);
graph.createGraph(dataFileIn);
dataFileIn.close();
return 0;
}
//function definitions................................................
//Purpose: Open an input file and test for successful open
//Pre-condition: File has been declared as input file stream, and
// file name is known
//Post-condition: File is open if successful, otherwise terminate the program
void OpenInputFile (ifstream& dataFileIn, char inputFile[])
{
//Open input file
dataFileIn.open(inputFile);
//If not successful open, display message and exit with error
if (!dataFileIn)
{
cout << "\n\n\n\t\t\t Error: Input File Not Opened"
<< inputFile << endl;
exit(1);
}
} //End OpenInputFile
DATA FILE:
Code:
3
0 1 2 -999
1 2 -999
2 -999
GRAPH CLASS:
Code:
#ifndef H_graph
#define H_graph
#include <iostream>
#include <fstream>
#include <iomanip>
#include "linkedList.h" //includes nodeType struct
#include "linkedQueue.h" //remove nodeType struct
#include "unorderedLinkedList.h"
using namespace std;
class graphType
{
public:
void createGraph(ifstream&);
//Overloaded function to create a graph.
//Postcondition: The graph is created using the
// adjacency list representation.
graphType(int size = 0);
//Constructor
//Postcondition: gSize = 0; maxSize = size;
// graph is an array of pointers to linked lists.
~graphType();
//Destructor
//The storage occupied by the vertices is deallocated.
protected:
int maxSize; //maximum number of vertices
int gSize; //current number of vertices
unorderedLinkedList<int> *graph; //array to create
//adjacency lists
};
void graphType::createGraph(ifstream& graphFile)
{
int vertex;
int adjacentVertex;
if (gSize != 0) //if the graph is not empty, make it empty
clearGraph();
graphFile >> gSize; //get the number of vertices
for (int index = 0; index < gSize; index++)
{
graphFile >> vertex;
graphFile >> adjacentVertex;
while (adjacentVertex != -999)
{
graph[vertex].insertLast(adjacentVertex);
graphFile >> adjacentVertex;
} //end while
} // end for
} //end createGraph
//Constructor
graphType::graphType(int size)
{
maxSize = size;
gSize = 0;
graph = new unorderedLinkedList<int>[size];
}
//Destructor
graphType::~graphType()
{
clearGraph();
}
#endif
LINKED LIST CLASS:
Code:
#ifndef H_LinkedListType
#define H_LinkedListType
#include <iostream>
#include <cassert>
using namespace std;
//Definition of the node
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
UNORDERED LINKED LIST:
Code:
#ifndef H_UnorderedLinkedList
#define H_UnorderedLinkedList
#include "linkedList.h"
using namespace std;
template <class Type>
class unorderedLinkedList: public linkedListType<Type>
{
protected:
//Modification to recognize the inherited protected variables
// from the base class when a template is used.
using linkedListType<Type>::count;
using linkedListType<Type>::first;
using linkedListType<Type>::last;
public:
void insertLast(const Type& newItem);
//Function to insert newItem at the end of the list.
//Postcondition: first points to the new list, newItem is
// inserted at the end of the list, last points to the
// last node, and count is incremented by 1.
};
template <class Type>
void unorderedLinkedList<Type>::insertLast(const Type& newItem)
{
nodeType<Type> *newNode; //pointer to create the new node
newNode = new nodeType<Type>; //create the new node
newNode->info = newItem; //store the new item in the node
newNode->link = NULL; //set the link field of newNode
//to NULL
if (first == NULL) //if the list is empty, newNode is
//both the first and last node
{
first = newNode;
last = newNode;
count++; //increment count
}
else //the list is not empty, insert newNode after last
{
last->link = newNode; //insert newNode after last
last = newNode; //make last point to the actual
//last node in the list
count++; //increment count
}
}//end insertLast
#endif