2014-02-11 86 views
0

如何從頭中刪除節點,並將其添加到雙鏈表的末尾?移動元素從頭到尾雙向鏈表

我只有一個雙向鏈表(比如說有5個節點),我想刪除第一個節點並將其添加到最後。

前:

1->2->3->4->5 

預期結果:

2->3->4->5->1 

回答

1

剛剛修改的頭部和尾部指針:

temp = head; 
head = head->next; 
head->prev = NULL; 

tail->next = temp; 
temp->prev = tail; 
tail = tail->next; 
tail->next = NULL; 
1

維持兩個變量(頭&尾)分別指向雙向鏈表的開頭和結尾。

temp = head 
head = head.next 
head.prev = null 

tail.next = temp 
temp.prev = tail 
tail = tail.next 
tail.next = null 
+0

什麼tail.prev? –

+0

@RikayanBandyopadhyay謝謝..我糾正了這一點。 +1爲您的答案 – naresh