#include using namespace std; // The node class class node{ public: int data; node *next; void print(); int find(int); // returns 0 if not found else 1 void deletell(int); int get(int, int); // returns the nth value }; int node::get(int d, int count){ count = count + 1; // count each node if(count == d){ // when the correct one is reached return data; // return it } if(next == NULL) // if the end of the list is reached return -1; // the list is too short, return -1 return(next -> get(d,count)); // otherwise count the next node } void node::deletell(int d){ if(next == NULL) return; if(next->data == d){ // if the next node is the one to be removed node *temp; temp = next; // point to the node to be removed next = next->next; // skip over the node to be removed delete temp; // delete the node to be removed } else { next->deletell(d); // otherwise keep searching down the list } } void node::print(){ cout << "On the way in: "<< data << endl; if(next != NULL){ // if the end of the list hasn't been reached next -> print(); // ask the next node to print itself } } int node::find(int d){ if(data == d){ return 1; // found it! } if(next == NULL){ return 0; // reached the end of the list without finding it } int answer; answer = next ->find(d); // ask my neighbor to look return(answer); // return what my neightbor found } // These are the functions that are part of main, // not part of the node class. However, they act // as ''wrapper'' functions to the node functions. // Their job is to check for special cases (like // empty lists) before calling the node functions. node * deletell(node *,int); // prototype void print(node *); // prototype for print void print(node *h){ // print code if(h == NULL){ // check for the empty list cout << "The list is empty\n"; return; } h -> print(); // if not empty call the node print function } int get(node *,int); // prototype for get int get(node *h, int d){ // get code if(h == NULL) // check for empty list return -1; return(h -> get(d,0)); // if not empty call the node get } int main(){ node *head; // points to a node object head = NULL; node *temp; int len; cout << "How many numbers? "; cin >> len; for(int i = len; i > 0; i--){ temp = new node; temp -> data = i*2; temp -> next = NULL; temp -> next = head; head = temp; } print(head); int d, data; cout << "Which node number? "; cin >> d; data = get(head,d); cout << "Node " << d << " stores " << data; cout << endl; } node * deletell(node *h, int x){ if(h -> data == x){ node *temp; temp = h; h = h -> next; delete temp; } else { h -> deletell(x); } return h; }