Double Linked List

Make a middle node of doubly link list to the top of the list.

Questions by shivi10j

Showing Answers 1 - 3 of 3 Answers

ravi6771

  • Aug 12th, 2009
 

let suppose x is the node to be moved to top


x->next->prev=x->prev;
x->prev->next=x->prev->next->next;
x->next=head;
head->prev=x;
head=x;

  Was this answer useful?  Yes

f2003062

  • Aug 19th, 2009
 

Lets say if the middle node of the doubly linked list is "middle" and the start node is "head".

then the code as follows.

middle->prev->next = middle->next;
middle->next->prev = middle->prev;

// Now make the middle as head.
middle->prev = NULL;
middle->next = head;
head->prev = middle;
head = middle;


kronos

  • Nov 16th, 2009
 

node *n1,*n2;
n1 = n2 = head;
while((n2->next) && (n2->next->next))
{
     n1 = n1->next;
     n2 = n2->next->next;
]

// n1 points to the middle now

n1->prev->next = n1->next;
n1->next->prev = n1->prev;
n1 ->prev = head ->prev; //NULL generally, non NULL value if circular list.
n1 ->next = head;
head = n1;

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions