單鏈表反轉 C語言

2021-09-29 23:04:18 字數 2032 閱讀 3428

題目:

時間複雜度為o(n)空間複雜度為o(1)

**:

/*1-6*/

#include

#include

#define elemtype int

int num=0;

typedef

struct node

node;

typedef node* linklist;

void

initlist

(linklist *l)

void

createfromtail

(linklist l)

r->next=

null;}

void

createfromhead

(linklist l)

}void

inslist

(linklist l,

int i,elemtype e)

if(pre==

null

) s=

(node*

)malloc

(sizeof

(node));

s->data=e;

s->next=pre->next;

pre->next=s;

}void

dellist

(linklist l,

int i)

if(pre->next==

null

) r=pre->next;

pre->next=r->next;

free

(r);

printf

("已釋放此結點!\n");

}void

change

(linklist l,

int l,

int r);if

(r>num||r<1)

; node *p,

*q,*new;

p=(node*

)malloc

(sizeof

(node));

q=(node*

)malloc

(sizeof

(node));

new=

(node*

)malloc

(sizeof

(node));

p=l;

int i=1;

for(i=

1;i<=r;i++)}

q->data=p->data;

p->data=new->data;

}void

view

(linklist l)

printf

("\n");

}int

main()

else

view

(l);

printf

("您想反轉哪兩個結點:\n");

int left,right;

scanf

("%d%d"

,&left,

&right)

;change

(l,left,right)

;view

(l);

printf

("hello world!\n");

return0;

}

運**況:

請您輸入--元素的總個數:

5若使用頭插法請輸入--

'h'若使用尾插法請輸入--『t』

t請您輸入--元素:12

345此煉表為:12

345 您想反轉哪兩個結點:13

此煉表為:32

145 hello world!

program ended with exit code:

0

單鏈表反轉(C語言)

思路 思路 遞迴法 list reverse recursive list head else return head include include struct list list init list list head return head next void print list list...

c語言單鏈表 單鏈表反轉(C語言)

思路 單鏈表反轉函式 三指標法 a,b,c三個指標分別指向三個位置,a指標和b指標用來反轉兩個結點,c指標指向下乙個節點用來標記和向前推進。list reverse threepoints1 list head return a 三指標法優化 說是優化,其實是一樣的思路,只不過,這裡的head作為b...

C 單鏈表反轉

複習下c 單鏈表反轉 思路 在順序讀取鍊錶的過程中,對每個節點逐個進行反轉 首先建立單鏈表 struct linknode linknode createlink return head 對鍊錶進行反轉 linknode reverse linknode head q next p head nex...