切片实现 stack

package main

import (
	"fmt"
)

type StackQueue struct {
	Arr []interface{}
}
func New(max int) *StackQueue{
    sq:=new(StackQueue)
    sq.Arr = make([]interface{},0, max)
    return sq
}

//入栈
func (this *StackQueue) Push(value interface{}) {
	n:=len(this.Arr)
	this.Arr = append(this.Arr[:n],value)
}
//出栈
func (this *StackQueue) Pop() (interface{}) {
	n:=len(this.Arr)
	if(n<=0){
		return nil
	}
	ele:=this.Arr[n-1]
	this.Arr = append(this.Arr[:n-1])
	return ele
}
//显示栈
func (this *StackQueue) Show() {
	fmt.Printf("%d", this.Arr)
}

func main() {
	s1:=New(1)
	s1.Push(3)
	s1.Push(22)
	s1.Push(5)
	ele:=s1.Pop()
	fmt.Printf(" -> %d\n", ele)
	s1.Show()
}

-> 5
[3 22]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值