Go語言實現棧與隊列基本操作學家
一、前言
go語言中,并沒有棧與隊列相關的數據結構,但是我們可以借助切片來實現棧與隊列的操作;接下來我們一起實現棧與隊列基本操作,并且還會實現用棧實現隊列,用隊列實現棧的操作。
二、實現棧與隊列基本操作
2.1 ?;静僮?/h3>
go語言實現棧和隊列主要用到append 和切片(用內置數組類型進行操作)
//創(chuàng)建棧 stack := make([]int, 0) //push壓入棧 stack = append(stack, 10) //pop彈出 v := stack[len(stack)-1] stack = stack[:len(stack)-1] //檢查??? len(stack) == 0
2.2 隊列基本操作
//創(chuàng)建隊列 queue := make([]int, 0) //enqueue入隊 queue = append(queue, 10) //dequeue出隊 v := queue[0] queue = queue[1:] //檢查隊列為空 len(queue) == 0
三、用棧實現隊列
3.1 理論
使用棧來模式隊列的行為,如果僅僅用一個棧,是一定不行的,所以需要兩個棧一個輸入棧,一個輸出棧,這里要注意輸入棧和輸出棧的關系。
下面動畫模擬以下隊列的執(zhí)行過程如下:
執(zhí)行語句:
queue.push(1); queue.push(2); queue.pop(); 注意此時的輸出棧的操作 queue.push(3); queue.push(4); queue.pop(); queue.pop();注意此時的輸出棧的操作 queue.pop(); queue.empty();

在push數據的時候,只要數據放進輸入棧就好,但在pop的時候,操作就復雜一些,輸出棧如果為空,就把進棧數據全部導入進來(注意是全部導入),再從出棧彈出數據,如果輸出棧不為空,則直接從出棧彈出數據就可以了。
最后如何判斷隊列為空呢?如果進棧和出棧都為空的話,說明模擬的隊列為空了。
3.2 算法題
接下來看一下LeetCode原題

請你僅使用兩個棧實現先入先出隊列。隊列應當支持一般隊列支持的所有操作(push、pop、peek、empty):
實現 MyQueue 類:
void push(int x) 將元素 x 推到隊列的末尾 int pop() 從隊列的開頭移除并返回元素 int peek() 返回隊列開頭的元素 boolean empty() 如果隊列為空,返回 true ;否則,返回 false 說明:
你 只能 使用標準的棧操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。 你所使用的語言也許不支持棧。你可以使用 list 或者 deque(雙端隊列)來模擬一個棧,只要是標準的棧操作即可。
3.3 思路
解決這個問題,需要一個輸出棧和輸入棧
先將數據放到輸入棧,再把數據從輸入棧放到輸出棧,此時輸出棧輸出數據的順序就和隊列一樣了,先入先出
3.4 代碼部分
type MyQueue struct {
stackIn []int // 用來保存Push數據
stackOut []int // 用來保存Pop數據
}
// 棧構造器
func Constructor() MyQueue {
return MyQueue{
stackIn: make([]int, 0),
stackOut: make([]int, 0),
}
}
func (this *MyQueue) Push(x int) {
// 判斷stackOut中是否有元素,有的話全部放到stackIn
for len(this.stackOut) != 0 {
val := this.stackOut[len(this.stackOut)-1]
this.stackOut = this.stackOut[:len(this.stackOut)-1]
this.stackIn = append(this.stackIn, val)
}
// 將數據加進stackIn
this.stackIn = append(this.stackIn, x)
}
func (this *MyQueue) Pop() int {
// 判斷stackIn中是否有元素,有的話全部放到stackOut
for len(this.stackIn) != 0 {
val := this.stackIn[len(this.stackIn)-1]
this.stackIn = this.stackIn[:len(this.stackIn)-1]
this.stackOut = append(this.stackOut, val)
}
// stackOut為零,說明沒有元素,return 0
if len(this.stackOut) == 0 {
return 0
}
// stackOut Pop 元素
res := this.stackOut[len(this.stackOut)-1]
this.stackOut = this.stackOut[:len(this.stackOut)-1]
return res
}
func (this *MyQueue) Peek() int {
// 調用Pop()方法
val := this.Pop()
// val為零,說明沒有元素,return 0
if val == 0 {
return 0
}
// Pop()方法刪除了val,這里加上
this.stackOut = append(this.stackOut, val)
return val
}
func (this *MyQueue) Empty() bool {
// 兩個棧都為空,說明為空,否則不為空
return len(this.stackOut) == 0 && len(this.stackIn) == 0
}代碼可以直接拿到力扣上運行。我已經將細節(jié)全部用注釋解釋了,如果不懂可以私信博主。
四、用隊列實現棧
4.1 理論
隊列模擬棧,其實一個隊列就夠了,那么我們先說一說兩個隊列來實現棧的思路。
隊列是先進先出的規(guī)則,把一個隊列中的數據導入另一個隊列中,數據的順序并沒有變,并沒有變成先進后出的順序。
所以用棧實現隊列, 和用隊列實現棧的思路還是不一樣的,這取決于這兩個數據結構的性質。
但是依然還是要用兩個隊列來模擬棧,只不過沒有輸入和輸出的關系,而是另一個隊列完全用又來備份的!
如下面動畫所示,用兩個隊列que1和que2實現隊列的功能,que2其實完全就是一個備份的作用,把que1最后面的元素以外的元素都備份到que2,然后彈出最后面的元素,再把其他元素從que2導回que1。
模擬的隊列執(zhí)行語句如下:
queue.push(1); queue.push(2); queue.pop(); // 注意彈出的操作 queue.push(3); queue.push(4); queue.pop(); // 注意彈出的操作 queue.pop(); queue.pop(); queue.empty();

4.2 算法題
接下來看一下LeetCode原題

請你僅使用兩個隊列實現一個后入先出(LIFO)的棧,并支持普通棧的全部四種操作(push、top、pop 和 empty)。
實現 MyStack 類:
void push(int x) 將元素 x 壓入棧頂。 int pop() 移除并返回棧頂元素。 int top() 返回棧頂元素。 boolean empty() 如果棧是空的,返回 true ;否則,返回 false 。
注意:
你只能使用隊列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 這些操作。 你所使用的語言也許不支持隊列。 你可以使用 list (列表)或者 deque(雙端隊列)來模擬一個隊列 , 只要是標準的隊列操作即可。
4.3 思路
用兩個隊列que1和que2實現隊列的功能,que2其實完全就是一個備份的作用,把que1最后面的元素以外的元素都備份到que2,然后彈出最后面的元素,再把其他元素從que2導回que1。
4.4 使用兩個隊列實現
type MyStack struct {
//創(chuàng)建兩個隊列
queue1 []int
queue2 []int
}
func Constructor() MyStack {
return MyStack{ //初始化
queue1:make([]int,0),
queue2:make([]int,0),
}
}
func (this *MyStack) Push(x int) {
//先將數據存在queue2中
this.queue2 = append(this.queue2,x)
//將queue1中所有元素移到queue2中,再將兩個隊列進行交換
this.Move()
}
func (this *MyStack) Move(){
if len(this.queue1) == 0{
//交換,queue1置為queue2,queue2置為空
this.queue1,this.queue2 = this.queue2,this.queue1
}else{
//queue1元素從頭開始一個一個追加到queue2中
this.queue2 = append(this.queue2,this.queue1[0])
this.queue1 = this.queue1[1:] //去除第一個元素
this.Move() //重復
}
}
func (this *MyStack) Pop() int {
val := this.queue1[0]
this.queue1 = this.queue1[1:] //去除第一個元素
return val
}
func (this *MyStack) Top() int {
return this.queue1[0] //直接返回
}
func (this *MyStack) Empty() bool {
return len(this.queue1) == 0
}4.5 優(yōu)化
其實這道題目就是用一個隊列就夠了。
一個隊列在模擬棧彈出元素的時候只要將隊列頭部的元素(除了最后一個元素外) 重新添加到隊列尾部,此時在去彈出元素就是棧的順序了。
4.6 使用一個隊列實現
type MyStack struct {
queue []int//創(chuàng)建一個隊列
}
/** Initialize your data structure here. */
func Constructor() MyStack {
return MyStack{ //初始化
queue:make([]int,0),
}
}
/** Push element x onto stack. */
func (this *MyStack) Push(x int) {
//添加元素
this.queue=append(this.queue,x)
}
/** Removes the element on top of the stack and returns that element. */
func (this *MyStack) Pop() int {
n:=len(this.queue)-1//判斷長度
for n!=0{ //除了最后一個,其余的都重新添加到隊列里
val:=this.queue[0]
this.queue=this.queue[1:]
this.queue=append(this.queue,val)
n--
}
//彈出元素
val:=this.queue[0]
this.queue=this.queue[1:]
return val
}
/** Get the top element. */
func (this *MyStack) Top() int {
//利用Pop函數,彈出來的元素重新添加
val:=this.Pop()
this.queue=append(this.queue,val)
return val
}
/** Returns whether the stack is empty. */
func (this *MyStack) Empty() bool {
return len(this.queue)==0
}以上就是Go語言實現棧與隊列基本操作學家的詳細內容,更多關于Go語言棧 隊列的資料請關注腳本之家其它相關文章!
相關文章
解讀rand.Seed(time.Now().UnixNano())的作用及說明
這篇文章主要介紹了關于rand.Seed(time.Now().UnixNano())的作用及說明,具有很好的參考價值,希望對大家有所幫助。2023-03-03
go?sync包中的互斥鎖Mutex和等待組WaitGroup使用詳解
這篇文章主要為大家介紹了go?sync包中的互斥鎖Mutex和等待組WaitGroup使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
關于golang利用channel和goroutine完成統計素數的思路
這篇文章主要介紹了golang利用channel和goroutine完成統計素數的思路詳解,通過思路圖分析及實例代碼相結合給大家介紹的非常詳細,需要的朋友可以參考下2021-08-08
解決golang post文件時Content-Type出現的問題
這篇文章主要介紹了解決golang post文件時Content-Type出現的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05
Go strconv包實現字符串和基本數據類型轉換的實例詳解
在Go語言(Golang)的編程實踐中,strconv包是一個非常重要的標準庫,它提供了在基本數據類型(如整型、浮點型、布爾型)和字符串之間的轉換功能,本文給大家介紹了關于Go語言字符串轉換strconv,需要的朋友可以參考下2024-09-09

