Advertisement
Guest User

stack nd queue homie

a guest
May 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int top=-1;
  6. int a[5];
  7.  
  8. int front=-1;
  9. int rear=-1;
  10.  
  11.  
  12. void push(int x)
  13. {
  14. if(top==4)
  15. {
  16. cout<<"Stack is full"<<endl;
  17. }
  18. else
  19. {
  20. a[top+1]=x;
  21. top++;
  22.  
  23. }
  24. }
  25.  
  26. void pop()
  27. {
  28. if(top==-1)
  29. {
  30. cout<<"No values in stack"<<endl;
  31. }
  32. else
  33. {
  34. top=top-1;
  35. }
  36. }
  37.  
  38.  
  39. void enqueue(int x)
  40. {
  41. if(rear==4)
  42. {
  43. cout<<"List is full"<<end;
  44. }
  45.  
  46. else
  47. {
  48. if(front==-1)
  49. {
  50. front=0;
  51. rear=0;
  52. a[rear]=x;
  53. }
  54. else{
  55. a[rear+1]=x;
  56. rear++;
  57. }
  58.  
  59. }
  60.  
  61. }
  62.  
  63. void dequeue()
  64. {
  65. if(front==-1)
  66. {
  67. cout<<"No values in the list"<<endl;
  68. }
  69.  
  70. else
  71. {
  72. if(front==rear)
  73. {
  74. front=-1;
  75. rear=-1;
  76. }
  77. else{
  78. front++;
  79.  
  80. }
  81. }
  82. }
  83.  
  84. void display()
  85. {
  86. for(int i=front;i<=rear;i++)
  87. {
  88. cout<<a[i]<<endl;
  89. }
  90. }
  91.  
  92. int main()
  93. {
  94.  
  95. push(10);
  96. push(20);
  97. pop();
  98.  
  99.  
  100.  
  101.  
  102. return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement