LoginSignup
0
0

More than 5 years have passed since last update.

push, popしか出来ないStack

Posted at

まずは結論から

コード

struct Stack<Element>{
    public private(set) var items : [Element]

    init() {
        items = [Element]()
    }

    init(_ value : [Element]) {
        items = [Element](value)
    }

    mutating func push(_ item: Element){
        items.append(item)
    }

    mutating func pop() -> Element?{
        return items.popLast()
    }
}

経緯

普通の配列にもpopLastがあるにはあるんですけど,他の方法でアイテムを編集できてしまったりするので,push, popしかできないものがほしいなぁと思ったので。
一応中身の個数とか知りたい場合もあるかと思って配列の中を見ることは出来ます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0