Write a program to insert a new member in a linked list. Solution #include #incl
ID: 3660790 • Letter: W
Question
Write a program to insert a new member in a linked list.Explanation / Answer
#include #include #include #include struct node { int num; struct node *next; struct node *prev; }; /* declaring a global node of type struct */ typedef struct node NODE; /* providing a type definition to the above created structure */ NODE *head=NULL; /* declaring some of the global variable that would be used throughout the program */ NODE *temp, *first, last; int info; void display(); void insert_at_end(); void insert_at_begin(); void insert_at_specifiedpos(); void main() /* starting the main method() */ { int i; clrscr(); printf(" program for insertion in a doubly linked list : "); do { printf(" Enter your choice : "); printf(" 1.Insert element at the end of the linkedlist :"); printf(" 2.Insert element at the begin of the linkedlist :"); printf(" 3.Insert at any specified position in the linkedlist :"); printf(" 4.Exit "); fflush(stdin); scanf(" %d",&i); switch(i) { case 1: insert_at_end(); display(); break; case 2: insert_at_begin(); display(); break; case 3: insert_at_specifiedpos(); display(); break; case 4: exit(0); } }while(inum=info; temp->next=NULL; temp->prev=NULL; if(head==NULL) /* checking whether list is empty */ { head=temp; first=temp; last=temp; } else { first=head; while(first->next!=NULL) { first=first->next; } first->next=temp; temp->prev=first; temp->next=NULL; last=temp; } } void display() { first=head; printf(" Status of the doubly linked list is as follows : "); while(first->next!=NULL) /* traversing the linked list */ { printf(" %d",first->num); first=first->next; } } void insert_at_begin() { printf(" Enter the value which do you want to insert at begining "); scanf(" %d",&info); temp=(NODE *)malloc(sizeof(NODE)); temp->num=info; temp->next=NULL; temp->prev=NULL; if(head==NULL) { head=temp; last=temp; } else { temp->next=head; head->prev=temp; temp->prev=NULL; head=temp; } } void insert_at_specifiedpos() { NODE *second; int node_num,info; int i=1; printf(" Enter the nodenumber after which you want to insert new node "); scanf("%d",&node_num); printf(" Enter the value of new node "); scanf(" %d",&info); temp=(NODE *)malloc(sizeof(NODE)); temp->num=info; temp->next=NULL; temp->prev=NULL; first=head; while(inext; } temp->next=first; temp->prev=second; first->prev=temp; second->next=temp; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.