[問題] 救命!完全看不懂LINK LIST

看板C_and_CPP (C/C++)作者 (左)時間13年前 (2012/07/23 00:16), 編輯推噓2(2014)
留言16則, 6人參與, 最新討論串1/2 (看更多)
#include<stdio.h> #include<stdlib.h> #include<memory.h> int main() { struct rst { int i; struct rst *link; }; struct rst node,*head,*tail,*p; head = (struct rst *)malloc(sizeof(node)); tail = (struct rst *)malloc(sizeof(node)); head->i=11; tail->i=99; head->link = tail; free(tail); free(head); system("pause"); return 0; } 以上是照著書寫出的很簡單的LINK LIST 但是實在無法解讀他是怎麼做的 問題1 : struct rst 內又宣告了一個 struct rst *link 這代表甚麼 ORZ 問題2 : 關於符號"->"是指向的意思嗎? head->i=11; 這是說head 指向i i=11嗎?還是說head的i等於11 那head -> link = tail 這該如何解讀 head 的 link 是 tail? 救命阿!!有沒有比較白話的解釋 請高手幫忙 -- 這年頭沒真相沒得混 ORZ..... http://ppt.cc/S!iF -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.45.251.233

07/23 00:17, , 1F
感覺你應該先去了解 指標 才能看這塊吧
07/23 00:17, 1F

07/23 00:17, , 2F
召喚EdisonX板友的精美圖文解說 (喂)
07/23 00:17, 2F

07/23 00:17, , 3F
覺得原PO連struct都很不熟...
07/23 00:17, 3F

07/23 00:18, , 4F
指標還OK struct真的有點不熟~"~
07/23 00:18, 4F

07/23 00:18, , 5F
先忘掉linked-list,把struct的相關語法弄熟一點吧
07/23 00:18, 5F

07/23 00:19, , 6F
好吧ORZ ~在努力把STRUCT搞熟點
07/23 00:19, 6F

07/23 00:22, , 7F
話說這份code變數命名方式還真不是一般人在用的..
07/23 00:22, 7F

07/23 00:22, , 8F
可是還是想知道 struct rst *link; 是甚麼意思
07/23 00:22, 8F

07/23 00:23, , 9F
link為一個指標,它指向struct rst這個資料型態
07/23 00:23, 9F

07/23 00:25, , 10F
http://ideone.com/XdWh4 看懂這個小範例對你大概有幫助~
07/23 00:25, 10F

07/23 00:43, , 11F
感謝各位高手賜教 看完範例聽完解釋 突然懂了XD
07/23 00:43, 11F
※ 編輯: a0916327869 來自: 118.160.164.189 (07/23 09:54)

07/23 11:16, , 12F
每個node裡面存一個指標,指向下個node。
07/23 11:16, 12F

07/23 12:06, , 13F
第一次看到資料結構書中有關linked list的struct定義,也是
07/23 12:06, 13F

07/23 12:07, , 14F
也是看不太懂,但時間會解決一切問題 @@
07/23 12:07, 14F

07/23 12:08, , 15F
有些還會用 typedef struct 另外將struct命名,但C與C++在
07/23 12:08, 15F

07/23 12:09, , 16F
struct的用法略有不同...
07/23 12:09, 16F
文章代碼(AID): #1G32T0PG (C_and_CPP)
文章代碼(AID): #1G32T0PG (C_and_CPP)