Bittle

LinkedList.cpp

Oct 10th, 2016
162
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <string>
  4. using namespace std;
  5.  
  6. class node {
  7. public:
  8.     node * next;
  9.     string data;
  10.  
  11. };
  12.  
  13. class linkedList {
  14.  
  15. private:
  16.     node * head;
  17.  
  18. public:
  19.     linkedList() {
  20.         head = NULL;
  21.     }
  22.  
  23.     void addFront(string item) {
  24.         node * temp = new node;
  25.  
  26.         temp->data = item;
  27.         temp->next = head;
  28.         head = temp;
  29.     }
  30.  
  31.     void addBack(string item) {
  32.         node * temp = new node;
  33.         temp->data = item;
  34.         temp->next = NULL;
  35.  
  36.         // empty list
  37.         if (head == NULL) {
  38.             head = temp;
  39.         }
  40.         // not empty list
  41.         else {
  42.             node * current = head;
  43.  
  44.             while (current->next != NULL) {
  45.                 current = current->next;
  46.             }
  47.             current->next = temp;
  48.         }
  49.     }
  50.    
  51.     // returns the first item on the list and removes it
  52.     string pop() {
  53.         string t = head -> data;
  54.         node * deleteThis = head;
  55.         head = head->next;
  56.         delete deleteThis;
  57.         return t;
  58.     }
  59.  
  60.     void remove(string item) {
  61.         if (head == NULL) {
  62.        
  63.         }
  64.         else if (head->next == NULL) {
  65.             head = NULL;
  66.         }
  67.         else {
  68.             node * previous = head;
  69.             node * current = head;
  70.             //cout << "in else, item = " <<item<< endl;
  71.             while (current != NULL) {
  72.                 if (current->data != item) {
  73.                     current = current->next;
  74.                     if (previous->next != current) {
  75.                         previous = previous->next;
  76.                     }
  77.                 }
  78.                 else {
  79.                     break;
  80.                 }
  81.             }
  82.  
  83.             cout << previous->data << " ++ " << current->data << endl;
  84.             if (current->next == NULL) {
  85.                 previous->next = NULL;
  86.             }
  87.             if (previous == head) {
  88.                 head = head->next;
  89.                 //delete previous;
  90.             }
  91.  
  92.  
  93.         }
  94.     }
  95.  
  96.     void display() {
  97.         cout << "\n\n== START OF DISPLAY ==" << endl;
  98.         node * zomb = head;
  99.  
  100.         while (zomb != NULL) {
  101.             cout << zomb->data << endl;
  102.             zomb = zomb->next;
  103.         }
  104.         cout << "== END OF DISPLAY ==\n\n" << endl;
  105.     }
  106. };
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment