6 1 單鏈表逆轉

2022-03-05 08:53:20 字數 1209 閱讀 9631

本題要求實現乙個函式,將給定的單鏈表逆轉。

list reverse( list l );
其中list結構定義如下:

typedef struct node *ptrtonode;

struct node ;

typedef ptrtonode list; /* 定義單鏈表型別 */

l是給定單鏈表,函式reverse要返回被逆轉後的鍊錶。

#include #include typedef int elementtype;

typedef struct node *ptrtonode;

struct node ;

typedef ptrtonode list;

list read(); /* 細節在此不表 */

void print( list l ); /* 細節在此不表 */

list reverse( list l );

int main()

/* 你的**將被嵌在這裡 */

5

1 3 4 5 2

1

2 5 4 3 1

參考:

ac code:list reverse( list l )

returnl;}

/***

a->b->c->d->d  l = null

↑p,q

a->b->c->d->d   a->null  

↑ ↑        ↑

q p       q,l

a->b->c->d->d   b->a->null  

↑        ↑ ↑

p,q       q l

a->b->c->d->d   b->a->null  

↑  ↑      ↑ ↑

q p      q l

a->b->c->d->d   b->a->null  

↑  ↑      ↑

q p     q,l

以此類推,最後將實現鍊錶的反轉。

***/

6 1單鏈表逆轉

資料結構與演算法題目集 在資料結構與演算法這門課程中終於與熟悉的鍊錶打了個照面,首先看到單鏈表逆轉這道題,腦海裡有閃現出幾種solutions solution 1 重新建立乙個鍊錶,以原鍊錶為輔助,給新鍊錶新增資料,再返回這個新鍊錶 solution2 不動節點位置,只改變資料域,即對稱swap資...

6 1 單鏈表逆轉

本題要求實現乙個函式,將給定的單鏈表逆轉。list reverse list l 其中list結構定義如下 typedef struct node ptrtonode struct node typedef ptrtonode list 定義單鏈表型別 l是給定單鏈表,函式reverse要返回被逆轉...

PTA6 1單鏈表逆轉

list reverse list l 其中list結構定義如下 typedef struct node ptrtonode struct node typedef ptrtonode list 定義單鏈表型別 l是給定單鏈表,函式reverse要返回被逆轉後的鍊錶。include include ...