Go语言实现栈数据结构
在Go语言中,栈是一种线性数据结构,遵循后进先出 (LIFO) 原则,这意味着最后压入栈的元素将首先弹出。我们将使用两种方法来使用整数切片和结构体来实现栈数据结构。让我们来看不同的例子来理解这个概念。
方法一:使用整数切片
在这个方法中,Go 使用两个函数 Push 和 Pop 分别从栈顶添加和删除值,并使用整数切片来存储栈中的值。Push 函数将一个整数添加到输入的切片末尾。Pop 函数从切片末尾获取值并返回它。IsEmpty 方法通过检查切片的长度来确定栈是否为空。主函数使用 Push 和 Pop 函数向栈中添加和删除值,并使用 IsEmpty 函数确定栈是否为空。
算法
步骤1 − 创建一个 main 包,并在程序中声明 fmt(格式化包)。main 包生成可执行代码,fmt 包帮助格式化输入和输出。
步骤2 − 创建一个名为 Stack 的整数切片类型。
步骤3 − 然后,使用 Push 方法向栈顶添加一个值。
步骤4 − 输入为 Stack 和一个整数 v。
步骤5 − 结果为在栈顶添加了 v 的栈。
步骤6 − 在下一步中,实现 Pop 方法以删除并返回栈顶的值。
步骤7 − 输出为栈顶的值和删除了顶值的栈。
步骤8 − 实现 IsEmpty 函数以确定栈是否为空。
步骤9 − 输出将是一个布尔值,指示栈是否为空。
步骤10 − 在主函数中创建一个 Stack 类型的实例,使用 Push 方法添加一些值,并使用 Pop 方法删除一些值。最后,使用 IsEmpty 函数查看栈是否为空。
示例
在这个例子中,我们将使用整数切片来实现栈数据结构。让我们看一下代码。
package main
import "fmt"
type Stack []int //stack
// Push adds a value to the top of the stack.
func (st *Stack) Push(v int) {
*st = append(*st, v)
}
// Pop removes and returns the top value from the stack.
func (st *Stack) Pop() int {
if st.IsEmpty() {
return 0
}
top := (*st)[len(*st)-1]
*st = (*st)[:len(*st)-1]
return top
}
func (st *Stack) IsEmpty() bool {
return len(*st) == 0
}
func main() {
st := Stack{}
st.Push(1)
st.Push(2)
fmt.Println("The value popped from the stack is given as:")
fmt.Println(st.Pop())
fmt.Println(st.Pop())
fmt.Println("Is the stack empty?")
fmt.Println(st.IsEmpty())
}
输出
The value popped from the stack is given as: 2 1 Is the stack empty? true
方法二:使用结构体
在这种方法中,栈的值存储在一个结构体中,一个 top 变量跟踪栈中顶值的索引。Push 方法通过增加 top 变量来将一个新值添加到 values 切片的末尾。Pop 方法递减 top 变量,获取当前顶点索引处的值并返回它。如果 top 变量等于 -1,表示栈中没有值,则 IsEmpty 方法返回 true。让我们看一下代码和算法。
算法
步骤1 − 创建一个 main 包,并在程序中声明 fmt(格式化包)。main 包生成可执行代码,fmt 包帮助格式化输入和输出。
步骤2 − 创建一个栈结构体,其中 values 和 top 为其两个字段。这里,top 是一个整数,跟踪栈中顶值的索引,values 是一个整数切片。
步骤3 − 在下一步中,创建一个名为 NewStack 的函数,该函数返回一个新的 Stack 结构体实例,该实例的 top 值为 -1,并且 values 切片为空。
步骤4 − 然后,使用 Push 方法将值压入栈顶。
步骤5 − 输入将是 Stack 结构体实例和整数值。
步骤6 − 输出将是栈,最新的值在栈顶。
步骤7 − 然后,实现一个 Pop 方法,该方法将栈顶的值弹出并返回它。
步骤8 − 在下一个情况下,输入将是 Stack 结构体的实例。
步骤9 − 输出将是栈的顶值,并且该值将被删除。
步骤10 − 实现一个返回 true 的 IsEmpty 方法,以确定栈是否为空。
步骤11 − 输出将是一个布尔值,指示栈是否为空。
步骤12 − 在主函数中使用 NewStack 函数创建一个 Stack 类型的实例,然后使用 Push 和 Pop 方法添加和删除值。最后,使用 IsEmpty 函数查看栈是否为空。
步骤13 − 使用 fmt.Println() 方法执行打印语句,其中 ln 表示换行。
示例
在这个例子中,我们将使用栈结构体来实现栈数据结构。让我们看一下代码。
package main
import "fmt"
type Stack struct { //stack
values []int
top int
}
func NewStack() *Stack {
return & Stack{
values: make([]int, 0),
top: -1,
}
}
// Push adds a value to the top of the stack.
func (st *Stack) Push(value int) {
st.top++
st.values = append(st.values, value)
}
// Pop removes and returns the top value from the stack.
func (st *Stack) Pop() int {
if st.IsEmpty() {
return 0
}
value := st.values[st.top]
st.top--
return value
}
func (st *Stack) IsEmpty() bool {
return st.top == -1
}
func main() {
st := NewStack()
st.Push(1)
st.Push(2)
fmt.Println("The value popped from the stack is given as:")
fmt.Println(st.Pop())
fmt.Println(st.Pop())
fmt.Println("Is the stack empty?")
fmt.Println(st.IsEmpty())
}
输出
The value popped from the stack is given as: 2 1 Is the stack empty? true
结论
我们使用两种方法执行了实现栈数据结构的程序。在第一种方法中,我们使用了整数切片,在第二种方法中,我们使用了栈结构体。
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP