Advertisement
Guest User

Help with my linked list remove function

a guest
Jul 20th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. /*
  2. // Lesson 13, pointers and dynamic memory
  3. //
  4. //
  5. // lists of nodes should look like this
  6. //
  7. // [data]->[data]->[data]->[data]->[data]->NULL
  8. //
  9. // in this exercise you will implement functions to add nodes to the end of the list
  10. // and remove nodes from the end of the list.
  11. //
  12. // the data in this exercise are simple ints. when creating new nodes use the getnum() function to fill the data.
  13. //
  14. // when everything works you should be able to add as many elements as you want, and remove elements back to zero.
  15. //
  16. // Fun Fact: this is a more simple version of an excercise you will be required to do one day in any programming course and many interviews.
  17. //
  18. */
  19.  
  20. #include <iostream>
  21.  
  22. /////////////////////////////////
  23. //define objects here
  24.  
  25. struct node
  26. {
  27.     int data;
  28.     node* next;
  29. };
  30.  
  31. //////////////////////////////////
  32. //place function declarations here
  33.  
  34. void insert(node** root);
  35. void remove(node** root);
  36. void display(node* root);
  37. int getsize(node* root);
  38. int getnum();
  39.  
  40. //////////////////////////////////
  41. //main program (do not modify)
  42. int main(int argc, char** argv)
  43. {
  44.     bool done = false;
  45.     node* list = NULL;
  46.  
  47.     while( !done )
  48.     {
  49.         int choice;
  50.         std::cout<<"\n\n========choices========\n";
  51.         std::cout<<"1. add a random number to the end of the list\n";
  52.         std::cout<<"2. remove a number from the end of the list\n";
  53.         std::cout<<"3. display the list\n";
  54.         std::cout<<"4. display the size of the list\n";
  55.         std::cout<<"0. quit\n";
  56.         std::cout<<"please choose an option: ";
  57.         std::cin>>choice;
  58.         std::cout<<"\n--------------------------\n";
  59.        
  60.         switch(choice)
  61.         {
  62.             case 0: done = true; break;
  63.             case 1:
  64.                 insert(&list);
  65.             break;
  66.             case 2:
  67.                 remove(&list);
  68.             break;
  69.             case 3:
  70.                 display(list);
  71.             break;
  72.             case 4:
  73.                 std::cout<<"the list size is "<<getsize(list)<<std::endl;
  74.             break;
  75.             default: break;
  76.         }
  77.     }
  78.  
  79.     while( list )
  80.     {
  81.         node* next = list->next;
  82.         delete list;
  83.         list = next;
  84.     }
  85.  
  86.     return 0;
  87. }
  88.  
  89.  
  90.  
  91. //////////////////////////////////
  92. //place function definitions here
  93.  
  94. void insert(node** root)
  95. {
  96.     //TODO: your code here
  97.     node* temp = *root;
  98.     if(*root)
  99.     {
  100.         while((*root)->next)
  101.         {
  102.             (*root) = (*root)->next;
  103.         }
  104.         (*root)->next = new node;
  105.         (*root)->next->data = getnum();
  106.         (*root)->next->next = 0;
  107.         *root = temp;
  108.     }
  109.     else
  110.     {
  111.         (*root) = new node;
  112.         (*root)->data = getnum();
  113.         (*root)->next = 0;
  114.     }
  115.  
  116. }
  117.  
  118. void remove(node** root)
  119. {
  120.     //TODO: your code here
  121.     node* temp = *root;
  122.     node* previous = 0;
  123.     if(*root)
  124.     {
  125.         while((*root)->next)
  126.         {
  127.             previous = *root;
  128.             *root = (*root)->next;
  129.         }
  130.         delete *root;
  131.         *root = temp;
  132.         if(previous)
  133.         {
  134.             previous->next = 0;
  135.         }
  136.     }
  137.     else
  138.     {
  139.         std::cout<<"cannot delete items from empty list\n";
  140.     }
  141. }
  142.  
  143. void display(node* root)
  144. {
  145.     //TODO: your code here
  146.     if(root)
  147.     {
  148.         while(root)
  149.         {
  150.             std::cout<<root->data<<"\n";
  151.             root = root->next;
  152.         }
  153.     }
  154.     else
  155.     {
  156.         std::cout<<"list is currently empty\n";
  157.     }
  158. }
  159.  
  160. int getsize(node* root)
  161. {
  162.     int size = 0;
  163.  
  164.     //TODO: your code here
  165.     if(root)
  166.     {
  167.         while(root)
  168.         {
  169.             ++size;
  170.             root = root->next;
  171.         }
  172.     }
  173.     else
  174.     {
  175.         return 0;
  176.     }
  177.    
  178.     return size;
  179. }
  180.  
  181. #include <cstdlib>
  182. int getnum()
  183. { return rand()%100; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement