鍊錶 逆序 列印

2021-06-18 09:37:18 字數 960 閱讀 3065

設煉表節點為

要求將一帶煉表頭list head的單向鍊錶逆序。

分析:1). 若煉表為空或只有乙個元素,則直接返回;

2). 設定兩個前後相鄰的指標p,q. 將p所指向的節點作為q指向節點的後繼;

3). 重複2),直到q為空

4). 調整煉表頭和鍊錶尾

示例:以逆序a->b->c->d為例,圖示如下

實現及測試**如下:

#include

#include

typedef

struct taglistnodelistnode, *list; 

void printlist(list head); 

list reverselist(list head); 

int main() 

printlist(head);           /*輸出原始鍊錶*/

head = reverselist(head);  /*逆序鍊錶*/

printlist(head);           /*輸出逆序後的鍊錶*/

return 0; 

}  list reverselist(list head) 

listnode *t = null, 

*p = head->next, 

*q = head->next->next; 

while(q != null) 

/*此時q指向原始鍊錶最後乙個元素,也是逆轉後的鍊錶的表頭元素*/

head->next->next = null;  /*設定鍊錶尾*/

head->next = p;           /*調整煉表頭*/

return head; 

}  void printlist(list head) 

printf("/n"); 

}  

(鍊錶構建)鍊錶逆序

一直煉表頭結點指標head,將鍊錶逆序。不可申請額外空間 include using namespace std struct listnode int main 實際最終執行的 include using namespace std struct listnode 這個建構函式有點沒看懂,是一種什...

鍊錶 逆序 例子

typedef struct node node node createlink int n return pheaer node reverselink node pher pheader pher pcurrentnode pheader pnext while pcurrentnode pne...

逆序鍊錶輸出

題目描述 將輸入的乙個單向鍊錶,逆序後輸出鍊錶中的值。輸入整數鍊錶 輸出整數鍊錶 樣例輸入1,2 3,4 5樣例輸出5,4 3,2 1 提示 注意鍊錶指標的處理,防止空指標,注意鍊錶長度 輸入部分參考 include func.h include include 請按照要求實現下列函式 請完成如下函...