Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Ignore \"Because this is your first big-time C data structure implementation...\

ID: 3641837 • Letter: I

Question

Ignore "Because this is your first big-time C data structure implementation..." Answer the First part of the question.

Explanation / Answer

#include #include // The structure clubs "data" and "next" pointer struct queue{ char data; struct queue *next; }; // typedef is used for user defined type node typedef struct queue node; // Another user defined type nodeptr is pointer to node type typedef node * nodeptr; // All functions pass nodeptr types by reference as these are modified during opeerations in functions void enqueue(nodeptr*,nodeptr*); // Inserting character in queue void dequeue(nodeptr*,nodeptr*); //Deleting character in queue void init(nodeptr *,nodeptr *); // initialize queue // display does nor reflect any change in main, only prints void display(nodeptr); // Output display //====================================================================== void main() { int opt; char ch; nodeptr front; /* front pointer in queue*/ nodeptr rear; /* rear pointer in queue */ // -------------------------------------------------------------------- // The assignments rear=front=NULL are done in init init(&front,&rear); do { printf(" Enter your option to perform in a queue "); printf(" 1. Insert "); printf(" 2. delete "); printf(" 3. display the list "); // User chooses options 1 2 3 scanf("%d",&opt); switch(opt) { case 1: enqueue(&front,&rear); break; case 2: dequeue(&front,&rear); break; case 3: display(front); break; } // User decides to quit or continue printf(" Do you wish to continue[y/n] "); scanf(" %c",&ch); }while(ch=='Y' || ch=='y'); printf(" Press any key to exit "); } //================================================================ void init(nodeptr *front,nodeptr *rear) { *(rear)=*(front)=NULL; } //================================================================ void enqueue(nodeptr *front,nodeptr *rear) { node * newnode; newnode=(node*)malloc(sizeof(node)); newnode->next=NULL; printf(" Enter the character to push "); fflush(stdin); scanf(" %c",&(newnode->data)); // Two cases arise, inserting into (i) empty queue (ii) non-empty queue if(*(front)==NULL && *(rear)==NULL) { // For empty queue front and rear are same after inertion *(front)=newnode; *(rear)=newnode; } else { // For non-empty queue, newnode is added at the end (rear), and // finally rear is made to point to the end of the list, that is, newnode (*(rear))->next=newnode; (*(rear))=(*(rear))->next; } } //=============================================================== void dequeue(nodeptr *front,nodeptr *rear) { node *delnode; /* Node to be deleted */ // No deletion is possible if the list is already empty if(*(front)==NULL && *(rear)==NULL) printf(" Queue is empty to delete any element "); else { // If non-empty, two cases arise (i) list has one node (ii) list has more than one node if (*(front)==*(rear)) { // If list has one node then both front and rear become NULL after deletion delnode=*(front); free(delnode); *(front)=NULL; *(rear)=NULL;} // Deleted node is also freed else {// If there are two or mode nodes then simply front is moved forward to next node and // first node is freed delnode=*(front); (*(front))=(*(front))->next; free(delnode);} } } //================================================================ void display(nodeptr f) { // Simply print the data pointed to by node pointer while(f!=NULL) { printf(" %c ",f->data); f=f->next; } } //==================================================================