0% found this document useful (0 votes)
8 views

10

This document contains a C program that implements a queue data structure using linked lists. It includes functions for initializing the queue, enqueuing and dequeuing elements, and displaying the queue's contents. The main function provides a menu for users to interact with the queue through various operations.

Uploaded by

swetha.v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

10

This document contains a C program that implements a queue data structure using linked lists. It includes functions for initializing the queue, enqueuing and dequeuing elements, and displaying the queue's contents. The main function provides a menu for users to interact with the queue through various operations.

Uploaded by

swetha.v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>

// Define the structure for a queue node


typedef struct Node {
int data;
struct Node* next;
} Node;

// Define the structure for the queue


typedef struct {
Node* front;
Node* rear;
} Queue;

// Function to initialize the queue


void initializeQueue(Queue* q) {
q->front = NULL;
q->rear = NULL;
}

// Function to create a new node and return its pointer


Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to enqueue (add) an element to the queue


void enqueue(Queue* q, int data) {
Node* newNode = createNode(data);
if (q->rear == NULL) { // Queue is empty
q->front = newNode;
q->rear = newNode;
} else {
q->rear->next = newNode;
q->rear = newNode;
}
printf("Enqueued %d to the queue.\n", data);
}

// Function to dequeue (remove) an element from the queue


int dequeue(Queue* q) {
if (q->front == NULL) {
printf("Queue is empty. Cannot dequeue.\n");
exit(1);
}
Node* temp = q->front;
int data = temp->data;
q->front = q->front->next;
if (q->front == NULL) { // Queue became empty
q->rear = NULL;
}
free(temp);
return data;
}

// Function to display the elements in the queue


void displayQueue(Queue* q) {
if (q->front == NULL) {
printf("Queue is empty.\n");
return;
}
Node* temp = q->front;
printf("Queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

// Main function
int main() {
Queue q;
initializeQueue(&q);
int choice, value;

while (1) {
printf("\nQueue Operations Menu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display Queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
value = dequeue(&q);
printf("Dequeued %d from the queue.\n", value);
break;
case 3:
displayQueue(&q);
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}

return 0;
}

You might also like