From: Jordan B. <jor...@me...> - 2011-05-02 02:30:26
|
I have the following code and have ran it through the debugger several times and still can not nail why it is acting so funny. I am doing this for a work project and am getting pretty desperate at this point with the meeting coming up and I still can not get down these simple linked lists program. As you can see the program should add the following numbers to the list and change as specified by the calls to the functions in respect to their arguments, The program will compile but has an infinite loop at the del function I belove as the memory fails once it gets to that location. Also when displaying the list the first time it does not show the digit 1 as it should. Upon displaying the list a second time it then shows it. Any help would be much appreciated. #include <iostream> using namespace std; class linklist { private: struct node { int data; node *link; }*p; public: linklist(); void append( int num ); void first( int num ); void next( int c, int num ); void del( int num ); void display(); int count(); ~linklist(); }; linklist::linklist() { p=NULL; } void linklist::append(int num) { node *newNode = new node; node *nextNode = new node; if (p == NULL) { p = newNode; p ->data = num; p->link = NULL; } else newNode = new node; node * n = p; while(n -> link){n = n -> link;} n-> link = newNode; newNode -> data = num; newNode -> link = NULL; } void linklist::first(int num) { node * newNode; newNode = new node; newNode -> data = num; newNode -> link = p; p = newNode; } void linklist::next( int c, int num) { node *lastNode; node *currNode; node *nextNode; currNode = p; lastNode = currNode; nextNode = currNode -> link; while(c != 0) { lastNode = currNode; currNode = currNode -> link; nextNode = currNode -> link; c--; } node *newNode; newNode = new node; newNode -> data = num; currNode -> link = newNode; newNode -> link = nextNode; } void linklist::del( int num ) { node * lastNode; node * currNode; node * nextNode; currNode = p; lastNode = currNode; nextNode = currNode -> link; while(currNode != NULL) { if(currNode -> data == num) { lastNode -> link = nextNode; break; } else lastNode = currNode; currNode = currNode -> link; nextNode = currNode -> link; } cout << "Itiems not found" << endl; } void linklist::display() { node * a_node; a_node = p; while(a_node != NULL) { cout << a_node -> data << endl; a_node = a_node -> link; } } int linklist:: count () { int count =1; node *a_node; if(p == NULL) { return count; } else { a_node = p; while(a_node -> link != NULL) { count++; a_node = a_node -> link; } } return count;} linklist::~linklist() { node *q; if( p == NULL ) return; while( p != NULL ) { q = p->link; delete p; p = q; } } void main() { cout << "Name: COMP 1302 Homework Four" << endl; cout << "Christine Kikuchi" << endl; cout << "Purpose: A simple list that will create a list," << endl; cout << " count the number of its elements, " << endl; cout << " and tell if an item is on the list." << endl; cout << endl; linklist ll; cout<<"No. of elements = "<<ll.count(); ll.append(12); ll.append(13); ll.append(23); ll.append(43); ll.append(44); ll.append(50); ll.first(2); ll.first(1); ll.next(3,333); ll.next(6,666); ll.display(); cout<<"\nNo. of elements = "<<ll.count(); cout << endl; ll.del(333); ll.del(12); ll.del(98); cout<<"\nNo. of elements = "<<ll.count(); cout << endl; ll.display(); system("pause"); } |