Jan 9, 2012

C Program to implement Priority Queue using structure.

Data structures using C,
Write a C Program to implement Priority Queue using structure.
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 ascending order priority queue using the structure, here items are inserted in ascending order. Structure is a c composite data type, in which we can define all the data types under the same name or object.Read more about C Programming Language .

/***********************************************************
* 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/


#define SIZE 5            /* Size of Queue */
int f=0,r=-1;       /* Global declarations */
typedef struct PRQ
{
 int ele;
 int pr;
}PriorityQ;

PriorityQ PQ[SIZE];

PQinsert(int elem, int pre)
{
 int i;       /* Function for Insert operation */
 if( Qfull()) printf("\n\n Overflow!!!!\n\n");
 else
 {
  i=r;
  ++r;
  while(PQ[i].pr >= pre && i >= 0) /* Find location for new elem */
  {
   PQ[i+1]=PQ[i];
   i--;
  }
  PQ[i+1].ele=elem;
  PQ[i+1].pr=pre;
 }
}

PriorityQ PQdelete()
{                      /* Function for Delete operation */
 PriorityQ p;
 if(Qempty()){ printf("\n\nUnderflow!!!!\n\n");
 p.ele=-1;p.pr=-1;
 return(p); }
 else
 {
  p=PQ[f];
  f=f+1;
  return(p);
 }
}

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,%d] ",PQ[i].ele,PQ[i].pr);
  printf("<-Rear");
 }
}

main()
{                         /* Main Program */
 int opn;
 PriorityQ p;
 do
 {
  clrscr();
  printf("\n ### Priority Queue Operations(DSC order) ### \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 and its Priority?");
  scanf("%d%d",&p.ele,&p.pr);
  PQinsert(p.ele,p.pr); break;
  case 2: p=PQdelete();
  if( p.ele != -1)
   printf("\n\nDeleted Element is %d \n",p.ele);
  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);
}


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

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!
To browse more C Programs visit this link