Bittle

linkedList

Feb 15th, 2017
180
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. class linkedList {
  6. private:
  7.  
  8.     class node {
  9.     public:
  10.         string data;
  11.         node *next;
  12.         node *prev;
  13.  
  14.         node(string passedData) {
  15.             data = passedData;
  16.             next = NULL;
  17.             prev = NULL;
  18.         }
  19.     };
  20.  
  21.     void display(node *p) {
  22.         if (p->next != NULL) {
  23.             cout << p->data << " ";
  24.             display(p->next);
  25.         } else {
  26.             cout << p->data << endl;
  27.         }
  28.     }
  29.  
  30.     void insertAt(int location, string item, node *p) {
  31.         if (location == 0) {
  32.             node *baby = new node(item);
  33.             (p->prev)->next = baby;
  34.             baby->prev = p->prev;
  35.             p->prev = baby;
  36.             baby->next = p;
  37.         } else {
  38.             insertAt(location - 1, item, p->next);
  39.         }
  40.     }
  41.  
  42.     void remove(string item, node *p) {
  43.         if (p->data == item) {
  44.             if (tail == head && tail == p) {
  45.                 tail = NULL;
  46.                 head = NULL;
  47.                 delete p;
  48.             } else if (head == p) {
  49.                 head = head->next;
  50.                 head->prev = NULL;
  51.                 delete p;
  52.             } else if (tail == p) {
  53.                 tail = tail->prev;
  54.                 tail->next = NULL;
  55.                 delete p;
  56.             } else {
  57.                 (p->next)->prev = p->prev;
  58.                 (p->prev)->next = p->next;
  59.                 delete p;
  60.             }
  61.         } else {
  62.             p = p->next;
  63.             remove(item, p);
  64.         }
  65.     }
  66.  
  67.     node *head;
  68.     node *tail;
  69.     int numItems;
  70.  
  71. public:
  72.     linkedList() {
  73.         head = NULL;
  74.         tail = NULL;
  75.         numItems = 0;
  76.     }
  77.  
  78.  
  79.     void display() {
  80.         display(head);
  81.     }
  82.  
  83.     void addBack(string item) {
  84.         node *baby = new node(item);
  85.         if (head == NULL) {
  86.             head = baby;
  87.             tail = baby;
  88.         } else {
  89.             tail->next = baby;
  90.             baby->prev = tail;
  91.             tail = baby;
  92.         }
  93.  
  94.         numItems+=1;
  95.     }
  96.  
  97.     void insertAt(int location, string item) {
  98.         insertAt(location, item, head);
  99.         numItems +=1;
  100.  
  101.     }
  102.  
  103.     void remove(string item) {
  104.         if (head != NULL) {
  105.             remove(item, head);
  106.             numItems -=1;
  107.         }
  108.     }
  109.  
  110.     int size(){
  111.         return numItems;
  112.     }
  113.  
  114.     bool empty(){
  115.         return numItems == 0;
  116.     }
  117.  
  118.     bool contains(string item){
  119.         bool flag = false;
  120.  
  121.         node * runner = head;
  122.  
  123.         while(runner!=NULL){
  124.             if(runner->data == item){
  125.                 flag = true;
  126.                 break;
  127.             }
  128.             runner = runner->next;
  129.         }
  130.  
  131.         return flag;
  132.     }
  133.  
  134. };
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment