#include <stdio.h>
#include <stdlib.h>
// Structure to represent a node in the queue
struct Node {
int data;
struct Node* next;
};
// Structure to represent the queue with pointers to the front and rear
struct Queue {
struct Node *front, *rear;
};
// Function to create a new node with the given data
struct Node* newNode(int data) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
return temp;
}
// Function to create an empty queue
struct Queue* createQueue() {
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
// Function to add an element to the queue
void enqueue(struct Queue* q, int data) {
// Create a new node
struct Node* temp = newNode(data);
// If the queue is empty, the new node will be the front and rear
if (q->rear == NULL) {
q->front = q->rear = temp;
printf("Enqueued %d to the queue.\n", data);
return;
}
// Attach the new node at the end of the queue and change the rear
q->rear->next = temp;
q->rear = temp;
printf("Enqueued %d to the queue.\n", data);
}
// Function to remove an element from the queue
void dequeue(struct Queue* q) {
// If the queue is empty, there is nothing to dequeue
if (q->front == NULL) {
printf("Queue is empty, nothing to dequeue.\n");
return;
}
// Store the previous front and move the front one node ahead
struct Node* temp = q->front;
q->front = q->front->next;
// If the front becomes NULL, then the queue is empty, so set rear to NULL
if (q->front == NULL) {
q->rear = NULL;
}
printf("Dequeued %d from the queue.\n", temp->data);
free(temp);
}
// Function to display the current elements of the queue
void displayQueue(struct Queue* q) {
struct Node* temp = q->front;
if (temp == NULL) {
printf("Queue is empty.\n");
return;
}
printf("Queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// Main function to demonstrate queue operations
int main() {
struct Queue* q = createQueue();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
displayQueue(q);
dequeue(q);
displayQueue(q);
dequeue(q);
dequeue(q);
dequeue(q);
return 0;
}
Explanation:
- Data Structure Design:
- The
Node
structure represents a node in the linked list, containingdata
(to store the integer value) andnext
(a pointer to the next node). - The
Queue
structure has two pointers:front
(points to the front of the queue) andrear
(points to the end of the queue).
- The
- Node Creation:
newNode(int data)
: This function creates a new node usingmalloc
and initializes it with the provideddata
. It returns a pointer to the newly created node.
- Queue Initialization:
createQueue()
: This function initializes a newQueue
by allocating memory for it and setting bothfront
andrear
pointers toNULL
, indicating an empty queue.
- Enqueue Operation:
enqueue(struct Queue* q, int data)
:- A new node is created.
- If the
rear
isNULL
, it means the queue is empty, so bothfront
andrear
pointers are set to this new node. - Otherwise, the
next
pointer of the currentrear
is updated to the new node, and thenrear
is updated to this new node.
- Dequeue Operation:
dequeue(struct Queue* q)
:- If the
front
isNULL
, the queue is empty, so a message is printed. - Otherwise, the front node is removed, and the
front
pointer is updated to the next node in the queue. - If the queue becomes empty after the removal (i.e.,
front
becomesNULL
), thenrear
is also set toNULL
.
- If the
- Display Function:
displayQueue(struct Queue* q)
: It traverses fromfront
torear
and prints each node’sdata
.
- Main Function:
- Creates an empty queue.
- Enqueues elements
10
,20
, and30
. - Displays the queue.
- Dequeues elements one by one and displays the queue after each operation.
Enqueued 10 to the queue.
Enqueued 20 to the queue.
Enqueued 30 to the queue.
Queue elements: 10 20 30
Dequeued 10 from the queue.
Queue elements: 20 30
Dequeued 20 from the queue.
Dequeued 30 from the queue.
Queue is empty, nothing to dequeue.
Explanation of the Output:
- The elements
10
,20
, and30
are added to the queue. After enqueueing, the queue looks like10 -> 20 -> 30
. - The first
dequeue
operation removes10
, leaving20 -> 30
. - The next
dequeue
removes20
, leaving only30
. - After removing
30
, the queue becomes empty. - Trying to dequeue from an empty queue gives a message indicating that there is nothing to remove.
This implementation demonstrates how to use a linked list to efficiently manage a queue, ensuring that elements can be added or removed dynamically as needed.