My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
|
C Program of priority queue using linked list
Posted Date: 24 Mar 2008 Resource Type: Articles/Knowledge Sharing Category: Computer & Technology
|
Posted By: ashish singh Member Level: Diamond Rating: Points: 4
|
|
|
|
/* Program of priority queue using linked list*/ # include # include
struct node { int priority; int info; struct node *link; }*front = NULL;
main() { int choice; while(1) { printf("1.Insert\n"); printf("2.Delete\n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : "); scanf("%d", &choice);
switch(choice) { case 1: insert(); break; case 2: del(); break; case 3: display(); break; case 4: exit(1); default : printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/
insert() { struct node *tmp,*q; int added_item,item_priority; tmp = (struct node *)malloc(sizeof(struct node)); printf("Input the item value to be added in the queue : "); scanf("%d",&added_item); printf("Enter its priority : "); scanf("%d",&item_priority); tmp->info = added_item; tmp->priority = item_priority; /*Queue is empty or item to be added has priority more than first item*/ if( front == NULL || item_priority < front->priority ) { tmp->link = front; front = tmp; } else { q = front; while( q->link != NULL && q->link->priority <= item_priority ) q=q->link; tmp->link = q->link; q->link = tmp; }/*End of else*/ }/*End of insert()*/
del() { struct node *tmp; if(front == NULL) printf("Queue Underflow\n"); else { tmp = front; printf("Deleted item is %d\n",tmp->info); front = front->link; free(tmp); } }/*End of del()*/
display() { struct node *ptr; ptr = front; if(front == NULL) printf("Queue is empty\n"); else { printf("Queue is :\n"); printf("Priority Item\n"); while(ptr != NULL) { printf("%5d %5d\n",ptr->priority,ptr->info); ptr = ptr->link; } }/*End of else */ }/*End of display() */
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|
Watch TV Channels
|