| Re: Linked list ..help Program to illustrate queue using linked list concept: /*Program to illustrate queue using linked list concept*/ #include /*Link section*/ struct node /*Structure used to represent elements as nodes*/ { int val; struct node *next; }*rear = NULL,*front = NULL,*temp = NULL; void enqueue(int); /*Function declaration section*/ int dequeue(); void disp(); /*Main Function starts here*/ void main() { int opt,item,dele; /*Loop which processes infinitely and ends when forced by the user*/ do { clrscr(); printf("Enter the required option:\n1.Enqueue\n2.Dequeue\n3.View the queue\n4.Exit:"); scanf("%d",&opt); /*Required option is received here*/ switch(opt) { case 1: printf("Enter the value to enqueue:"); scanf("%d",&item); enqueue(item); /*Element added to the queue */ break; case 2: /*Element removed from the queue pointed by the front pointer*/ dele = dequeue(); if(dele == 0) { disp(); continue; } getch(); break; case 3: disp(); /*Displays the queue when called upon*/ break; } }while(opt != 4); /*Checks for the entered option*/ } void enqueue(int item) { if(rear == NULL) { /*Reserving memory space using malloc*/ rear = (struct node *)malloc(sizeof(struct node)); /*Assigning the first value to the rear pointer*/ rear->val = item; /*Assigning the next of rear pointer to NULL*/ rear->next = NULL; /*Assigning the front pointer to the rear pointer*/ front = rear; } else { /*Reserving memory space using malloc*/ rear->next = (struct node *)malloc(sizeof(struct node)); /*Assigning the value of the next element of rear pointer*/ rear->next->val = item; /*Assigning the next of next element of rear pointer to NULL*/ rear->next->next = NULL; /*Moving rear by one node*/ rear = rear->next; } } /*Function used to dequeue an element*/ int dequeue() { int dequeued; /*Variable used to store the dequeued element*/ if(front == NULL) /*Checking for the queue to be NULL*/ { printf("\nQueue is empty\n"); /*Printing the queue is empty*/ return; } dequeued = front->val; /*Assigning the dequeued value to a variable*/ /*Printing the dequeued element*/ printf("The dequeued item is %d",front->val); temp = front; /*Temp initialized to front*/ front = front->next; /*Element is deleted by moving the front pointer*/ free(temp); /*Temp is made free*/ if(front == NULL) /*Checking for the queue to be NULL*/ rear = NULL; return dequeued; /*Dequeued value is returned here*/ } /*Function to display the queue elements*/ void disp() { if(front == NULL) /*Checking for the queue to be NULL*/ { printf("The queue is empty"); getch(); return; } temp = front; /*Temp initialized to front pointer*/ printf("\nQueue elements are:\n"); /*Loop to display the queue elements*/ while(temp != NULL) { printf("%d\n",temp->val); temp = temp->next; /*Temp is passed to the next node by pointers*/ } getch(); } Output: Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:3 The queue is empty Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:1 Enter the value to enqueue:15 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:1 Enter the value to enqueue:69 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:3 Queue elements are: 15 69 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:1 Enter the value to enqueue:39 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:1 Enter the value to enqueue:25 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:1 Enter the value to enqueue:30 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:3 Queue elements are: 15 69 39 25 30 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:2 The dequeued item is 15 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:2 The dequeued item is 69 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:3 Queue elements are: 39 25 30 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:2 The dequeued item is 39 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:2 The dequeued item is 25 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:2 The dequeued item is 30 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:2 Queue is empty Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:4
------- |