Advertisement
Falak_Ahmed_Shakib

stack/queuq

Oct 31st, 2020 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #include<stdio.h>
  2. int MAXISIZE=3;
  3. int stack[3];
  4. int top=-1;
  5.  
  6. int isempty()
  7. {
  8. if(top==-1)return 1;
  9. else return 0;
  10. }
  11. int isfull()
  12. {
  13. if(top==MAXISIZE-1)return 1;
  14. else return 0;
  15. }
  16. int peck()
  17. {
  18. return stack[top];
  19. }
  20. int push(int data)
  21. {
  22. top=top+1;
  23. stack[top]=data;
  24. }
  25. int pop()
  26. {
  27. int data;
  28. data=stack[top];
  29. top=top-1;
  30. return data;
  31.  
  32. }
  33. int main()
  34. {
  35.  
  36. if(isempty())printf("isempty\n");
  37. else printf("%d\n",top+1);
  38.  
  39. if(!isfull())push(1);
  40. if(!isfull())push(2);
  41. if(!isfull())push(3);
  42.  
  43. if(isempty())printf("isempty\n");
  44. else printf("not empty and size = %d\n",top+1);
  45.  
  46.  
  47.  
  48.  
  49. while(!isempty())
  50. {
  51. int data=pop();
  52. printf("%d\n",data);
  53.  
  54. }
  55.  
  56.  
  57.  
  58.  
  59. }
  60.  
  61.  
  62. #include <stdio.h>
  63. int MAXSIZE = 3;
  64. int q[3];
  65. int front = 0;
  66. int rear = -1;
  67. int cnt = 0;
  68.  
  69. int isempty()
  70. {
  71. if(!cnt)
  72. return 1;
  73. else
  74. return 0;
  75. }
  76.  
  77. int isfull()
  78. {
  79. if(cnt == MAXSIZE)
  80. return 1;
  81. else
  82. return 0;
  83. }
  84.  
  85. int peak()
  86. {
  87. return q[front];
  88. }
  89.  
  90. int size()
  91. {
  92. return cnt;
  93. }
  94.  
  95. int enque(int data)
  96. {
  97.  
  98. if( /*isfull()==0 &&*/ rear == MAXSIZE-1)
  99. rear = -1;
  100.  
  101. rear++;
  102.  
  103.  
  104. q[rear] = data;
  105.  
  106. cnt++;
  107. }
  108. int deque()
  109. {
  110. int data = q[front++];
  111.  
  112. if(front == MAXSIZE)
  113. {
  114. front = 0;
  115. }
  116. cnt--;
  117. return data;
  118. }
  119.  
  120.  
  121. void print()
  122. {
  123. printf("Whole deque\n");
  124.  
  125.  
  126. while(!isempty())
  127. {
  128. int data=deque();
  129. printf("%d\n",data);
  130. }
  131. }
  132. int main()
  133. {
  134. int a;
  135. while(1)
  136. {
  137. char str[10];
  138.  
  139.  
  140. scanf("%s",str);
  141. //printf("%s\n",str);
  142.  
  143. if(str[1]=='u')
  144. {
  145. scanf("%d",&a);
  146.  
  147. if(isfull())printf("queue is full\n");
  148.  
  149. else enque(a);
  150. }
  151. else if(str[1]=='o')
  152. {
  153.  
  154. if(isempty())
  155. printf("queue is empty\n");
  156.  
  157. else{
  158. printf("%d\n",deque());
  159. }
  160. }
  161.  
  162. else if(str[1]=='r')
  163. {
  164. print();
  165. }
  166. }
  167.  
  168.  
  169.  
  170. /* while(!isempty())
  171. {
  172. int data=deque();
  173. printf("%d\n",data);
  174. }*/
  175.  
  176. }
  177.  
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement