栈(golang)


栈的定义

栈(stack)是限定在仅在表尾插入和删除的操作的线性表。
我们允许插入和删除的一端称为栈顶(top),另一端称为栈底(bottom),不含任何数据元素的栈称空栈。栈又称为后进先出(Last in First out)的线性表,简称LIFO结构。

理解栈的定义需要注意:
   它是一个特殊的线性表,也就是说,栈元素具有线性关系,即前驱后继关系。只不过它是一种特殊的线性表。定义中说在线性表尾进行插入的和删除操作,这里的表尾是指栈顶,而不是栈底。


栈的插入,叫作进栈,也称压栈、入栈。
栈有删除操作,叫作出栈,也有的叫弹栈。

在这里插入图片描述

package main

import(
	"fmt"
)

/*
*  栈的抽象数据类型
* ADT statck
* Data
*   同线性表。元素具有相同的类型,相邻的元素具有前驱和后继关系
* Operation
*   InitStack(*S):初始化操作,建立一个空栈。
*   DestroyStack(*S): 若栈存在,则销毁它。
*   ClearStack(*S): 将栈清空
*   StackEmpty(*S): 若栈为空,返回true,否则返回false.
*   GetTop(S,*e): 若栈存在并非空,用e返回栈顶的元素
*   Push(*S,e): 若栈S存在,插入元素e到栈S中并成为栈顶
*   Pop(*S,*e): 删除栈S中栈顶元素,并用e返回其值。
*   StackLength(*S): 返回栈S的元素个数.
*/

//使用顺序存储实现的栈

const MAXSIZE = 5

type Stack struct{
	 stack *[MAXSIZE]int
	 top int
}

func InitStack(st *Stack) *Stack{
	 st = new(Stack)
	 st.stack = new([MAXSIZE]int)
	 return st
}

func DestroyStack(st *Stack) *Stack{
	fmt.Printf("%p\n",st)
	 st = nil
	 return st
}

func ClearStack(st *Stack){
	st.stack = new([MAXSIZE]int)
	st.top = 0
}

func Push(st *Stack, e int){
	 if st.top == MAXSIZE{
		 fmt.Printf("stack full\n")
		 return
	 }
	 st.stack[st.top] = e
	 st.top++
}

func Pop(st *Stack, e *int){
	 if st.top <= 0{
		 fmt.Println("stack is empty")
		 return
	 }
	 *e = st.stack[st.top-1]
	 st.stack[st.top-1] = 0
	 st.top--
}

func StackLength(st *Stack)int{
	 return st.top
}

func GetTop(st *Stack, e *int){
	if st.top <= 0{
		fmt.Printf("stack is empty")
		return
	}
	*e = st.stack[st.top-1]
}

func StackEmpty(st *Stack) bool{
	if st.top == 0{
		return true
	}
	return false
}

func main(){
	var st *Stack 
	st = InitStack(st)
	//fmt.Printf("%#v\n",st)
	//st = DestroyStack(st)
	fmt.Printf("%#v\n",st)
	Push(st,1)
	Push(st,2)
	Push(st,3)
	Push(st,4)
	Push(st,5)
	fmt.Printf("%#v\n",st.stack)

	var e *int = new(int)
	Pop(st,e)
	fmt.Printf("e:%v,length:%v, values:%v\n",*e,StackLength(st),st.stack)

	Pop(st,e)
	fmt.Printf("e:%v,length:%v, values:%v\n",*e,StackLength(st),st.stack)


	Pop(st,e)
	fmt.Printf("e:%v,length:%v, values:%v\n",*e,StackLength(st),st.stack)

	Pop(st,e)
	fmt.Printf("e:%v,length:%v, values:%v\n",*e,StackLength(st),st.stack)


	Pop(st,e)
	fmt.Printf("e:%v,length:%v, values:%v\n",*e,StackLength(st),st.stack)

	Pop(st,e)
	fmt.Printf("e:%v,length:%v, values:%v\n",*e,StackLength(st),st.stack)

	fmt.Println()
	for i:=10; i <15;i++{
		Push(st,i)
	}
	fmt.Printf("%#v\n",st.stack)
	fmt.Println()

	for i:=0; i <5;i++{
	  Pop(st,e)
	  fmt.Printf("e:%v,length:%v, values:%v\n",*e,StackLength(st),st.stack)
	}

	fmt.Println()

	Push(st,100)
	GetTop(st,e)
	fmt.Printf("top value:%v\n",*e) //100

	Push(st,200)
	GetTop(st,e)
	fmt.Printf("top value:%v\n",*e)//200

	fmt.Printf("is empty stack:%v\n",StackEmpty(st))
	ClearStack(st)
	fmt.Printf("is empty stack:%v\n",StackEmpty(st))
	fmt.Printf("%#v\n",st.stack)
}





stack_obj.go 
package main

import(
	"fmt"
)


type Stack struct{
	 stack []int
	 top int
	 maxsize int
}

func NewStack(maxsize int) *Stack{
	return &Stack{
		stack: make([]int,maxsize,maxsize),
		top: 0,
		maxsize: maxsize,
	}
}

func (this *Stack) Destroy(){
	 this.stack = nil
	 this.top = 0
	 this.maxsize = 0
}


func (this *Stack)Push(value int)(bool,error){
	if this.top >= this.maxsize{
		return false,fmt.Errorf("stack is full")
	}
	this.stack[this.top] = value
	this.top++
	return true,nil
}

func (this *Stack)Pop()(value int,err error){
	 if this.top <=0 {
		 value = -1
		 err = fmt.Errorf("stack is empty")
		 return
	 }
	 value = this.stack[this.top-1]
	 this.stack[this.top-1] = 0
	 this.top--
	 return
}

func (this *Stack) Clear()(bool,error){
	 if this.stack == nil{
		 return false,fmt.Errorf("stack is nil")
	 }
	 this.top = 0
	 this.stack = make([]int,this.maxsize,this.maxsize)
	 return true
}

func (this *Stack) Emepty() bool{
	  if this.top == 0 || this.stack == nil{
		  return true
	  }
	  return false
}

func (this *Stack) Size() int{
	 return this.top
}


func main(){
	st := NewStack(5)
	fmt.Printf("%#v, stack:%v\n",st,st.stack)
	//st.Destroy()
	//fmt.Printf("%#v\n",st)
	for i:=0; i <5;i++{
		st.Push(i*2)
	}
	fmt.Printf("%#v, stack:%v\n",st,st.stack)
	_,err := st.Push(100)
	fmt.Printf("push err:%v\n",err)
	fmt.Println()

	for i:=0; i <5;i++{
		v,err := st.Pop()
		fmt.Printf("v:%v,err:%v\n",v,err)
	}
	fmt.Printf("%#v, stack:%v\n",st,st.stack)
	fmt.Println()
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值