Advertisement
SabirSazzad

Link list Stack implement

Feb 26th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node
  5. {
  6.    int data;
  7.    Node *point;
  8. };
  9.  
  10. Node *head;
  11. Node* getnewnode(int value)
  12. {
  13.     Node *newnode = new Node();
  14.     newnode -> data = value;
  15.     newnode -> point = NULL;
  16.     return newnode;
  17. }
  18. void Push(int value, int limit)
  19. {
  20.     int Count = 1;
  21.     Node *temp = head;
  22.     while(temp != NULL)
  23.     {
  24.         Count++;
  25.         temp = temp->point;
  26.     }
  27.     if(Count <= limit)
  28.     {
  29.         Node *newnode = getnewnode(value);
  30.         newnode -> point = head;
  31.         head = newnode;
  32.     }
  33.     else
  34.     {
  35.         cout << "\nStack Over Flow\n" <<endl;
  36.     }
  37. }
  38. void Pop()
  39. {
  40.     if(head==NULL)
  41.     {
  42.         cout << "\nStack Empty\n" <<endl;
  43.         return;
  44.     }
  45.     Node *temp = head;
  46.     head = temp->point;
  47.     delete temp;
  48. }
  49. void print()
  50. {
  51.     Node *temp = head;
  52.     cout << "Current Link List data...." <<endl;
  53.     while(temp != NULL)
  54.     {
  55.         cout << temp->data << " ";
  56.         temp = temp->point;
  57.     }
  58.     cout <<endl;
  59. }
  60. int main()
  61. {
  62.     int limit;
  63.     limit = 4;
  64.     cout << "Stack limit 4\n" <<endl;
  65.     Push(10,limit);
  66.     print();
  67.     Push(20,limit);
  68.     print();
  69.     Push(30,limit);
  70.     print();
  71.     Push(40,limit);
  72.     print();
  73.     Push(50,limit);// Stack will over flow
  74.     Pop();
  75.     print();
  76.     Pop();
  77.     print();
  78.     Pop();
  79.     print();
  80.     Pop();
  81.     print();
  82.     Pop(); // Stack Empty Nothing to pop
  83.  
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement