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

Queue_Implementations

The document provides two implementations of a queue: one using an array and the other using a linked list. Each implementation includes functions for enqueueing, dequeueing, peeking at the front element, and displaying the elements in the queue. The main function allows users to interact with the queue through a menu-driven interface.

Uploaded by

medidhag54
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Queue_Implementations

The document provides two implementations of a queue: one using an array and the other using a linked list. Each implementation includes functions for enqueueing, dequeueing, peeking at the front element, and displaying the elements in the queue. The main function allows users to interact with the queue through a menu-driven interface.

Uploaded by

medidhag54
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Queue Implementations (Converted from Stack)

1. Queue Implementation Using Array


#include <stdio.h>
#define SIZE 10

int front = -1, rear = -1, queue[SIZE];

void enqueue();
void dequeue();
void peek();
void display();

int main()
{
int choice;
printf("Operations of Queue:
");
printf("1.Enqueue\n2.Dequeue\n3.Peek\n4.Display\n5.Exit\n");

while(1)
{
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: peek(); break;
case 4: display(); break;
case 5: return 0;
default: printf("\nInvalid choice, try again.\n");
}
}
}

void enqueue()
{
int n;
if(rear == SIZE - 1)
{
printf("\nQueue overflow\n");
}
else
{
printf("Enter the element to be added to the queue: ");
scanf("%d", &n);

if (front == -1)
front = 0; // Set front for the first element

rear = rear + 1;
queue[rear] = n;
}
}

void dequeue()
{
if(front == -1 || front > rear)
{
printf("\nQueue underflow\n");
}
else
{
printf("Dequeued element: %d\n", queue[front]);
front = front + 1;

if (front > rear)


front = rear = -1; // Reset queue when empty
}
}

void peek()
{
if(front == -1)
{
printf("\nQueue is empty, nothing to peek.\n");
}
else
{
printf("Front element of the queue: %d\n", queue[front]);
}
}
void display()
{
int i;
if(front == -1)
{
printf("\nQueue is empty (underflow)\n");
}
else
{
printf("\nElements present in the queue:\n");
for(i = front; i <= rear; i++)
{
printf("%d\t", queue[i]);
}
printf("\n");
}
}

2. Queue Implementation Using Linked List


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

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

struct Node *front = NULL, *rear = NULL;

void enqueue();
void dequeue();
void peek();
void display();

int main()
{
int choice;
printf("Operations of Queue:\n");
printf("1.Enqueue\n2.Dequeue\n3.Peek\n4.Display\n5.Exit\n");

while(1)
{
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: peek(); break;
case 4: display(); break;
case 5: return 0;
default: printf("\nInvalid choice, try again.\n");
}
}
}

void enqueue()
{
int item;
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL)
{
printf("\nMemory allocation failed\n");
return;
}

printf("Enter the element to enqueue: ");


scanf("%d", &item);

newNode->data = item;
newNode->next = NULL;

if (rear == NULL) // If queue is empty


{
front = rear = newNode;
}
else
{
rear->next = newNode; // Link the new node at the end
rear = newNode; // Update rear
}

printf("Item %d enqueued.\n", item);


}

void dequeue()
{
if (front == NULL) // Check if the queue is empty
{
printf("\nQueue underflow\n");
}
else
{
struct Node* temp = front;
printf("Dequeued item: %d\n", temp->data);
front = front->next; // Move the front pointer

if (front == NULL) // If the queue becomes empty


rear = NULL;

free(temp); // Free the memory of the dequeued node


}
}

void peek()
{
if (front == NULL)
{
printf("\nQueue is empty, nothing to peek.\n");
}
else
{
printf("Front item: %d\n", front->data);
}
}

void display()
{
if (front == NULL) // Check if the queue is empty
{
printf("\nQueue is empty (underflow)\n");
}
else
{
struct Node* temp = front;
printf("\nElements present in the queue:\n");
while (temp != NULL)
{
printf("%d\t", temp->data);
temp = temp->next;
}
printf("\n");
}
}

You might also like