看板 Ruby 關於我們 聯絡資訊
※ 引述《skyboy (yes i do...)》之銘言: : 想請問一下 : ruby在linklist的用法? : 查了一下好像都沒有看到相關的資料 : ex: : struct item{ : int num; : item *parent; : item *next; : }; : ruby有類似用struct和pointer去做linklist的方法嗎? 真要做的話,就只能用 class 啦 class Node attr_accessor :data, :parent, :next def initialize(initValue) @data = initValue end end 至於在 LinkedList 裡面實作就會類似這樣: a = Node.new('a') b = Node.new('b') c = Node.new('c') a.parent = nil; a.next = b b.parent = a ; b.next = c c.parent = b ; c.next = nil b.next.data # output => "c" 相當的容易 --- reference 應該勉強算一種 pointer 吧? XD 如果是用 Ruby 來練習資料結構 會很容易理解, 但是型態上的管理可能會變得比較麻煩 不過這正是 Ruby 的美...... -- ╭───╮╭───╮┬   ╭───╮╭───╮ │   ││   ││   │___│├───╯ ├───╯╰───╯╰───│   ││   \ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.119.98.33
skyboy:感謝 :) 12/21 11:32