Priority Queue Prof Sainath Patil VCET Vasai Rd

  • Slides: 10
Download presentation
Priority Queue Prof. Sainath Patil, VCET Vasai Rd. . .

Priority Queue Prof. Sainath Patil, VCET Vasai Rd. . .

Priority Queue • Priority Queue is more specialized data structure than Queue. • Like

Priority Queue • Priority Queue is more specialized data structure than Queue. • Like ordinary queue, priority queue has same method but with a major difference. • In Priority queue items are ordered by key value so that item with the lowest value of key is at front and item with the highest value of key is at rear or vice versa. • So we're assigned priority to item based on its key value. • Lower the value, higher the priority. Prof. Sainath Patil, VCET Vasai Rd. . .

Program of priority queue using linked list #include<stdio. h> #include<stdlib. h> struct node {

Program of priority queue using linked list #include<stdio. h> #include<stdlib. h> struct node { int priority; int data; struct node *next; }*front=NULL; void insert(int item, int item_priority); int del(); void display(); int is. Empty(); Prof. Sainath Patil, VCET Vasai Rd. . .

void main() { int choice, item_priority; while(1) { printf("1. Insertn 2. Deleten 3. Displayn

void main() { int choice, item_priority; while(1) { printf("1. Insertn 2. Deleten 3. Displayn 4. Quitn"); printf("Enter your choice : "); scanf("%d", &choice); Prof. Sainath Patil, VCET Vasai Rd. . .

switch(choice) { case 1: printf("Input the item to be added in the queue :

switch(choice) { case 1: printf("Input the item to be added in the queue : "); scanf("%d", &item); printf("Enter its priority : "); scanf("%d", &item_priority); insert(item, item_priority); break; case 2: printf("Deleted item is %dn", del()); break; case 3: display(); break; case 4: exit(1); default : printf("Wrong choicen"); }/*End of switch }/*End of while */ }End of main()*/ Prof. Sainath Patil, VCET Vasai Rd. . .

int is. Empty() { if( front == NULL ) return 1; else return 0;

int is. Empty() { if( front == NULL ) return 1; else return 0; }/*End of is. Empty()*/ Prof. Sainath Patil, VCET Vasai Rd. . .

void insert(int item, int item_priority) { struct node *tmp, *p; tmp=(struct node *)malloc(sizeof(struct node));

void insert(int item, int item_priority) { struct node *tmp, *p; tmp=(struct node *)malloc(sizeof(struct node)); tmp->data=item; tmp->priority=item_priority; /*Queue is empty or item to be added has priority more than first element*/ if( is. Empty() || item_priority < front->priority ) { tmp->next=front; front=tmp; } Prof. Sainath Patil, VCET Vasai Rd. . .

else { p = front; while( p->next!=NULL && p->next->priority<=item_priority ) { p=p->next; } tmp->next=p->next;

else { p = front; while( p->next!=NULL && p->next->priority<=item_priority ) { p=p->next; } tmp->next=p->next; p->next=tmp; } }/*End of insert()*/ Prof. Sainath Patil, VCET Vasai Rd. . .

int del() { struct node *tmp; int item; if( is. Empty() ) { printf("Queue

int del() { struct node *tmp; int item; if( is. Empty() ) { printf("Queue Underflown"); exit(1); } else { tmp=front; item=tmp->data; front=front->next; free(tmp); } return item; }/*End of del()*/ Prof. Sainath Patil, VCET Vasai Rd. . .

void display() { struct node *ptr; ptr=front; if( is. Empty() ) printf("Queue is emptyn");

void display() { struct node *ptr; ptr=front; if( is. Empty() ) printf("Queue is emptyn"); else { printf("Queue is : n"); printf("Priority Itemn"); while(ptr!=NULL) { printf("%5 d %5 dn", ptr->priority, ptr->data); ptr=ptr->next; } } }/*End of display() */ Prof. Sainath Patil, VCET Vasai Rd. . .