fork download
  1. #include <limits.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. // Node structure representing a single node in the linked
  6. // list
  7. typedef struct Node {
  8. int data;
  9. struct Node* next;
  10. } Node;
  11.  
  12. // Function to create a new node
  13. Node* createNode(int new_data)
  14. {
  15. Node* new_node = (Node*)malloc(sizeof(Node));
  16. new_node->data = new_data;
  17. new_node->next = NULL;
  18. return new_node;
  19. }
  20.  
  21. // Structure to implement queue operations using a linked
  22. // list
  23. typedef struct Queue {
  24.  
  25. // Pointer to the front and the rear of the linked list
  26. Node *front, *rear;
  27. } Queue;
  28.  
  29. // Function to create a queue
  30. Queue* createQueue()
  31. {
  32. Queue* q = (Queue*)malloc(sizeof(Queue));
  33. q->front = q->rear = NULL;
  34. return q;
  35. }
  36.  
  37. // Function to check if the queue is empty
  38. int isEmpty(Queue* q)
  39. {
  40.  
  41. // If the front and rear are null, then the queue is
  42. // empty, otherwise it's not
  43. if (q->front == NULL && q->rear == NULL) {
  44. return 1;
  45. }
  46. return 0;
  47. }
  48.  
  49. // Function to add an element to the queue
  50. void enqueue(Queue* q, int new_data)
  51. {
  52.  
  53. // Create a new linked list node
  54. Node* new_node = createNode(new_data);
  55.  
  56. // If queue is empty, the new node is both the front
  57. // and rear
  58. if (q->rear == NULL) {
  59. q->front = q->rear = new_node;
  60. return;
  61. }
  62.  
  63. // Add the new node at the end of the queue and
  64. // change rear
  65. q->rear->next = new_node;
  66. q->rear = new_node;
  67. }
  68.  
  69. // Function to remove an element from the queue
  70. void dequeue(Queue* q)
  71. {
  72.  
  73. // If queue is empty, return
  74. if (isEmpty(q)) {
  75. printf("Queue Underflow\n");
  76. return;
  77. }
  78.  
  79. // Store previous front and move front one node
  80. // ahead
  81. Node* temp = q->front;
  82. q->front = q->front->next;
  83.  
  84. // If front becomes null, then change rear also
  85. // to null
  86. if (q->front == NULL)
  87. q->rear = NULL;
  88.  
  89. // Deallocate memory of the old front node
  90. free(temp);
  91. }
  92.  
  93. // Function to get the front element of the queue
  94. int getFront(Queue* q)
  95. {
  96.  
  97. // Checking if the queue is empty
  98. if (isEmpty(q)) {
  99. printf("Queue is empty\n");
  100. return INT_MIN;
  101. }
  102. return q->front->data;
  103. }
  104.  
  105. // Function to get the rear element of the queue
  106. int getRear(Queue* q)
  107. {
  108.  
  109. // Checking if the queue is empty
  110. if (isEmpty(q)) {
  111. printf("Queue is empty\n");
  112. return INT_MIN;
  113. }
  114. return q->rear->data;
  115. }
  116.  
  117. // Driver code
  118. int main()
  119. {
  120. Queue* q = createQueue();
  121.  
  122. // Enqueue elements into the queue
  123. enqueue(q, 10);
  124. enqueue(q, 20);
  125.  
  126. printf("Queue Front: %d\n", getFront(q));
  127. printf("Queue Rear: %d\n", getRear(q));
  128.  
  129. // Dequeue elements from the queue
  130. dequeue(q);
  131. dequeue(q);
  132.  
  133.  
  134. // Enqueue more elements into the queue
  135. enqueue(q, 30);
  136. enqueue(q, 40);
  137. enqueue(q, 50);
  138.  
  139. // Dequeue an element from the queue
  140. dequeue(q);
  141.  
  142. printf("Queue Front: %d\n", getFront(q));
  143. printf("Queue Rear: %d\n", getRear(q));
  144. return 0;
  145. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Queue Front: 10
Queue Rear: 20
Queue Front: 40
Queue Rear: 50