top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to insert an element in the middle of doubly LinkList?

+3 votes
540 views
How to insert an element in the middle of doubly LinkList?
posted Mar 9, 2016 by Aditi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

1) Find Middle Element. Start from the head and take two pointer move one pointer only by one node and another pointer by two nodes in each iteration. Run the loop till end of LinkList. Pointer 1 will be the middle of the linklist.
2) Now Step 1 will give you the middle of the linklist. Just allocate the node copy the value in the node say it newnode then
newnode->next=pointer1->next;
pointer1>next->prev = newnode;
pointer1->next=newnode;
newnode->prev=pointer1;

I leave the implementation to you as it is very simple exercise.

answer Mar 10, 2016 by Salil Agrawal
–1 vote

newNode->PREV = curPtr

newNode->NEXT = curPtr->NEXT

newNode->NEXT->PREV = newNode

curPtr->NEXT = newNode

increment(HEAD->LENGTH)

answer Mar 10, 2016 by Ashish Kumar Khanna
First you need to findout the middle element, then only you can add element. Let me try out this..
Similar Questions
+1 vote

Say you are given a linklist as {a, b, a, b, a} , you need to divides up its nodes to make two smaller lists. The sublists should be made from alternating elements in the original list. So in this case one sublist should be {a, a, a} and the other should be {b, b}.

...