A Queue is an ordered collection of items to which new items may be inserted from one end called rear of the Queue and items may be deleted from the other end called front of the queue. Unlike that of the array, the definition of the Queue provides for the insertion and deletion of items, so that a Queue is dynamic, constantly changing object.
FIFO - First In First Out
• Placing an item in a queue is called “insertion or enqueue”, which is done at the end of the queue called “rear”.
• Removing an item from a queue is called “deletion or dequeue”, which is done at the other end of the queue called “front”.
• Some of the applications are: printer queue, keystroke queue, etc.
/* Implementation of Queue as an array */
#include<stdio.h>
#include<conio.h>
#define SIZE 5
int que[SIZE];
int front,rear,ele,k;
void add(int);
int del ();
void show();
IsEmpty();
IsFull();
void main()
{
int ch;
clrscr();
front=rear=k=0;
do
{
clrscr();
gotoxy(30,1); printf("1. Add Element");
gotoxy(30,2); printf("2. Delete Element");
gotoxy(30,3); printf("3. Show all Elements");
gotoxy(30,4); printf("4. Exit");
gotoxy(30,6); printf("Enter Your Choice :");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(IsFull())
printf("\n Queue is Full");
else
{
printf("\n Enter Element to insert :");
scanf("%d", &ele);
add(ele);
}
getch();
break;
case 2:
if(IsEmpty())
printf("\n Queue is Empty");
else
printf("\n Removed Element is :%d", del ());
getch();
break;
case 3:
if(IsEmpty())
printf("\n Queue is Empty");
else
show();
getch();
break;
}
}while(ch!=4);
}
IsEmpty()
{
return(k==0);
}
IsFull()
{
return(k==SIZE);
}
void add(int ele)
{
que[rear]=ele;
rear++;
k++;
}
int del ()
{
front++;
k--;
return(que[front-1]);
}
void show()
{
int i;
printf("\n The Queue Elements are......\n");
for(i=front;i<rear;i++)
printf("%d\n", que[i]);
}
No comments:
Post a Comment