C Program for Simple DSC order Priority QUEUE Implementation using Structure.
Source: Dr. G T Raju, Professor & Head, Dept. of CSE, RNSIT.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #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); } |