fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX_CAPACITY 5
  5.  
  6. // Define the queue structure
  7. typedef struct {
  8. int data[MAX_CAPACITY];
  9. int front;
  10. int rear;
  11. int size;
  12. } Queue;
  13.  
  14. // Initialize the queue
  15. void initQueue(Queue *q) {
  16. q->front = 0;
  17. q->rear = -1;
  18. q->size = 0;
  19. }
  20.  
  21. // Returns the current size of the queue
  22. int size(Queue *q) {
  23. return q->size;
  24. }
  25.  
  26. // Pushes two values into the queue
  27. void Push(Queue *q, int val1, int val2) {
  28. if (q->size + 2 <= MAX_CAPACITY) {
  29. q->rear = (q->rear + 1) % MAX_CAPACITY;
  30. q->data[q->rear] = val1;
  31. q->size++;
  32.  
  33. q->rear = (q->rear + 1) % MAX_CAPACITY;
  34. q->data[q->rear] = val2;
  35. q->size++;
  36. } else {
  37. printf("Overflow\n");
  38. }
  39. }
  40.  
  41. // Pops the front two elements and pushes their absolute difference
  42. void pop(Queue *q) {
  43. if (q->size < 2) {
  44. printf("Underflow\n");
  45. } else {
  46. int val1 = q->data[q->front];
  47. q->front = (q->front + 1) % MAX_CAPACITY;
  48. q->size--;
  49.  
  50. int val2 = q->data[q->front];
  51. q->front = (q->front + 1) % MAX_CAPACITY;
  52. q->size--;
  53.  
  54. int diff = abs(val1 - val2);
  55. if (q->size < MAX_CAPACITY) {
  56. q->rear = (q->rear + 1) % MAX_CAPACITY;
  57. q->data[q->rear] = diff;
  58. q->size++;
  59. } else {
  60. printf("Overflow\n");
  61. }
  62. }
  63. }
  64.  
  65. // Returns the front element of the queue
  66. int front(Queue *q) {
  67. if (q->size > 0) {
  68. return q->data[q->front];
  69. } else {
  70. return -1; // Queue is empty
  71. }
  72. }
  73.  
  74. // Returns the back element of the queue
  75. int back(Queue *q) {
  76. if (q->size > 0) {
  77. return q->data[q->rear];
  78. } else {
  79. return -1; // Queue is empty
  80. }
  81. }
  82.  
  83. // Main function to demonstrate the queue operations
  84. int main() {
  85. Queue q;
  86. initQueue(&q);
  87.  
  88. // Demonstrate functionalities
  89. Push(&q, 3, 5);
  90. printf("Front: %d\n", front(&q));
  91. printf("Back: %d\n", back(&q));
  92. printf("Size: %d\n", size(&q));
  93.  
  94. Push(&q, 8, 2);
  95. printf("Front: %d\n", front(&q));
  96. printf("Back: %d\n", back(&q));
  97. printf("Size: %d\n", size(&q));
  98.  
  99. pop(&q);
  100. printf("After pop\n");
  101. printf("Front: %d\n", front(&q));
  102. printf("Back: %d\n", back(&q));
  103. printf("Size: %d\n", size(&q));
  104.  
  105. Push(&q, 7, 1);
  106. printf("After pushing more elements\n");
  107. printf("Front: %d\n", front(&q));
  108. printf("Back: %d\n", back(&q));
  109. printf("Size: %d\n", size(&q));
  110.  
  111. return 0;
  112. }
  113.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Front: 3
Back: 5
Size: 2
Front: 3
Back: 2
Size: 4
After pop
Front: 8
Back: 2
Size: 3
After pushing more elements
Front: 8
Back: 1
Size: 5