stack

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2020 License: Apache-2.0 Imports: 0 Imported by: 0

README

栈(Stack)

问题引入:

如何计算[3+2*6-2]这个字符串呢?

先入后出(FILO FirstInLastOut)

栈顶和栈底,Top:可以插入和删除,Bottom是固定的一端

栈(stack)是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表

- 子程序的调用:在跳往子程序前,会先将下个指令的地址存到堆栈中,直到子程序执行完后再
将地址取出,以回到原来的程序中。 
- 处理递归调用:和子程序的调用类似,只是除了储存下一个指令的地址外,也将参数、区域变
量等数据存入堆栈中。
- 二叉树的遍历。
- 图形的深度优先(depth 一 first)搜索法。

数组实现栈

加减乘除算法用栈实现
1. 创建两个栈,numStack,operStack
2. exp计算表达式
3. 扫描是数字,直接入numStack;
4. 如果是运算符
4.1 如果operStack是空栈,直接入栈;
不是空栈的话,如果栈顶的运算符优先级大于当前准备入栈的运算符优先级,先pop出栈
并将数栈也pop出两个数,进行运算,将运算的结果push到数栈,符号再入符号栈
5. 如果扫描表达式完毕,依次从符号栈取出符号,然后从数栈取出两个数,运算后的结果入数栈,直到符号位为空.register

  • goland template
/*
@Time : ${DATE} ${TIME} 
@Author : ${USER}
@File : ${NAME}
@Software: ${PRODUCT_NAME}


package ${GO_PACKAGE_NAME}
*/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	// contains filtered or unexported fields
}

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

Stack is a base structure for LIFO

func New

func New() *Stack

New Initializes new Stack

func (*Stack) IsEmpty

func (stack *Stack) IsEmpty() bool

IsEmpty return true of a stack depth == 0

func (*Stack) Peek

func (stack *Stack) Peek() interface{}

Peek returns top of a stack without deletion

func (*Stack) Pop

func (stack *Stack) Pop() interface{}

Pop Deletes top of a stack and return it

func (*Stack) Push

func (stack *Stack) Push(item interface{})

Push a given item into Stack

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL