program to implement a queue using a link list. asap Solution #include #include
ID: 3634149 • Letter: P
Question
program to implement a queue using a link list. asapExplanation / Answer
#include #include #include /***** Structure template *****/ struct list{ char data; struct list *next; }; /***** Redefining struct list as node *****/ typedef struct list node; void qinsert(node**,node**); /** Inserting character function in queue **/ void qdelete(node**,node**); /** Deleting character function in queue **/ void disp(node*); /** Output displaying function **/ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |*"front" and "rear" pointer needed to be changed when inserting and *| |* deleting so addresses of them is passed in qinsert and qdelete *| * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void main() { int opt; /* Option inputing variable */ char ch; /* choice inputing variable */ node *front; /* front pointer in queue*/ node *rear; /* rear pointer in queue */ rear=front=NULL; do { printf(" Enter your option to perform in a queue "); printf(" 1. Insert "); printf(" 2. delete "); printf(" 3. Display the list "); scanf("%d",&opt); switch(opt) { case 1: qinsert(&front,&rear); break; case 2: qdelete(&front,&rear); break; case 3: disp(front); break; } printf(" Do you wish to continue[y/n] "); ch=(char)getche(); }while(ch=='Y' || ch=='y'); printf(" Done by "SHABBIR" "); printf(" Press any key to exit "); getch(); } void qinsert(node **front,node **rear) { node *newnode; /* New node to be inserted */ newnode=(node*)malloc(sizeof(node)); newnode->next=NULL; printf(" Enter the character to push "); fflush(stdin); scanf("%c",&(newnode->data)); if(*front==NULL && *rear==NULL) { *front=newnode; *rear=newnode; } else { /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |* "rear" points to last node whose next field must be pointed to *| |* newnode and then rear points to the latest node i.e. new node *| * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ (*rear)->next=newnode; *rear=newnode; } } void qdelete(node **front,node **rear) { node *delnode; /* Node to be deleted */ if((*front)==NULL && (*rear)==NULL) printf(" Queue is empty to delete any element "); else { delnode=*front; (*front)=(*front)->next; free(delnode); } } void disp(node *f) { while(f!=NULL) { printf(" %c ",f->data); f=f->next; } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.