Write a program in C to implement the linked list and perform insertion and deletion of a node from the linked list from the start, end, and intermediate positions of the list. The program should take care of the boundary cases. #include #include typedef struct node { int data; struct node *link; }nod; nod *insertion_function(nod *head, int *n, int x, int posflag) { /*input: head node, x:element, pos: -1 indicates start, n last of the node*/ /*output: length of the list after insertion*/ nod *temp_node=(nod*)malloc(sizeof(nod)); nod *current=NULL; temp_node->data=x; if(head==NULL) { temp_node->link=NULL; head=temp_node; } if(posflag==-1) { temp_node->link=head; head=temp_node; } else if(posflag==*n) { temp_node->link=NULL; current=head; while(current->link!=NULL) current=current->link; current->link=temp_node; } *n=*n+1; return head; } void printfunc(nod *head) { nod *current=NULL; current=head; while(current!=NULL) { printf("%d->", current->data); current=current->link; } printf("null"); } int main() { int x, n=0, posflag; char ch='Y'; nod *head=NULL; while(ch=='Y') { scanf("%d", &x); scanf("%d", &posflag); head=insertion_function(head, &n, x, posflag); printfunc(head); printf("\t\t%d\n",n); ch=getch(stdin); } return 0; } The above are the test cases and given below is the sample output (Note: The program should pass all the test cases) Enter the choice: 1. Insertion 2. Deletion Enter the element for deletion from list: The list now is: Empty!! Do you want to continue(Y/N): Enter the choice: 1. Insertion 2. Deletion Enter the element and position of insertion into list: position is ignored as the list is empty! the list now is: 4-> Do you want to continue(Y/N): Enter the choice: 1. Insertion 2. Deletion Enter the element and position of insertion into list: The list now is: 2->4-> Do you want to continue(Y/N): Enter the choice: 1. Insertion 2. Deletion Enter the element and position of insertion into list: The list now is: 2->4->6-> Do you want to continue(Y/N): Enter the choice: 1. Insertion 2. Deletion Enter the element and position of insertion into list: The list now is: 2->4->3->6-> Do you want to continue(Y/N): Enter the choice: 1. Insertion 2. Deletion Enter the element for deletion from list: The list now is: 2->3->6-> Do you want to continue(Y/N): Enter the choice: 1. Insertion 2. Deletion Enter the element for deletion from list: The list doesnot contains the element 4 The list now is: 2->3->6-> Do you want to continue(Y/N): Enter the choice: 1. Insertion 2. Deletion Enter the element for deletion from list: The list is shorter than specified position! entering at the end of list The list now is: 2->3->6->9 Do you want to continue(Y/N
Write a program in C to implement the linked list and perform insertion and deletion of a node from the linked list from the start, end, and intermediate positions of the list. The program should take care of the boundary cases.
#include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *link; }nod; nod *insertion_function(nod *head, int *n, int x, int posflag) { /*input: head node, x:element, pos: -1 indicates start, n last of the node*/ /*output: length of the list after insertion*/ nod *temp_node=(nod*)malloc(sizeof(nod)); nod *current=NULL; temp_node->data=x; if(head==NULL) { temp_node->link=NULL; head=temp_node; } if(posflag==-1) { temp_node->link=head; head=temp_node; } else if(posflag==*n) { temp_node->link=NULL; current=head; while(current->link!=NULL) current=current->link; current->link=temp_node; } *n=*n+1; return head; } void printfunc(nod *head) { nod *current=NULL; current=head; while(current!=NULL) { printf("%d->", current->data); current=current->link; } printf("null"); } int main() { int x, n=0, posflag; char ch='Y'; nod *head=NULL; while(ch=='Y') { scanf("%d", &x); scanf("%d", &posflag); head=insertion_function(head, &n, x, posflag); printfunc(head); printf("\t\t%d\n",n); ch=getch(stdin); } return 0; }
The above are the test cases and given below is the sample output
(Note: The program should pass all the test cases)
Enter the choice: 1. Insertion 2. Deletion Enter the element for deletion from list: The list now is: Empty!! Do you want to continue(Y/N): Enter the choice: 1. Insertion 2. Deletion Enter the element and position of insertion into list: position is ignored as the list is empty! the list now is: 4-> Do you want to continue(Y/N): Enter the choice: 1. Insertion 2. Deletion Enter the element and position of insertion into list: The list now is: 2->4-> Do you want to continue(Y/N): Enter the choice: 1. Insertion 2. Deletion Enter the element and position of insertion into list: The list now is: 2->4->6-> Do you want to continue(Y/N): Enter the choice: 1. Insertion 2. Deletion Enter the element and position of insertion into list: The list now is: 2->4->3->6-> Do you want to continue(Y/N): Enter the choice: 1. Insertion 2. Deletion Enter the element for deletion from list: The list now is: 2->3->6-> Do you want to continue(Y/N): Enter the choice: 1. Insertion 2. Deletion Enter the element for deletion from list: The list doesnot contains the element 4 The list now is: 2->3->6-> Do you want to continue(Y/N): Enter the choice: 1. Insertion 2. Deletion Enter the element for deletion from list: The list is shorter than specified position! entering at the end of list The list now is: 2->3->6->9 Do you want to continue(Y/N):
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 3 images