Item class Item name def initializen a b
復習:クラス定義の例(続) ただ、このままだと、部品にアクセス(取り出し、参照) しにくいので… Item class Item name def initialize(n, a, b, s) address @name = n birth_date @address = a sex age @birth_date = b @sex = s @age = Time. now. year - b (1) attr_accessor につづけて (2) インスタンス変数名の前にコロン( : ) end attr_accessor : name, : address, : birth_date, : sex, : age end
復習:演習 2:複素数 (3) インスタンスを 2つ以上つくり、計算がちゃん と行われることを確かめよ a = Complex. new(1, -1) b = Complex. new(3, 2) # print a. show, " + ", b. show, " = “; print (a. tasu(b)). show, "n“ => 4. 0 + 1. 0 i print a. show, " - ", b. show, " = “; print (a. hiku(b)). show, "n" => -2. 0 - 3. 0 i print a. show, " * ", b. show, " = “; print (a. kakeru(b)). show, "n“ => 5. 0 – 1. 0 i print a. show, " / ", b. show, " = “; print (a. waru(b)). show, "n“ => 0. 077 + 0. 385 i
Cell について x = Cell. new(“a”, Cell. new(“b”, Cell. new(“c”, nil))) により作成された連結リストは: x data “a” next data “b” next data “c” next
クラスCellにgetメソッドを追加 class Cell def initialize(x, y) @data = x @next = y end # def attr_accessor : data, : next def get(n) return self if (n <= 0) if (@next != nil) return @next. get(n-1) else return nil end # if end #def end # class 既に定義済み self は「自分自身」を 指すポインタ
クラスCellのgetメソッドの検証 c = Cell. new(“c”, nil) ; b = Cell. new("b", c) ; x = Cell. new("a", b) # getの検証 p p p class Cell def get(n) n=0 x. get(0) return self if (n <= 0) x (= self) if (@next != nil) n=1 return @next. get(n-1) x. get(1) else b (= b. get(0)) return nil end # if x. get(2) = b. get(1) end #def end = c. get(0) = c data next p x. get(3) x “a” “b” b “c” c
クラスCellにinsertメソッドを追加(続) class Cell def initialize(x, y) @data = x @next = y end # def attr_accessor : data, : next def get(n) …. end 既に定義済み def insert(n, cont) if (n == 1) @next = Cell. new(cont, @next) else # n > 1 と仮定 ここにはちょっと問題がある @next. insert(n - 1, cont) 値のチェックをすることが必要。 end # if end #def end # class
クラスCellのinsertメソッドの検証 c = Cell. new(“c”, nil) ; b = Cell. new("b", c) ; x = Cell. new("a", b) x. insert(2, “d”) n =2 n =1 b. insert(1, ”d”) “d” data next x “a” data next “b” b def insert(n, cont) if (n == 1) “d” @next) next = Cell. new(cont, @next else # n > 1 と仮定 @next. insert(n - 1, cont) next end # if end #def data next “c” data next
リストの先頭に insert data a next “h” “a” data “b” next data next “c” このとき、insert(1, ”h”) をすると、どうなる? data y next 該当するプログラム: y = Cell. new(@data, @next) cont y @data, @next = cont,
クラスCellにdeleteメソッドを追加 class Cell def initialize(x, y) @data = x 既に定義済み @next = y end # def attr_accessor : data, : next def get(n) …. def end insert(n, cont) … end def delete(n) if (n == 1) @next = @next else # n > 1 と仮定 @next. delete(n - 1) end # if end #def end # class ここにはちょっと問題がある 値のチェックをすることが必要。
クラスCellのdeleteメソッドの検証 c = Cell. new(“c”, nil) ; d = Cell. new(“d”, c) b = Cell. new("b", c) ; x = Cell. new("a", b) def delete(n) if (n == 1) @next = @next else # n > 1 と仮定 @next. delete(n - 1) end # if end #def x. delete(2) n =2 n =1 b. delete(1) data x next “a” data next “b” b data next “d” d data “c” c next
リストの実現のための別な方法(続) • この方式による新たなリスト構造のための データ構造(クラス)をNCellとする class NCell def initialize(data) @contents = Cell. new(nil, data) end attr_accessor : contents end 使用例: c = Cell. new(“c”, nil) ; b = Cell. new("b", c) a = Cell. new("a", b) ; x = NCell. new(a)
- Slides: 43