Implentation of queue
using arrays
Program:
#include<stdio.h>
#include<conio.h>
#define SIZE 10
int arr[SIZE], front = -1, rear = -1, i;
void enqueue();
void dequeue();
void display();
void main()
{
int ch;
clrscr();
do
{
printf("\n[1].ENQUEUE [2].DEQUEUE [3].Display [4].Exit\n");
printf("\nEnter your choice [1-4] : ");
scanf("%d", &ch);
switch(ch)
{
case 1 :
enqueue();
break;
case 2 :
dequeue();
break;
case 3 :
display();
break;
case 4 :
break;
default :
printf("\nInvalid option\n");
getch();
}
} while(ch != 4);
getch();
}
void enqueue()
{
if(rear == SIZE - 1)
{
printf("\nQueue is full (overflow)\n");
getch();
return;
}
rear ;
printf("\nEnter the element to ENQUEUE : ");
scanf("%d", &arr[rear]);
if(front == -1)
front++;
}
void dequeue()
{
if(front == -1)
{
printf("\nQueue is empty (underflow)\n");
getch();
return;
}
printf("\nThe DEQUEUE element is : %d\n", arr[front]);
getch();
if(front == rear)
front = rear = -1;
else
front++;
}
void display()
{
if(front == -1)
{
printf("\nQueue is empty (underflow)\n");
getch();
return;
}
printf("\nThe elements in queue are :\n\nFRONT ->");
for(i = front; i <= rear; i++)
printf(" ... %d", arr[i]);
printf(" ... <- REAR\n");
getch();
}
Output:
[1].ENQUEUE [2].DEQUEUE [3].Display [4].Exit
Enter your choice [1-4] : 1
Enter the element to ENQUEUE : 10
[1].ENQUEUE [2].DEQUEUE [3].Display [4].Exit
Enter your choice [1-4] : 1
Enter the element to ENQUEUE : 20
[1].ENQUEUE [2].DEQUEUE [3].Display [4].Exit
Enter your choice [1-4] : 3
The elements in queue are :
FRONT -> ... 10 ... 20 ... <- REAR
Enter your choice [1-4] : 2
The DEQUEUE element is : 10
[1].ENQUEUE [2].DEQUEUE [3].Display [4].Exit
Enter your choice [1-4] : 3
The elements in queue are :
FRONT -> ... 20 ... <- REAR
[1].ENQUEUE [2].DEQUEUE [3].Display [4].Exit
Enter your choice [1-4] : 2
The DEQUEUE element is : 20
[1].ENQUEUE [2].DEQUEUE [3].Display [4].Exit
Enter your choice [1-4] : 2
Queue is empty (underflow)
[1].ENQUEUE [2].DEQUEUE [3].Display [4].Exit
Enter your choice [1-4] : 4
-
UpdatedDec 31, 2019
-
Views7,990
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order