You are on page 1of 2

//Priority Queue.

#include<conio.h> #include<iostream.h> struct node { int data; int pri; struct node *next; }; void create(); void display(); struct node *front=NULL; int main() { int n; cout<<"how many nodes you want to create"; cin>>n; for(int i=0;i<n;i++) {create(); } display(); getch(); return 0; } void create() { struct node *temp,*ptr,*ptr1; int num,p,flag=0; cout<<"Enter the Number:"<<endl; cin>>num; cout<<"Enter the periority :"<<endl; cin>>p; temp=new node; temp->data=num; temp->pri=p; temp->next=NULL; if(front==NULL) {

front=temp; } else if(front->pri>temp->pri) { temp->next=front; front=temp; } else { ptr=front; ptr1=front; while((ptr->pri <= temp->pri) && ptr->next!=NULL) { ptr1=ptr; ptr=ptr->next; } if(ptr->pri <= temp->pri && ptr->next==NULL) { ptr->next=temp; } else { ptr1->next=temp; temp->next=ptr; } } }

void display() { struct node *ptr; ptr=front; while(ptr!=NULL) { cout<<ptr->data<<"->"; ptr=ptr->next; } }

You might also like