[問題] Linklist的Reverse Bug
這是用C寫下來的
Linklist 反轉函式
在輸出的時候有點問題
就是說 假設我裡面的資料是 4->8->11->357
反轉之後印出
會得到 4->357->11->8
第一個資料並沒有反轉到
呼叫這個函式的時候 我傳入了一個代表串列頭的指標 first
也就是reverse_lk(first);
印的時候也是傳入first;
在程式碼裡面有兩行的printf("%p\n",ptr);
我發現ptr的位址並沒有改
也就是可能first並沒有反轉到最後面去嗎??
如果是這樣的話 請問一下要怎麼樣改呢?
謝謝大家
--------------------------------------------------------------------
void reverse_lk( NODE *ptr)
{
NODE *prev_node,*this_node,*next_node;
printf("%p\n",ptr);
next_node=ptr->next;
this_node=NULL;
while(next_node->next!=NULL){//反轉
prev_node=this_node;
this_node=next_node;
next_node=next_node->next;
this_node->next=prev_node;
}
next_node->next=this_node;//把頭接上
ptr->next=next_node;
printf("%p\n",ptr);
}
然後這是我印資料的程式碼
void print_lk( NODE *ptr)//印出值
{
NODE *n=ptr;
while(n->next!=NULL)
{
printf("%d ->",n->data);
n=n->next;
}
printf("%d ->",n->data);
printf(" end\n");
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.116.117.15
推
12/23 19:27, , 1F
12/23 19:27, 1F
→
12/23 19:28, , 2F
12/23 19:28, 2F
→
12/23 19:29, , 3F
12/23 19:29, 3F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章