栈:四个字就是后进先出,或者先进后出的一种数据结构,可以基于数组实现,也可以基于链表实现,栈(操作系统)在计算机中是由操作系统自动分配,存放函数的参数值与参数变量的值等
栈的优势是,存取速度比堆要快,仅次于直接位于CPU中的寄存器。但缺点是,存在栈中的数据大小与生存期必须是确定的,缺乏灵活性。另外,栈数据在多个线程或者多个栈之间是不可以共享的,但是在栈内部多个值相等的变量是可以指向一个地址的。
只要栈的剩余空间大于所申请空间,系统将为程序提供内存,否则将报异常提示栈溢出(但在这里我没有实现,感觉可以的小伙伴可以自己尝试一下)。
编写数组基类
package main
import (
"fmt"
"errors"
)
//interface 实例化int,int., string ,string
type List interface {
Size() int //函数大小,返回大小
Get(index int)(interface{},error) //根据索引抓取数据
Set(index int,newval interface{})error //设置
Insert(index int,newval interface{})error //插入
Append(newval interface{}) error//追加
Remove(index int)error //删除
Clear() //清空
String() string //返回字符串
}
//结构体
type ArrayList struct{
dataStore [] interface{}
theSize int
}
func New() *ArrayList{
list:=new(ArrayList)
list.dataStore=make([]interface{},0,10) //分配内存10个数组元素
list.theSize=0 //0
//fmt.Println("new",list.theSize,cap(list.dataStore))
return list
}
func (list *ArrayList) Size() int{
return list.theSize //返回大小
}
func(list *ArrayList) Append (newval interface{}){
//list.checkmemisfull()
list.dataStore=append(list.dataStore,newval) //数据叠加
//fmt.Println(list.theSize,cap(list.dataStore))
//list.dataStore[list.theSize]=newval //赋值
list.theSize++ //索引移动
}
func (list *ArrayList) Get(index int)(interface{},error){
if index <0 || index >=list.theSize{
return nil,errors.New("索引越界")
}
return list.dataStore[index],nil
}
func (list *ArrayList) Set(index int,newval interface{})(error){
if index <0 || index >=list.theSize{
return errors.New("索引越界")
}
list.dataStore[index]=newval //赋值新的值
return nil
}
func (list *ArrayList ) checkmemisfull(){
if list.Size()==cap(list.dataStore){
newDataStore:=make([]interface{},0,2*list.Size())//开辟更大内存
copy(newDataStore,list.dataStore) //拷贝
list.dataStore=newDataStore //赋值
}
}
func (list *ArrayList )Insert(index int,newval interface{})error {
if index <0 || index >=list.theSize{
return errors.New("索引越界")
}
list.checkmemisfull()
list.dataStore=list.dataStore[:list.Size()+1]//开辟内存,延展使用的内存
for i:=list.Size();i>index;i--{
list.dataStore[i]=list.dataStore[i-1] //从后往前赋值
}
list.dataStore[index]=newval //插入数据
list.theSize++ //索引加1
return nil
}
func (list *ArrayList )Remove(index int)error {
if index <0 || index >=list.theSize{
return errors.New("索引越界")
}
list.dataStore=append(list.dataStore[:index],list.dataStore[index+1:]...) //删除
list.theSize--
return nil
}
func (list *ArrayList )Clear() {
list.dataStore=make([]interface{},0,10) //清空
list.theSize=0
}
func (list *ArrayList ) String() string {
return fmt.Sprint(list.dataStore)
}
编写堆栈基类
package main
type StackArray interface{
Clear()
Size()int
Pop() interface{}
Push(data interface{})
isEmpty() bool
isFull() bool
}
type Stack struct{
datasoure [] interface{}
capsize int
currentsize int
}
func NewStack()*Stack{
mystack:=new(Stack)
mystack.datasoure=make([]interface{},0,10) //开辟内存
mystack.capsize=10 //空间
mystack.currentsize=0 //0
return mystack
}
func (mystack *Stack) Clear(){
mystack.datasoure=make([]interface{},0)
mystack.capsize=0
mystack.currentsize=0
}
func (mystack *Stack) Size()int {
return mystack.currentsize
}
func (mystack *Stack) isEmpty() bool{
if mystack.currentsize==0{
return true
}else{
return false
}
}
func (mystack *Stack) isFull() bool{
if mystack.currentsize==mystack.capsize{
return true
}else{
return false
}
}
func (mystack *Stack) Pop() interface{}{
if !mystack.isEmpty(){
last:=mystack.datasoure[mystack.currentsize-1]
mystack.datasoure= mystack.datasoure[:mystack.currentsize-1]
mystack.currentsize--
return last
}
return nil
}
func (mystack *Stack) Push(data interface{}){
if !mystack.isFull(){
mystack.datasoure=append(mystack.datasoure,data) //压入
mystack.currentsize++
}
}
编写main函数调用
func main(){
mystack :=NewStack()
mystack.Push(1)
mystack.Push(2)
mystack.Push(3)
mystack.Push(4)
fmt.Println(mystack.Pop())
fmt.Println(mystack.Pop())
fmt.Println(mystack.Pop())
fmt.Println(mystack.Pop())
}
运行结果