Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
2.4k views
in Computer by (69.2k points)

Write an algorithm to insert an element k in double linked list at

(i) Start of linked list 

(ii) After a given position P of list 

(iii) End of linked list.

1 Answer

+1 vote
by (69.8k points)
selected by
 
Best answer

Algorithm to insert an element k in double linked list at a position which is:- 

(i) Start of a linked list 

while inserting at the start of the linked list we have to change the root node and so we need to return the new root to the calling program.

nodeptr insertatstart ( nodeptr root, nodeptr k)

{

k->next = root;

k->back = root->back;

root->back->next = k;

root->back=k; 

return k;

}

(ii) After a given position p of list 

Here we assume that p points to that node in the doubly linked list after which insertion is required. 

insertafter ( nodeptr p , nodeptr k ) 

{

if (p == null)

{

printf (“void insertion /

return;

}

k->back = p; 

k->next = p->next; 

p->next->back = 

k; p->next = k; 

return;

  }

(iii) End of linked list

Here k is the element to be inserted and root is the pointer to the first node of the doubly linked list.

insertatend ( nodeptr root, nodeptr k )

{

nodeptr trav;

while (trav->next != root)

trav = trav->next;

k->next=root;

root->back=k;

trav->next = k;

k->back=trav;

return;

}

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...