Queue Operations using C Language
Write a program to Queue Operations using C Language to implement the following functions :
- Create Queue
- Delete Queue
- Display Queue
- Delete Queue
Queue Operations using C Language Code
#include #include #include typedef struct queue { char name[20]; char diesease[50]; struct add { int hno; int sno; char city[20]; }add; struct queue * link; }queue; typedef struct prior { queue * front; queue * rear; int priority; struct prior * next; }prior; prior *create(); prior *delet(prior *); void display(queue *,int *); void alloc(queue *); prior *freeall(prior *); void main() { int ch,ch1,temp; prior *t,*f=NULL; do { clrscr(); printf("\n\t1.CREATE\n\t2.DELETE\n\t3.DISPLAY\n\t4.EXIT\n\tENTER UR CHOICE:"); scanf("%d",&ch); switch(ch) { case 1: if(f!=NULL) f=freeall(f); printf("\nCREATE"); f=create(); break; case 2: if(f==NULL) printf("FIRST CREATE !!!!!"); else { printf("\nDELETE"); f=delet(f); } getch(); break; case 3: if(f==NULL) printf("FIRST CREATE !!!!!"); else { do { clrscr(); printf("\n\tDISPLAY\n\t1.BY CATEGORY\n\t2.ALL\n\t3.EXIT\n\tENTER UR CHOICE"); scanf("%d",&ch1); switch(ch1) { case 1: printf("\nENTER THE CATEGORY:\n1.SERIOUS\n2.NON-SERIOUS\n3.GENERAL"); scanf("%d",&ch); temp=1; t=f; clrscr(); while(t!=NULL) { if(t->priority==ch) break; t=t->next; } if(t!=NULL) display(t->front,&temp); else printf("NOT FOUND"); getch(); break; case 2: t=f; temp=1; clrscr(); while(t!=NULL) { display(t->front,&temp); t=t->next; } getch(); break; case 3: break; default:printf("\nINVALID CHOICE"); } }while(ch1!=3); } getch(); break; case 4: break; default:printf("\nINVALID CHOICE"); } }while(ch!=4); getch(); } prior * create() { int p; prior *f=NULL,*nw1,*t; queue *nw; do { nw=(queue *)malloc(sizeof(queue)); nw->link=NULL; alloc(nw); printf("\nGIVE THE PRIORITY TO THIS PATIENT:"); while(1) { scanf("%d",&p); if(p>0&&p<4) break; else printf("INVALID PRIORITY"); } if(f==NULL) { nw1=(prior *)malloc(sizeof(prior)); nw1->next=NULL; nw1->priority=p; nw1->front=nw; nw1->rear=nw; f=nw1; } else { if(f->priority>p) { nw1=(prior *)malloc(sizeof(prior)); nw1->next=NULL; nw1->priority=p; nw1->front=nw; nw1->rear=nw; nw1->next=f; f=nw1; } t=f; while(p>=t->next->priority&&t->next!=NULL) t=t->next; if(p==t->priority) { t->rear->link=nw; t->rear=nw; } else { nw1=(prior *)malloc(sizeof(prior)); nw1->next=NULL; nw1->priority=p; nw1->front=nw; nw1->rear=nw; nw1->next=t->next; t->next=nw1; } } printf("DO U WANT TO ENTER MORE:"); }while(getche()=='y'||getche()=='Y'); return f; } void alloc(queue *nw) { printf("\nENTER NAME:"); flushall(); gets(nw->name); printf("\nENTER DIESEASE:"); flushall(); gets(nw->diesease); printf("\nENTER H.NO:"); scanf("%d",&(nw->add.hno)); printf("\nENTER S.NO:"); scanf("%d",&(nw->add.sno)); printf("\nENTER CITY:"); flushall(); gets(nw->add.city); } void display(queue *t,int *temp) { while(t!=NULL) { printf("\n\nNAME:"); printf("%s",t->name); printf("\nDIESEASE:"); printf("%s",t->diesease); printf("\nADDRESS"); printf("\nHOUSE.NO:"); printf("%d",(t->add.hno)); printf("\nSTREET.NO:"); printf("%d",(t->add.sno)); printf("\nCITY:"); printf("%s",(t->add.city)); t=t->link; getch(); } } prior *delet(prior *f) { prior *nw; queue *q,*q1; q=f->front; if(q->link==NULL) { nw=f; f=f->next; free(nw); } else f->front=q->link; free(q); printf("\nDATA is DELETED!!!!!!"); getch(); return f; } prior *freeall(prior *f) { prior *t; queue *s,*r; while(f!=NULL) { r=f->front; while(r!=NULL) { s=r; r=r->link; free(s); } t=f; f=f->next; free(t); } return f; }
Leave a Reply