PAT 單鏈表逆轉

2021-08-10 08:36:52 字數 1382 閱讀 7160

#include 

#include 

typedef

intelementtype;  

typedef

structnode *ptrtonode;  

structnode ;  

typedefptrtonode list;  

list read(); 

/* 細節在此不表 */

voidprint( list l ); 

/* 細節在此不表 */

list reverse( list l );  

intmain()  

list read()  

returnhead;      

}voidprint( list l )  

putchar(

'\n'

);  

}  list reverse( list l )  

returnl2;  

筆記:typedef struct node * ptrtonode 

struct node *  =   ptrtonode 

ptrtonode  list 相當於定義了乙個 struct node 型別的指標

1.對list read()解析

先要定義乙個結構變數   賦值為null;

開闢乙個結構空間 。

然後輸入資料長度,判斷是否為0   ,是則返回null

不是則繼續輸入資料,然後資料弄到結構裡面去,記得next為null;

那麼要判斷了如果有大於等於乙個元素,就要在list裡面繼續新增下去,怎麼做呢?

用迴圈來做啊

while(len)     在 裡面開闢乙個 ptrtonode 型別的變數 node  那麼這個變數就帶有  num 和 null

每次輸入乙個數 在把list 指向node 即可乙個鍊錶形成了;

ok,going on。

在重新寫乙個看看

2.對print函式進行解析

你要輸出  首先要看這個它有沒有空?沒有才可以輸出的。

if(l==null)  

3.reverse 函式  首先判斷空不  要寫  if

(l==null)?

不空在進行下面的步驟。

設定乙個臨時儲存的函式和乙個儲存逆反鍊錶的鍊錶。

現在coding

4.寫乙個完整的試一試;

逆轉單鏈表

逆轉單鏈表 struct node reverse llist node head 此時temp表示以前的尾結點,p表示temp結點的前一結點 head temp 逆轉尾結點為頭結點 head next p 頭結點指標域指向p return head 逆轉迴圈鍊錶 struct node rever...

單鏈表逆轉

單鏈表逆轉 單鏈表逆轉,將單鏈表中各結點的next域改為指向其前驅結點。原先第乙個結點的next域為空,head改為指向原先的最後乙個結點。逆轉剛才如下圖所示 鍊錶類singlylist public class singlylist 構造單鏈表,由values陣列提供元素 public singl...

單鏈表逆轉

資料結構與演算法題目集 中文 list reverse list l 其中list結構定義如下 typedef struct node ptrtonode struct node typedef ptrtonode list 定義單鏈表型別 l是給定單鏈表,函式reverse要返回被逆轉後的鍊錶。i...