Advertisement
skb50bd

DublyLinkedList

Nov 15th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class Numb {
  8. private:
  9.     int value;
  10.     Numb *next;
  11.     Numb *prev;
  12. public:
  13.     Numb(): value(0), next(NULL), prev(NULL) {}
  14.     Numb(int a): value(a), next(NULL), prev(NULL) {}
  15.     ~Numb() {}
  16.  
  17.     void read() {
  18.         cout << "Enter value: ";
  19.         cin >> value;
  20.     }
  21.     void show() {
  22.         cout << "Value is : " << value << endl;
  23.     }
  24.  
  25.     int getValue() { return value; }
  26.     void setNext(Numb *N) { next = N; }
  27.     void setPrev(Numb *P) { prev = P; }
  28.     Numb * getNext() { return next; }
  29.     Numb * getPrev() { return prev; }
  30. };
  31.  
  32.  
  33. int main() {
  34.     Numb *Head = new Numb();
  35.     Numb *Tail;
  36.     Numb *Current;
  37.     Numb *Previous = Head;
  38.  
  39.     int choice;
  40.  
  41.     while(true) {
  42.         cout << "Enter Choice: " << endl;
  43.         cout << "1. Insert (After Head)" << endl;
  44.         cout << "2. Show All" << endl;
  45.         cout << "3. Search (Search for a specefic integer data)" << endl;
  46.         cout << "4. Delete (Find it and Delete it)" << endl;
  47.         cout << "5. Length (Current length of the list)" << endl;
  48.         cout << "0. Exit" << endl;
  49.         cout << "Choice: ";
  50.  
  51.         cin >> choice;
  52.  
  53.         if(choice == 0)
  54.             exit(1);
  55.  
  56.         else if(choice == 1) {
  57.             while(true) {
  58.                 Current = new Numb();
  59.                 Current -> read();
  60.                 Previous -> setNext(Current);
  61.                 Current -> setPrev(Previous);
  62.  
  63.                 char cont;
  64.                 cout << "Do you want to continue (y/n)? ";
  65.                 cin >> cont;
  66.                 if(cont == 'n' || cont == 'N') {
  67.                     Tail = Current;
  68.                     break;
  69.                 }
  70.                     Previous = Current;
  71.             }
  72.         }
  73.  
  74.         else if(choice == 2)
  75.             for(Current = Head; Current; Current = Current -> getNext())
  76.                 Current -> show();
  77.  
  78.         else if(choice == 3) {
  79.             int searchValue;
  80.             cout << "Enter Value to Find: ";
  81.             int i;
  82.             cin >> searchValue;
  83.             for(i = 1, Current = Head; Current; i++, Current = Current -> getNext())
  84.                 if(Current -> getValue() == searchValue) {
  85.                     cout << "Found in position: " << i << endl;
  86.                     break;
  87.                 }
  88.         }
  89.  
  90.         else if(choice == 4) {
  91.             int searchValue;
  92.             cout << "Enter Value to Delete: ";
  93.             cin >> searchValue;
  94.             for(Current = Head; Current; Current = Current -> getNext()) {
  95.                 if(Current -> getValue() == searchValue) {
  96.                     if(Current == Head) {
  97.                         Head = Current -> getNext();
  98.                         Head -> setPrev(NULL);
  99.                     }
  100.                     else if(Current == Tail) {
  101.                         Tail = Current -> getPrev();
  102.                         Tail -> setNext(NULL);
  103.                     }
  104.                     else {
  105.                         Previous -> setNext(Current -> getNext());
  106.                         (Current -> getNext()) -> setPrev(Previous);
  107.                     }
  108.  
  109.                     delete Current;
  110.                     cout << "Deletion Confirmed" << endl;
  111.                     break;
  112.                 }
  113.                 else
  114.                     Previous = Current;
  115.             }
  116.         }
  117.  
  118.         else if(choice == 5) {
  119.             int len = 0;
  120.             for(Current = Head; Current; Current = Current -> getNext())
  121.                 len++;
  122.             cout << "Length of the list is: " << len << endl;
  123.         }
  124.  
  125.         int bak = 1;
  126.         cout << "Enter 0 to go back: ";
  127.         while(bak)
  128.             cin >> bak;
  129.         cout << endl << endl;
  130.     }
  131.  
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement