Linked List Help
Dec 6, 2013 at 1:51pm UTC
I am attempting to implement code in which I use a linked list to order a set of data ( name, surname, id, mark). While it will work for a small number of inputs from a file, it throws an exception after a while for a larger number of data bits (my input file has 1000+ sets of data)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
//main.cpp
#include <iostream>
#include <fstream>
#include "linkedlist.h"
#include <windows.h>
using namespace std;
#define FN "student1000.txt" // hardwired file name
void main(){
cout << "Using the Linked List" << endl << endl;
list studentList;
//
// read in data from student.txt
//
ifstream in;
in.open(FN);
if (in.fail()) {
cout << "unable to open " << FN << endl;
getchar(); // type key to dismiss window
return ;
}
while (!in.eof()) {
node *student = new node();
in >> student->surname >> student->name >> student->id >> student->mark;
studentList.add(student);
//studentList.print();
cout << endl;
}
in.close();
//cout << studentList.count << endl;
//
// generate result file
//studentList.write("studentOutput.txt");
studentList.print();
cout << endl;
studentList.search("Abraham" , "CRONIN" );
cout << endl;
studentList.search("Collin" , "BREE" );
cout << endl;
studentList.search("Etienne" , "HOLOHAN" );
cout << endl;
studentList.search("" , "AAAA" );
cout << endl;
studentList.search("" , "ZZZZ" );
cout << endl;
getchar();
return ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
//implementation of the linked list
#include "linkedlist.h"
#include <iostream>
list::list()
{
first = NULL;
current = NULL;
previous = NULL;
}
list :: ~list()
{
delete first;
}
void list::add(node* newest)
{
if (first)
{
if (first->comp(newest) < 0)
{
newest->next = first;
first = newest;
}
else if (first->comp(newest) > 0)
{
previous = first;
add(first->next, newest);
}
}
else
{
first = newest;
}
}
void list::add(node* _current, node* newest)
{
if (_current)
{
if (_current->comp(newest) < 0)
{
previous->next = newest;
newest->next = _current;
}
else if (_current->comp(newest) > 0)
{
previous = _current;
add(_current->next, newest);
}
}
else
{
previous->next = newest;
}
}
node* list::search(string name, string surname)
{
node* temporary = new node;
temporary->name = name;
temporary->surname = surname;
if (first)
{
first->print();
if ((temporary->name == first->name) && (temporary->surname == first->surname))
{
cout << "Data found" << endl;
return first;
}
else if (first->comp(temporary) > 0)
{
cout << "data does not exist" << endl;
return NULL;
}
else if (first->comp(temporary) < 0)
{
return search(first->next, temporary);
}
}
else
{
cout << "No data inside the List" << endl;
return NULL;
}
}
node* list::search(node* _current, node* temporary)
{
if (current)
{
if ((temporary->name == _current->name) && (temporary->surname == _current->surname))
{
cout << "Match found" << endl;
return _current;
}
else if (_current->comp(temporary) > 0)
{
cout << "data does not exist" << endl;
return NULL;
}
else if (_current->comp(temporary) < 0)
{
return search(_current->next, temporary);
}
}
else
{
cout << "No match found" << endl;
return NULL;
}
}
void list::print()
{
node* temporary = first;
while (temporary)
{
temporary->print();
temporary = temporary->next;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
//implementation of the node class-stores the data
#include "node.h"
#include <iostream>
using namespace std;
node::node()
{
name = surname = "" ;
id = mark = 0;
next = NULL;
}
node :: ~node()
{
if (next)
next->~node();
delete this ;
}
int node::comp(node* newest)
{
if (int r = newest->surname.compare(surname))
return r;
else
return (r = newest->name.compare(name));
}
void node::print()
{
cout << name << " " << surname << " " << id << " " << mark << endl;
}
The Error:
Unhandled exception at 0x0F6E08C9 (msvcr120d.dll) in LinkedListExample.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00412FE4).
Dec 6, 2013 at 5:34pm UTC
Bump
Dec 7, 2013 at 1:03pm UTC
Still need help with this guys
Topic archived. No new replies allowed.