Remember
me?
RegisterForgot Your Password?

Basic Queue operations

Write menu driven program using ‘C’ for Static implementation of Queue. The menu  includes

-          Insert

-          Delete

-          Display

-          Exit

queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once an element is added, all elements that were added before have to be removed before the new element can be invoked.

DOWNLOAD  SOURCE CODE  OUTPUT

CODE:

#define SIZE 5 /* Size of Queue */
int Q[SIZE],f=0,r=-1; /* Global declarations */

Qinsert(int elem)
{ /* Function for Insert operation */
if( Qfull()) printf(“\n\n Overflow!!!!\n\n”);
else
{
++r;
Q[r]=elem;
}
}

int Qdelete()
{ /* Function for Delete operation */
int elem;
if(Qempty()){ printf(“\n\nUnderflow!!!!\n\n”);
return(-1); }
else
{
elem=Q[f];
f=f+1;
return(elem);
}
}

int Qfull()
{ /* Function to Check Queue Full */
if(r==SIZE-1) return 1;
return 0;
}

int Qempty()
{ /* Function to Check Queue Empty */
if(f > r) return 1;
return 0;
}

display()
{ /* Function to display status of Queue */
int i;
if(Qempty()) printf(” \n Empty Queue\n”);
else
{
printf(“Front->”);
for(i=f;i<=r;i++)
printf(“%d “,Q[i]);
printf(“<-Rear”);
}
}

main()
{ /* Main Program */
int opn,elem;
do
{
clrscr();
printf(“\n ### Queue Operations ### \n\n”);
printf(“\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n”);
printf(“\n Your option ? “);
scanf(“%d”,&opn);
switch(opn)
{
case 1: printf(“\n\nRead the element to be Inserted ?”);
scanf(“%d”,&elem);
Qinsert(elem); break;
case 2: elem=Qdelete();
if( elem != -1)
printf(“\n\nDeleted Element is %d \n”,elem);
break;
case 3: printf(“\n\nStatus of Queue\n\n”);
display(); break;
case 4: printf(“\n\n Terminating \n\n”); break;
default: printf(“\n\nInvalid Option !!! Try Again !! \n\n”);
break;
}
printf(“\n\n\n\n Press a Key to Continue . . . “);
getch();
}while(opn != 4);
}

OUTPUT:

### Queue Operations ###
Press 1-Insert, 2-Delete,3-Display,4-Exit

Your option ?
1
Read the element to be Inserted ?3
Press a Key to Continue . . .
### Queue Operations ###
Press 1-Insert, 2-Delete,3-Display,4-Exit

Your option ? 1
Read the element to be Inserted ?6
Press a Key to Continue . . .
### Queue Operations ###
Press 1-Insert, 2-Delete,3-Display,4-Exit

Your option ? 1
Read the element to be Inserted ?9
Press a Key to Continue . . .

 

### Queue Operations ###
Press 1-Insert, 2-Delete,3-Display,4-Exit

Your option ? 3
Status of Queue

Front->3 6 9 <-Rear

 

Press a Key to Continue . . .

 

### Queue Operations ###
Press 1-Insert, 2-Delete,3-Display,4-Exit

Your option ? 2
Deleted Element is 3
Press a Key to Continue . . .

 

 

### Queue Operations ###
Press 1-Insert, 2-Delete,3-Display,4-Exit

Your option ? 3
Status of Queue

Front->6 9 <-Rear

 

Press a Key to Continue . . .