Implementation of Priority Queue
CODE
👇
- #include<stdio.h>
- #define MAX 50
- typedef struct
- {
- int data[MAX];
- int front,rear;
- }QUEUE;
- void InitQ(QUEUE *pq)
- {
- pq->front = pq->rear = -1;
- }
- void AddQ(QUEUE *pq, int num)
- {
- int i;
- for(i=pq->rear;i>pq->front;i--)
- if(num>pq->data[i])
- pq->data[i+1]=pq->data[i];//shift element
- else
- break;
- pq->data[i+1]=num;
- pq->rear++;
- }
- int DelQ(QUEUE *pq)
- {
- int num;
- pq->front++;
- num=pq->data[pq->front];
- return(num);
- }
- int isempty(QUEUE *pq)
- {
- return(pq->front == pq->rear);
- }
- int isfull(QUEUE *pq)
- {
- return(pq->rear==MAX-1);
- }
- void main()
- {
- int num,choice;
- QUEUE q1;
- InitQ(&q1);
- printf(">>Implementation of Priority Queue<<");
- do
- {
- printf("\n1: Add \n");
- printf("2: Delete \n");
- printf("3: Exit \n");
- printf("Enter Your Choice: \t");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1: printf("Enter Element To Be Added:\t");
- scanf("%d",&num);
- if(isfull(&q1))
- printf("\t>>Queue Overflow<<\n");
- else
- AddQ(&q1,num);
- break;
- case 2: if(isempty(&q1)==1)
- printf("\t>>Queue Is Empty<<\n");
- else
- printf("The Deleted Element Is:\t %d \n",DelQ(&q1));
- break;
- case 3: printf("\t>>END<<");
- break;
- default:printf("\t>>Enter Right Choice<<");
- }
- }
- while(choice!=3);
- }
👉Excute👈
//Output
/*
>>Implementation of Priority Queue<<
1: Add
2: Delete
3: Exit
Enter Your Choice: 2
>>Queue Is Empty<<
1: Add
2: Delete
3: Exit
Enter Your Choice: 1
Enter Element To Be Added: 5
1: Add
2: Delete
3: Exit
Enter Your Choice: 2
The Deleted Element Is: 5
1: Add
2: Delete
3: Exit
Enter Your Choice: 4
>>Enter Right Choice<<
1: Add
2: Delete
3: Exit
Enter Your Choice: 3
>>END<<
*/
//ThE ProFessoR
Comments
Post a Comment