0% found this document useful (0 votes)
12 views5 pages

Queueeeee

The document contains a C program that implements a queue using a linked list. It provides functions to enqueue, dequeue, and display the elements of the queue, along with a menu-driven interface for user interaction. The program successfully handles user inputs and outputs the results of operations performed on the queue.

Uploaded by

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

Queueeeee

The document contains a C program that implements a queue using a linked list. It provides functions to enqueue, dequeue, and display the elements of the queue, along with a menu-driven interface for user interaction. The program successfully handles user inputs and outputs the results of operations performed on the queue.

Uploaded by

Bhumika Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAM:

#include <stdio.h>
#include <stdlib.h>

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

struct Node* front = NULL;


struct Node* rear = NULL;

void enqueue(int value) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Queue overflow!\n");
return;
}
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("Enqueued %d into the queue.\n", value);
}
void dequeue() {
if (front == NULL) {
printf("Queue is empty! Nothing to dequeue.\n");
return;
}
struct Node* temp = front;
printf("Dequeued %d from the queue.\n", front->data);
front = front->next;
if (front == NULL) {
rear = NULL;
}
free(temp);
}

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

int main() {
int choice, value;
while (1) {
printf("\nMenu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
OUTPUT:

Menu:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter value to enqueue: 23
Enqueued 23 into the queue.

Menu:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter value to enqueue: 23
Enqueued 23 into the queue.

Menu:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter value to enqueue: 24
Enqueued 24 into the queue.

Menu:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Dequeued 23 from the queue.

Menu:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Queue elements: 23 -> 24 -> NULL

Menu:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting program.

=== Code Execution Successful ===

You might also like