fork download
  1. #include<bits/stdc++.h>
  2. #define MAX_SIZE 100
  3. using namespace std;
  4.  
  5. typedef struct {
  6. int items[MAX_SIZE];
  7. int front;
  8. int rear;
  9. } Queue;
  10.  
  11. void initializeQueue(Queue* q)
  12. {
  13. q->front = -1;
  14. q->rear = 0;
  15. }
  16.  
  17. bool isEmpty(Queue* q) { return (q->front == q->rear - 1); }
  18.  
  19. bool isFull(Queue* q) { return (q->rear == MAX_SIZE); }
  20.  
  21. void insertion(Queue* q, int value)
  22. {
  23. if (isFull(q)) {
  24. printf("Queue is full\n");
  25. return;
  26. }
  27. q->items[q->rear] = value;
  28. q->rear++;
  29. }
  30.  
  31. void deletion(Queue* q)
  32. {
  33. if (isEmpty(q)) {
  34. printf("Queue is empty\n");
  35. return;
  36. }
  37. q->front++;
  38. }
  39.  
  40. int peek(Queue* q)
  41. {
  42. if (isEmpty(q)) {
  43. printf("Queue is empty\n");
  44. return -1;
  45. }
  46. return q->items[q->front + 1];
  47. }
  48.  
  49. void display(Queue* q)
  50. {
  51. if (isEmpty(q)) {
  52. printf("\nQueue is empty\n");
  53. return;
  54. }
  55.  
  56. printf("\nCurrent Queue: ");
  57. for (int i = q->front + 1; i < q->rear; i++) {
  58. cout<<q->items[i]<<' ';
  59. }
  60. printf("\n");
  61. }
  62.  
  63. int main()
  64. {
  65. Queue q;
  66. initializeQueue(&q);
  67.  
  68. int n;
  69. string s="\n\n1.insertion,\n2.deletion,\nchoose any: ";
  70. while(cout<<s,cin>>n)
  71. {
  72. int data;
  73. (n-1)? (deletion(&q)):(cout<<"\nData: ",cin>> data,insertion(&q, data));
  74. display(&q);
  75. }
  76.  
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0.01s 5284KB
stdin
1
11
1
13
1
14
1
16
1
18
2
2
2
stdout

1.insertion,
2.deletion,
choose any: 
Data: 
Current Queue: 11 


1.insertion,
2.deletion,
choose any: 
Data: 
Current Queue: 11 13 


1.insertion,
2.deletion,
choose any: 
Data: 
Current Queue: 11 13 14 


1.insertion,
2.deletion,
choose any: 
Data: 
Current Queue: 11 13 14 16 


1.insertion,
2.deletion,
choose any: 
Data: 
Current Queue: 11 13 14 16 18 


1.insertion,
2.deletion,
choose any: 
Current Queue: 13 14 16 18 


1.insertion,
2.deletion,
choose any: 
Current Queue: 14 16 18 


1.insertion,
2.deletion,
choose any: 
Current Queue: 16 18 


1.insertion,
2.deletion,
choose any: