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