Swift建立棧的泛型結構體以及top()、push()、pop()定義函數的定義

來源:互聯網
上載者:User

標籤:

首先可以使用swift定義Stack的結構體

//泛型表達

struct Stack<T> {

  var items = <T>()

  //定義棧頂函數,返回棧頂元素

  mutating func top()->T{

    return items.last!

  }

  //定義push函數,將item插入棧中

  mutating func push(item:T){

    items.append(item)

  }

  //定義pop函數,將棧頂函數退棧

  mutating func pop()->T{

      return items.removeLast()

  }

}

Swift建立棧的泛型結構體以及top()、push()、pop()定義函數的定義

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.