#include<stdio.h> #include<stdlib.h> #define MAX 5 void enqueue(); void dequeue(); void display(); struct node { int data; int priority; struct node * next; }; struct node * head=NULL; int count = 0; main() { int choice; do { printf("\n----MENU----\n"); printf("1.enqueue\n2.dequeue\n3.display\n4.EXIT\n"); printf("\nEnter your choice\n"); scanf("%d",&choice); switch(choice) { case 1:enqueue();break; case 2:dequeue();break; case 3:display();break; case 4:printf("\n~~~~Thank You~~~~\n");break; default:printf("\nINVALID INPUT !!!!\n"); } }while(choice!=4); } void enqueue() { if(MAX > count) { count++; struct node * newnode, *temp,*pretemp; temp=head; newnode=(struct node *)malloc(sizeof(struct node)); printf("\nEnter ELEMENT and its PRIORITY respectively\n"); scanf("%d%d",&newnode->data,&newnode->priority); if(head==NULL) { head=newnode; newnode->next=NULL; } else if(temp->next==NULL) { if(temp->priority<=newnode->priority) { temp->next=newnode; newnode->next=NULL; } else { newnode->next=head; head=newnode; } } else { while(temp->next!=NULL) { if(temp->priority>newnode->priority) break; pretemp=temp; temp=temp->next; } if(temp->priority>newnode->priority && temp!=head) { newnode->next=temp; pretemp->next=newnode; } else if(temp->priority <= newnode->priority && temp->next == NULL) { temp->next=newnode; newnode->next=NULL; } else { newnode->next=head; head=newnode; } } } else printf("\nQueue Overflow !!!!!\n"); } void dequeue() { struct node * temp; temp=head; if(count!=0) { head=head->next; free(temp); count--; } else printf("\nQueue Underflow !!!!!\n"); } void display() { struct node *temp; temp=head; if(head==NULL) printf("\nList is empty...\n"); else { printf("List elements are :\t"); while(temp->next!=NULL) { printf("%d\t",temp->data); temp=temp->next; } printf("%d\t",temp->data); } }
|
|
Labels: Data Structure in C