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

Q UEUE

Uploaded by

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

Q UEUE

Uploaded by

Ashwin Harikumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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