[問題] 串鍊鏈結,新增節點函數問題

看板C_and_CPP作者 (drinktea)時間6年前 (2019/03/23 03:43), 編輯推噓4(4013)
留言17則, 8人參與, 6年前最新討論串1/1
各位高手們好,小的在自修串鍊連結遇到以下問題 在想如何寫一個"單向鏈結串列"的新增函數 以下是我參考的書籍所寫的程式碼 -----------------我是分隔線----------------------------------- struct student { char name[20]; int score; struct student *next; } struct student *ptr,*head,*current,*prev; void insert_func(void){ ptr = (struct student *)malloc(sizeof(struct student)); ptr->name =(char *)”John”; ptr->score = (int *) 85; prev = head; current = head->next; while((current != NULL) && (current->score > ptr->score) ){ prev = currect; current = current->next; } ptr->next = current; prev->next = ptr; } ---------------------我是分隔線----------------------------------------- 以上是串鍊連結的程式碼,目的是"新增node並排序"。 我想請教 insert_func()中的程式碼,指標 prev = head 之後的部分看不懂,想求大神 解釋。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.129.55.180 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1553312592.A.D6A.html

03/23 13:45, 6年前 , 1F
你有把你寫的code丟進編譯器編過嗎?
03/23 13:45, 1F

03/23 23:08, 6年前 , 2F
這程式編譯不會過吧...
03/23 23:08, 2F

03/24 03:38, 6年前 , 3F
我是擷取其中一部分拉,重點是insert_func我看不懂,我沒
03/24 03:38, 3F

03/24 03:39, 6年前 , 4F
把main()加進來
03/24 03:39, 4F

03/24 04:25, 6年前 , 5F
prev=head之後,current取得list的第二個節點,接下來只
03/24 04:25, 5F

03/24 04:25, 6年前 , 6F
要list還沒走到尾巴並且current存的節點的成績大於新節
03/24 04:25, 6F

03/24 04:25, 6年前 , 7F
點的成績,current就會持續往下個節點走訪,一旦while
03/24 04:25, 7F

03/24 04:25, 6年前 , 8F
條件不成立後,代表找到要插入新節點的位置,即開始進
03/24 04:25, 8F

03/24 04:25, 6年前 , 9F
行插入新節點的動作,概念上差不多是在做這些事
03/24 04:25, 9F

03/24 04:57, 6年前 , 10F
你這是什麼程式語言?
03/24 04:57, 10F

03/24 09:09, 6年前 , 11F
樓上,看起來應該是C
03/24 09:09, 11F

03/24 11:49, 6年前 , 12F
把新增的分數自動排序到list
03/24 11:49, 12F

03/24 13:03, 6年前 , 13F
Malloc 完對node的init 編譯會報錯吧......
03/24 13:03, 13F

03/24 13:06, 6年前 , 14F
Char Array 沒在宣告時初始化 後續應該用strcpy 無法直接ass
03/24 13:06, 14F

03/24 13:06, 6年前 , 15F
ign 另外 int scope 為啥要轉成 pointer 再給值呢?
03/24 13:06, 15F

03/24 13:13, 6年前 , 16F
程式插入第一個node不會比較 邏輯怪怪的 如果想當head node
03/24 13:13, 16F

03/24 13:13, 6年前 , 17F
不建議用一樣的structure
03/24 13:13, 17F
文章代碼(AID): #1SbQjGrg (C_and_CPP)