Advertisement
csetanzil

Implemented Stack Using Linked List by Tanzil

May 12th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Node
  5. {
  6. public:
  7.     int info;
  8.     Node * next;
  9. };
  10.  
  11. class List: public Node
  12. {
  13. public:
  14.     Node *head,*last;
  15.     List()
  16.     {
  17.         head = last = NULL;
  18.     }
  19.     void Insert();
  20.     void Delete();
  21.     void Search(int);
  22.     void Display();
  23.     void Traverse();
  24. };
  25.  
  26. void List::Insert()
  27. {
  28.     Node *temp = new Node;
  29.     temp->next = NULL;
  30.     cout<<"Enter value for the new Node : "<<endl;
  31.     cin>>temp->info;
  32.     if(head==NULL)
  33.     {
  34.         head = last = temp;
  35.         return;
  36.     }
  37.     last->next = temp;
  38.     last  = temp;
  39. }
  40.  
  41. void List::Delete()
  42. {
  43.     if(head==NULL)
  44.     {
  45.         cout<<"Stack Underflow : "<<endl;
  46.         return;
  47.     }
  48.     cout<<"The item "<<last->info<<" is deleted " <<endl;
  49.     if(head == last)
  50.     {
  51.         delete head;
  52.         head = last = NULL;
  53.         return;
  54.     }
  55.     Node *cur = head;
  56.     Node * pre = NULL;
  57.     while(cur->next!=NULL)
  58.     {
  59.         pre = cur;
  60.         cur = cur->next;
  61.     }
  62.     delete cur;
  63.     last = pre;
  64.     pre->next = NULL;
  65. }
  66.  
  67. void List::Search(int item)
  68. {
  69.     int position = 0;
  70.     Node *cur = head;
  71.     while(cur!=NULL)
  72.     {
  73.         position++;
  74.         if(cur->info==item)
  75.         {
  76.             cout<<"The item is at "<<position<<" Position : "<<endl;
  77.             return;
  78.         }
  79.         cur = cur->next;
  80.     }
  81.     cout<<"The element doesn't exits: "<<endl;
  82. }
  83.  
  84. void List::Display()
  85. {
  86.     Node *cur = head;
  87.     if(head == NULL)
  88.     {
  89.         cout<<"Stack is Empty " <<endl;
  90.         return;
  91.     }
  92.     while(cur!=NULL)
  93.     {
  94.         cout<<cur->info<<" -> ";
  95.         cur = cur->next;
  96.     }
  97.     cout<<endl;
  98. }
  99.  
  100. void List::Traverse()
  101. {
  102.  
  103.     while(1)
  104.     {
  105.         int chk;
  106.         cout<<"Enter your Choice "<<endl;
  107.         cout<<" 1:Insert \t 2:Delete \t 3:Display \t 4:Search \t 5:Exit "<<endl;
  108.         cin>>chk;
  109.  
  110.         switch(chk)
  111.         {
  112.         case 1:
  113.             Insert();
  114.             break;
  115.         case 2:
  116.             Delete();
  117.             break;
  118.         case 3:
  119.             Display();
  120.             break;
  121.         case 4:
  122.             int item;
  123.             cout<<"Enter Item to be searched :"<<endl;
  124.             cin>>item;
  125.             Search(item);
  126.             break;
  127.         case 5:
  128.             return;
  129.         default:
  130.             cout<<"Wrong Input : Try Again "<<endl;
  131.             fflush(stdin);
  132.  
  133.         }
  134.     }
  135. }
  136.  
  137. int main()
  138. {
  139.     List ob;
  140.     ob.Traverse();
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement