From: Jordan B. <jor...@me...> - 2011-05-01 16:21:43
|
Below is the program that I have and am trying to fill in the blanks to help me better understand pointers. I have added the code to the append function and the first function I believe this is right but want to make sure I am heading in the right direction. The function "first" should add the variable being passed do it to the first of the list and the function append should add the element at the end of the list. #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; if(p =NULL) { p = new node; p -> data = num; p -> link = newNode; } else newNode -> data = num; newNode -> link = new node; newNode = newNode-> link; } void linklist::first(int num) { node *newNode; newNode = new node; newNode -> data = p -> data; newNode -> link = p -> link; p=NULL; p = new node; p -> data = num; p -> link = newNode; *newNode = NULL; } void linklist::next( int c, int num) { //Add implementation code here } void linklist::del( int num ) { //Add implementation code here } void linklist::display() { //Add implementation code here } linklist::~linklist() { node *q; if( p == NULL ) return; while( p != NULL ) { q = p->link; delete p; p = q; } } void main() { linklist ll; cout<<"No. of elements = "<<ll.count(); ll.append(12); llappend(13); ll.append(23); ll.append(43); ll.append(44); ll.append(50); ll.first(2); llfirst(1); ll.next(3,333); ll.next(6,666); ll.display(); cout<<"\nNo. of elements = "<<ll.count(); ll.del(333); ll.del(12); ll.del(98); cout<<"\nNo. of elements = "<<ll.count(); } |