GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Concepts  >  Data Structures
Go To First  |  Previous Question  |  Next Question 
 Data Structures  |  Question 201 of 202    Print  
Double Linked List
Make a middle node of doubly link list to the top of the list.


  
Total Answers and Comments: 3 Last Update: November 17, 2009     Asked by: shivi10j 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
August 12, 2009 08:58:39   #1  
ravi6771 Member Since: August 2009   Contribution: 3    

RE: Double Linked List
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;

 
Is this answer useful? Yes | No
August 19, 2009 03:37:53   #2  
f2003062 Member Since: October 2006   Contribution: 7    

RE: Double Linked List
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;



 
Is this answer useful? Yes | No
November 16, 2009 19:20:17   #3  
kronos Member Since: November 2009   Contribution: 1    

RE: Double Linked List
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;

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape