Data structures using C,Priority QUEUE is a abstract data type in which the objects are inserted with respect to certain priority. In this program, we created the simple descending order priority queue, here items are inserted in descending order. Read more about C Programming Language .
To browse more C Programs visit this link
/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/
#define SIZE 5 /* Size of Queue */
int PQ[SIZE],f=0,r=-1; /* Global declarations */
PQinsert(int elem)
{
int i; /* Function for Insert operation */
if( Qfull()) printf("nn Overflow!!!!nn");
else
{
i=r;
++r;
while(PQ[i] <= elem && i >= 0) /* Find location for new elem */
{
PQ[i+1]=PQ[i];
i--;
}
PQ[i+1]=elem;
}
}
int PQdelete()
{ /* Function for Delete operation */
int elem;
if(Qempty()){ printf("nnUnderflow!!!!nn");
return(-1); }
else
{
elem=PQ[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 Queuen");
else
{
printf("Front->");
for(i=f;i<=r;i++)
printf("%d ",PQ[i]);
printf("<-Rear");
}
}
main()
{ /* Main Program */
int opn,elem;
do
{
clrscr();
printf("n ### Priority Queue Operations(DSC order) ### nn");
printf("n Press 1-Insert, 2-Delete,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("nnRead the element to be Inserted ?");
scanf("%d",&elem);
PQinsert(elem); break;
case 2: elem=PQdelete();
if( elem != -1)
printf("nnDeleted Element is %d n",elem);
break;
case 3: printf("nnStatus of Queuenn");
display(); break;
case 4: printf("nn Terminating nn"); break;
default: printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
}while(opn != 4);
}
Read more Similar C Programs
Data Structures
Learn C Programming
You can easily select the code by double clicking on the code area above.
To get regular updates on new C programs, you can Follow @c_program
You can discuss these programs on our Facebook Page. Start a discussion right now,
our page!
Share this program with your Facebook friends now! by liking it
(you can send this program to your friend using this button)
Like to get updates right inside your feed reader? Grab our feed!
(c) www.c-program-example.com