企业网站建设的步骤过程,中学生制作网站怎么做,买的虚拟主机怎么做网站,深圳高端网站定制公司线性表中#xff0c;栈和队列是非常重要的两种数据结构#xff0c;本文将就这两种数据结构进行 golang语言实现参考#xff1a;go语言中文文档#xff1a;www.topgoer.com转自#xff1a;https://www.jianshu.com/p/e8de9ac93cbc一.栈的实现我们需要实现如下几个方法push(…线性表中栈和队列是非常重要的两种数据结构本文将就这两种数据结构进行 golang语言实现参考go语言中文文档www.topgoer.com转自https://www.jianshu.com/p/e8de9ac93cbc一.栈的实现我们需要实现如下几个方法push() 向栈中压入一个元素pop() 从栈顶取出一个元素isEmpty() 判断栈是否为空length() 获取栈中元素的数目peer() 查询栈顶元素我们需要注意 peer() 方法并不会将栈顶元素删除数组实现如下type stack struct { cache []int}func (sk *stack) push(n int) { sk.cache append(sk.cache, n)}func (sk stack) length() int { return len(sk.cache)}func (sk *stack) pop() int { if sk.length() 0 { return 0 } item : sk.cache[sk.length()-1] sk.cache sk.cache[:len(sk.cache)-1] return item}func (sk stack) isEmpty() bool { return len(sk.cache) 0}func (sk stack) peer() int { return sk.cache[sk.length()-1]}接下来我们将用链表实现以下项目并使用 interface{} 来代替 int实现多种类型的兼容type stackLink struct { Top *node Length int}type node struct { Val interface{} Prev *node}func (sl *stackLink) push(value interface{}) { newNode : node{ Val: value, Prev: sl.Top} sl.Top newNode sl.Length}func (sl *stackLink) pop() interface{} { topNodeVal : sl.Top.Val sl.Top sl.Top.Prev sl.Length-- return topNodeVal}func (sl stackLink) length() int { return sl.Length}func (sl stackLink) isEmpty() bool { return sl.Length 0}func (sl stackLink) peer() interface{} { return sl.Top.Val}由于任何的变量都实现了空接口所以我们可以通过传递空接口来实现在栈中压入不同元素的目的二.队列实现同样我们对于队列实现了如下方法enqueue() 入队列dequeue() 出队列isEmpty() 判断队列是否为空getLength() 获队列的长度链表实现方式如下type queue struct { First *node Last *node Len int}type node struct { Val interface{} Next *node Pre *node}func (qu *queue) enqueue(data interface{}) { nNode : node{ Val: data, Pre: qu.First, Next: nil} if qu.First nil { qu.First nNode } else { qu.First.Next nNode qu.First nNode } if qu.Last nil { qu.Last nNode } qu.Len}func (qu *queue) dequeue() interface{} { if qu.Len 0 { nNode : qu.Last.Val if qu.Last.Next ! nil { qu.Last.Next.Pre nil } qu.Last qu.Last.Next qu.Len-- return nNode } return errors.New(error)}func (qu queue) isEmpty() bool { return qu.Len 0}func (qu queue) getLength() int { return qu.Len}三.栈和队列的应用在这一部分我们通过栈来实现表达式的计算例如我们需要计算 (1((23)*(4*5)))我们维护两个栈一个是值栈一个是操作栈我们在读取表达式的时候采取如下的策略如果遇到 (我们将忽略它如果遇到数字将其压入值栈如果遇到操作符将其压入操作栈如果遇到 )我们从值栈中取出两个值 n1和 n2在操作栈中我们取出一个操作符 op我们进行 n2 op n1的操作(例如 n1 3n2 9op /我们将执行 9/3 )将所得的结果压入值栈func stackCompute(str string) int { var ( vsk stackLink opsk stackLink ) for _, v : range str { if v 9 v 0 { vsk.push(int(v) - 0) } else if v || v - || v * || v / { opsk.push(int(v)) } else if v ) { n1 : vsk.pop().(int) n2 : vsk.pop().(int) op : opsk.pop().(int) var ans int switch op { case : ans n2 n1 case -: ans n2 - n1 case *: ans n2 * n1 case /: ans n2 / n1 } vsk.push(int(ans)) } } for !opsk.isEmpty() { n1 : vsk.pop().(int) n2 : vsk.pop().(int) op : opsk.pop().(int) var ans int switch op { case : ans n2 n1 case -: ans n2 - n1 case *: ans n2 * n1 case /: ans n2 / n1 } vsk.push(int(ans)) } char : vsk.pop().(int) return int(char)}我们如下调用stackCompute((1((23)*(4*5))))将会得到结果 101