Advertisement
skb50bd

SinglyLinkedList

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