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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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):

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education