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