Advertisement
Waliul

Stack Implementation (Linked List)

Jan 16th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define mx 2
  5. int i(0);
  6.  
  7. class node
  8. {
  9. public:
  10. int info;
  11. node *link;
  12. };
  13.  
  14. class stackk
  15. {
  16. node *head;
  17. public:
  18. stackk()
  19. {
  20. head = NULL;
  21. }
  22. void push(int val)
  23. {
  24. node *ptr = new node();
  25. ptr->info = val;
  26. ptr->link = head;
  27. head = ptr;
  28. i++;
  29. }
  30.  
  31. int pop()
  32. {
  33. int val = head->info;
  34. head = head->link;
  35. i--;
  36. return val;
  37. }
  38.  
  39. bool isEmpty()
  40. {
  41. if(head == NULL)
  42. {
  43. cout<<"\nStack Underflow!!!!\n\n";
  44. return true;
  45. }
  46. return false;
  47. }
  48.  
  49. bool isFull()
  50. {
  51. if(i >= mx)
  52. {
  53. cout<<"\nStack Overflow!!!\n\n";
  54. return true;
  55. }
  56. return false;
  57. }
  58.  
  59. };
  60.  
  61. int main()
  62. {
  63. int i, j, k, n;
  64. stackk stk1;
  65. while(true)
  66. {
  67. cout<<"Stack Operations:\n1. Push\n2. Pop\n0. Exit\nEnter a number from 0 to 2 : ";
  68. cin>>k;
  69. if(k == 1)
  70. {
  71. if(!stk1.isFull())
  72. {
  73. cout<<"\nEnter the value you want to push : ";
  74. cin>>n;
  75. stk1.push(n);
  76. cout<<endl;
  77. }
  78. }
  79. else if(k == 2)
  80. {
  81. if(!stk1.isEmpty())
  82. cout<<"\nThe top of the stack : "<<stk1.pop()<<endl<<endl;;
  83. }
  84. else if(k == 0)
  85. break;
  86. else
  87. cout<<"\nEnter correct number!!!\n\n";
  88. }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement