Bittle

My Linked List

Oct 10th, 2016
179
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 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.             // special cases, if items isnt found
  69.             bool flag = false;
  70.  
  71.             node * previous = head;
  72.             node * current = head;
  73.             while (current != NULL) {
  74.                 if (current->data != item) {
  75.                     current = current->next;
  76.                     if (previous->next != current) {
  77.                         previous = previous->next;
  78.                     }
  79.                 }
  80.                 else {
  81.                     flag = true;
  82.                     break;
  83.                 }
  84.             }
  85.             if (flag) {
  86.                 if (current->next == NULL) {
  87.                     previous->next = NULL;
  88.                 }
  89.                 else {
  90.                     if (previous == head) {
  91.                         head = head->next;
  92.                     }
  93.                     else {
  94.                         previous->next = current->next;
  95.                     }
  96.                 }
  97.             }
  98.             else {
  99.                 // debugg code
  100.                 cout << "item " << item << " wasn't found in the list" << endl;
  101.             }
  102.             delete current;
  103.         }
  104.     }
  105.    
  106.     // sort the items of the linked list
  107.     void sort() {
  108.  
  109.         if (head == NULL  || head->next == NULL) {
  110.             // list is either empty, or has one item, no need to sort
  111.         }
  112.         else {
  113.             node * lefter = head;
  114.             node * smallest = head;
  115.             node * zombie = head->next;
  116.  
  117.             while (lefter != NULL) {
  118.                 zombie = lefter;
  119.                 while (zombie != NULL) {
  120.                     if (zombie->data < smallest->data) {
  121.                         cout << "smaller one  = " << zombie->data << endl;
  122.                         smallest = zombie;
  123.                     }
  124.                     zombie = zombie->next;
  125.                 }
  126.  
  127.                 // switch the values
  128.                 string l = lefter->data;
  129.                 lefter->data = smallest->data;
  130.                 smallest->data = l;
  131.                
  132.                 lefter = lefter->next;
  133.             }
  134.         }
  135.     }
  136.  
  137.     void display() {
  138.         cout << "\n\n== START OF DISPLAY ==" << endl;
  139.         node * zomb = head;
  140.  
  141.         while (zomb != NULL) {
  142.             cout << zomb->data << endl;
  143.             zomb = zomb->next;
  144.         }
  145.         cout << "== END OF DISPLAY ==\n\n" << endl;
  146.     }
  147. };
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment