In: Computer Science
Question two
Answer:
I am giving the code to entire implementation of double linked list.
Question One:
code:
#include <stdio.h> #include <stdlib.h> struct node{ struct node *prev; int data; struct node *next; }; struct node *start = NULL; void display_main_choices(void); void display_insertion_choices(void); void display_deletion_choices(void); void insertion_beginning(void); void insertion_end(void); void insertion_after_given_node(void); void insertion_before_given_node(void); void deletion_beginning(void); void deletion_end(void); void deletion_after_given_node(void); void deletion_before_given_node(void); void display_elements(void); void main(){ int main_choice, insertion_choice, deletion_choice; printf("Welcome to the world of programming .\n"); display_main_choices() ; scanf("%d",&main_choice); while(main_choice != -1){ switch(main_choice){ case 1: display_insertion_choices(); scanf("%d",&insertion_choice); while(insertion_choice != -1){ switch(insertion_choice){ case 1: insertion_beginning(); //printf("Insertion at beginning\n"); break; case 2: insertion_end(); //printf("Insertion at end\n"); break; case 3: insertion_after_given_node(); //printf("Insertion after given node\n"); break; case 4: insertion_before_given_node(); //printf("Insertion before given node\n"); break; default: printf("You have choosen wrong choice. Please choose again.\n"); break; } display_insertion_choices(); scanf("%d",&insertion_choice); } break; case 2: display_deletion_choices(); scanf("%d",&deletion_choice); while(deletion_choice != -1){ switch(deletion_choice){ case 1: deletion_beginning(); //printf("Deletion at beginning\n"); break; case 2: deletion_end(); //printf("Deletion at end\n"); break; case 3: deletion_after_given_node(); //printf(("Deletion after given node\n")); break; case 4: deletion_before_given_node(); //printf("Deletion before given node\n"); break; default: printf("You have choosen wrong choice. Please choose again.\n"); break; } display_deletion_choices(); scanf("%d",&deletion_choice); } break; case 3: display_elements(); break; default: printf("You have chosen wrong choice. Please choose again.\n"); break; } display_main_choices(); scanf("%d",&main_choice); } } void display_main_choices(void){ printf("******************* Main Operations ****************\n"); printf(" 1 - Insertion\n"); printf(" 2 - Deletion\n"); printf(" 3 - Display\n"); printf(" Note : Enter -1 to QUIT\n"); printf("Please choose one option : "); } void display_insertion_choices(void){ printf("****************** Insertion Choices ***************\n"); printf(" 1 - Insertion at beginnning\n"); printf(" 2 - Insertion at end\n"); printf(" 3 - Insertion after a given node\n"); printf(" 4 - Insertion before a given node\n"); printf(" NOTE : Enter -1 to go to main menu\n"); printf("Please choose one option : "); } void display_deletion_choices(void){ printf("**************** Deletion Choices **************\n"); printf(" 1 - Deeltion at beginning\n"); printf(" 2 - Deletion at end\n"); printf(" 3 - Deletion after a given node\n"); printf(" 4 - Deletion before a given node\n"); printf(" NOTE : Enter -1 to go to main menu\n"); printf("Please choose one option : "); } void insertion_beginning(void){ struct node *newnode, *tmp; tmp = start; if(start == NULL){ newnode = (struct node *)malloc(sizeof(struct node)); printf("Enter the data for the newnode : "); scanf("%d",&newnode->data); newnode->next = NULL; newnode->prev = NULL; start = newnode; }else{ newnode = (struct node *)malloc(sizeof(struct node)); printf("Enter the data for the newnode : "); scanf("%d",&newnode->data); newnode->prev = NULL; newnode->next = start; start->prev = newnode; start = newnode; } } void insertion_end(void){ struct node *newnode, *tmp; tmp = start; if(start == NULL){ insertion_beginning(); } else{ while(tmp->next){ tmp = tmp->next; } newnode = (struct node *)malloc(sizeof(struct node)); printf("Enter the data for the new node : "); scanf("%d",&newnode->data); newnode->next = NULL; newnode->prev = tmp; tmp->next = newnode; } } void insertion_after_given_node(void){ struct node *newnode, *tmp, *tmp2; int node, found = 1; tmp = start; if(start == NULL){ printf("Linked List is empty till now. Please choose insertion at beginning\n"); } else{ printf("Enter the node value : "); scanf("%d",&node); if(start->next == NULL){ if(start->data == node) insertion_end(); else printf("Wrong node entered. Please choose again.\n"); } else{ while(tmp->data != node){ tmp = tmp->next; if(tmp == NULL){ found = 0; break; } } if(found == 1){ newnode = (struct node *)malloc(sizeof(struct node)); printf("Enter the data for the newnode : "); scanf("%d",&newnode->data); tmp2 = tmp->next; tmp->next = newnode; newnode->prev = tmp; newnode->next = tmp2; }else{ printf("Node is not present in the existed linked list. Please choose again\n"); } } } } void insertion_before_given_node(void){ struct node *newnode, *tmp, *tmp2; int node, found = 1; tmp = start; if(start == NULL){ printf("Linked List is empty till now. Please choose again\n"); } else{ printf("Enter the node value : "); scanf("%d",&node); if(start->next == NULL){ if(start->data == node){ insertion_beginning(); } else{ printf("Wrong node entered. Please choose again\n"); } } else if(start->data == node){ insertion_beginning(); } else{ while(tmp->next->data != node){ tmp = tmp->next; if(tmp->next == NULL){ found = 0; break; } } if(found == 1){ tmp2 = tmp->next; newnode = (struct node *)malloc(sizeof(struct node)); printf("Enter the data for the new node : "); scanf("%d",&newnode->data); tmp->next = newnode; newnode->prev = tmp; newnode->next = tmp2; } else{ printf("Entered node value is not in the linked list. Please chooose again\n"); } } } } void deletion_beginning(void){ struct node *tmp; if(start == NULL){ printf("Linked List is empty. Deletion is not possible.\n"); } else{ tmp = start->next; free(start); start = tmp; if(start != NULL){ start->prev = NULL; } } } void deletion_end(void){ struct node * tmp; tmp = start; if(start == NULL){ printf("Linked List is empty. Deletion is not possible.\n"); } else{ if(start->next == NULL){ free(start); start = NULL; } else{ while(tmp->next->next ){ tmp = tmp->next; } free(tmp->next); tmp->next = NULL; } } } void deletion_after_given_node(void){ struct node *tmp, *tmp2; tmp = start; int node, found=1; if(start == NULL){ printf("Sorry ! Linked List is empty. Please choose again.\n"); } else{ printf("Enter the node value : "); scanf("%d",&node); if(start->next == NULL){ if(start->data == node){ printf("Sorry ! There is no node after the entered node value.\n"); } else{ printf("Sorry ! Node value is not found.\n"); } } else{ while(tmp->data != node){ tmp = tmp->next; if(tmp == NULL){ found = 0; break; } } if(found == 1){ if(tmp->next == NULL){ printf("Sorry ! There is no node after the entered node value.\n"); }else{ if(tmp->next->next == NULL){ deletion_end(); }else{ tmp2 = tmp->next; tmp->next = tmp->next->next; tmp->next->next->prev = tmp; free(tmp2); } } }else{ printf("Sorry ! Entered node value is not found in the existed linked list.\n"); } } } } void deletion_before_given_node(void){ struct node *tmp, *tmp2; int node,found = 1; tmp = start; if(start == NULL){ printf("Sorry ! Linked List is empty. Deletion is not possible.\n"); }else{ printf("Enter the node value : "); scanf("%d",&node); if(start->data == node){ printf("Sorry ! There is no node before the entered node value.\n"); } else if(start->next->next == NULL){ if(start->next->data == node) deletion_beginning(); else printf("Node value is not present.\n"); } else if(start->next->data == node){ deletion_beginning(); }else{ while(tmp->next->next->data != node){ tmp = tmp->next; if(tmp->next->next == NULL){ found = 0; break; } } if(found == 1){ tmp2 = tmp->next->next; free(tmp->next); tmp->next = tmp2; tmp2->prev = tmp; }else{ printf("Sorry ! There is no node value that you have entered.\n"); } } } } void display_elements(void){ struct node *tmp; tmp = start; printf("******************** Elements in the linked list *****************\n"); while(tmp){ printf("%d\n",tmp->data); tmp = tmp->next; } }
QUESTION TWO:
Initially queue is 30->25->5->15->10
(i) After deleting two elements, queue is 5->15->10. This is because as queue follows first in first out principle.
(ii) After inserting 7 and then 20, queue is 5->15->20->7->20;
(iii) After deleting one element, queue is 15->20->7->20;