SHARE
TWEET

LL Help (C++) Current Program

a guest Dec 14th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. struct LLnode{
  6.     string theData;
  7.     LLnode * fwdPtr;
  8. };
  9.  
  10. void push_front(LLnode * &llh, string newData{
  11.     if(!llh){
  12.         LLnode * newNode = new LLnode;
  13.         newNode -> theData = newData;
  14.         newNode -> fwdPtr = nullptr;
  15.         llh = newNode;
  16.     }
  17.     else{
  18.         LLnode * trav = new LLnode;
  19.         trav = llh;
  20.         LLnode * newNode = new LLnode;
  21.         newNode -> theData = newData;
  22.         newNode -> fwdPtr = trav;
  23.         llh = newNode;
  24.  
  25.     }
  26. }
  27.  
  28. void push_back(LLnode * &llh, string newData){
  29.     LLnode * trav = new LLnode;
  30.     if(!llh){
  31.         llh = new LLnode;
  32.         llh -> theData = newData;
  33.         llh -> fwdPtr = nullptr;
  34.     }
  35.     else{
  36.         LLnode * newNode = new LLnode;
  37.         newNode -> theData = newData;
  38.         newNode -> fwdPtr = nullptr;
  39.  
  40.         trav = llh;
  41.         while(trav -> fwdPtr){
  42.             trav = trav -> fwdPtr;
  43.         }
  44.         trav -> fwdPtr = newNode;
  45.     }
  46. }
  47.  
  48. int list_length(LLnode * &llh){
  49.     int count = 0;
  50.     LLnode * trav = new LLnode;
  51.     trav = llh;
  52.     if(!llh){
  53.         return 0;
  54.     }
  55.     else{
  56.         while(trav){
  57.             count ++;
  58.             trav = trav -> fwdPtr;
  59.         }
  60.         return count;
  61.     }
  62.  
  63. }
  64.  
  65. string retrieve_front (LLnode * &llh){
  66.     if(!llh)
  67.         throw string ("Exception at retrieve back");
  68.     return (llh -> theData);
  69. }
  70.  
  71. string retrieve_back (LLnode * &llh){
  72.     LLnode * trav = new LLnode;
  73.     trav = llh;
  74.     if(!llh)
  75.         throw string ("Exception at retrieve back");
  76.     while(trav -> fwdPtr){
  77.         trav = trav -> fwdPtr;
  78.     }
  79.     return trav -> theData;
  80. }
  81.  
  82. void display_nodes(LLnode * &llh) {
  83.     LLnode * trav = new LLnode;
  84.     trav = llh;
  85.     if(!llh){
  86.         cout << "No nodes to display " << endl;
  87.     }
  88.     else{
  89.         cout << "Displaying nodes: " << endl;
  90.         while(trav){
  91.             cout << trav -> theData << ", ";
  92.             trav = trav -> fwdPtr;
  93.         }
  94.     }
  95.     cout << endl;
  96. }
  97.  
  98. int main(){
  99.     LLnode * theLLHeader1 = nullptr;
  100.     cout << "Main: number of nodes in empty list " << list_length(theLLHeader1) << endl;
  101.     display_nodes(theLLHeader1);
  102.     push_front(theLLHeader1, "aaaaa");
  103.     push_back(theLLHeader1, "bbbbb");
  104.     push_front(theLLHeader1, "before aaaaa");
  105.     push_back(theLLHeader1, "after bbbbb");
  106.     cout << "Main: number of nodes after 4 pushed: " << list_length(theLLHeader1) << endl;
  107.     display_nodes(theLLHeader1);
  108.     cout << "Main: retrieve front: " << retrieve_front (theLLHeader1) << endl;
  109.     cout << "Main: retrieve back: " << retrieve_back (theLLHeader1) << endl;
  110.     cout << endl;
  111.  
  112.     LLnode * theLLHeader2 = nullptr;
  113.  
  114.     push_front(theLLHeader2, "33333");
  115.     push_front(theLLHeader2, "22222");
  116.     push_front(theLLHeader2, "11111");
  117.     push_back(theLLHeader2, "44444");
  118.     push_back(theLLHeader2, "55555");
  119.     push_back(theLLHeader2, "66666");
  120.     display_nodes (theLLHeader2);
  121.  
  122.     return 0;
  123. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top