[蟲?] Stack 的code 有錯

看板java作者 (支持自經區服貿貨貿)時間10年前 (2015/04/13 02:42), 10年前編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/1
剛剛在看Cracking the Coding Interview 發現他的程式有一個小錯 他是這樣寫的 class Stack{ Node top; Object pop(){ if (top != null){ Node item = top.data; top = top.next;                ^^^^^^^^^        return item; } return null; } void push(Object item){ Node t = new Node(item); t.next = top; top = t; } Object peek(){ return top.data; } } ........................ 在 pop()這個方法裡,top.data 依照書中class Node裡的data型態是int 所以Node item 不應該等於top.data 應該要改成 Node item = top; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 135.0.163.188 ※ 文章網址: https://www.ptt.cc/bbs/java/M.1428864138.A.1CF.html ※ 編輯: yule1224 (135.0.163.188), 04/13/2015 02:43:36

04/13 03:11, , 1F
未找資料,但應該就是你所講的= top; 推個。
04/13 03:11, 1F
文章代碼(AID): #1LAhoA7F (java)
文章代碼(AID): #1LAhoA7F (java)