Q UEUE

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

PROGRAM: QUEUE USING ARRAY

#include<stdio.h>
#define MAX 3
int queue[MAX];
int front = -1;
int rear = -1;
int item;
void enqueue();
void dequeue();
void display();
void main()
{
int ch = 0;
while(ch != 4)
{
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter the choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
}
}

void enqueue()
{
if(rear == MAX - 1)
{
printf("Overflow\n");
}
else
{
printf("Insert the element: ");
scanf("%d", &item);

if(front == -1 && rear == -1)


{
front = 0;
rear = 0;
}
else
{
rear = rear + 1;
}
queue[rear] = item;
printf("Inserted %d\n", item);
}
}

void dequeue()
{
if(front == -1 && rear == -1)
{
printf("Underflow\n");
}
else
{
item = queue[front];
printf("Deleted %d\n", item);

if(front == rear)
{
front = -1;
rear = -1;
}
else
{
front = front + 1;
}
}
}

void display()
{
if(front == -1)
{
printf("Queue is empty\n");
}
else
{
printf("Queue elements are: ");
for(int i = front; i <= rear; i++)
{
printf("%d ", queue[i]);
}
printf("\n");
}
}
OUTPUT
ubuntu@ubuntu-H81M-S:~/AshwinHarikumar-10$ ./a.out
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Insert the element in queue: 12
Inserted 12
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Insert the element in queue: 14
Inserted 14
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Insert the element in queue: 15
Inserted 15
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Deleted 12
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Queue elements are: 14 15
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting...

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy