溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python中怎么用隊列實現(xiàn)棧

發(fā)布時間:2021-10-26 10:48:53 來源:億速云 閱讀:203 作者:柒染 欄目:編程語言

這篇文章給大家介紹Python中怎么用隊列實現(xiàn)棧,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

題目:

使用隊列實現(xiàn)棧的下列操作:

  • push(x) – 元素 x 入棧

  • pop() – 移除棧頂元素

  • top() – 獲取棧頂元素

  • empty() – 返回棧是否為空

Implement the following operations of a stack using queues.

  • push(x) – Push element x onto stack.

  • pop() – Removes the element on top of the stack.

  • top() – Get the top element.

  • empty() – Return whether the stack is empty.

示例:

MyStack stack = new MyStack();
stack.push(1);
stack.push(2); 
stack.top(); // 返回 2
stack.pop(); // 返回 2
stack.empty(); // 返回 false

注意:

  • 你只能使用隊列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 這些操作是合法的。

  • 你所使用的語言也許不支持隊列。 你可以使用 list 或者 deque(雙端隊列)來模擬一個隊列 , 只要是標準的隊列操作即可。

  • 你可以假設所有操作都是有效的(例如, 對一個空的棧不會調用 pop 或者 top 操作)

Notes:

  • You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.

  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.

  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

解題思路:

方法一(兩個隊列):

隊列先進后出,棧后進先出。用隊列實現(xiàn)棧,可以用兩個隊列完成題解:入棧時用 queue1 來存入節(jié)點;出棧時queue1 內節(jié)點順序出隊列并入隊列到 queue2,直到queue1剩最后一個元素時即為棧頂元素,彈出即可;用一個 top 指針一直指向棧頂元素,top() 方法查詢棧頂元素時直接返回 top 指針即可。

方法二(一個隊列):

只用一個隊列,只需要在入棧時反轉隊列即可:

入棧存入到隊列queue
節(jié)點1入棧:queue:1
反轉隊列0次:queue:1
節(jié)點2入棧queue:1->2
反轉隊列1次:
queue:1->2 --> queue:2->1
節(jié)點2入棧queue:2->1->3
反轉隊列2次:
queue:2->1->3 ---> queue:1->3->2 ---> queue:3->2->1
......

這樣不管什么時候出隊順序都是按照出棧的順序。

Java:

方法一

class MyStack {
 Queue<Integer> queue1;
 Queue<Integer> queue2;
 private int top;//指向棧頂元素
 public MyStack() {
 queue1 = new LinkedList<>();
 queue2 = new LinkedList<>();
 }
 public void push(int x) {
 queue1.offer(x);
 top = x;//新加入元素為棧頂元素
 }
 public int pop() {
 while (queue1.size() > 1) {//條件為隊列1的元素個數(shù)大于一
 top = queue1.poll();//用top暫存元素,當循環(huán)結束時,top剛好是棧頂元素
 queue2.add(top);
 }
 //隊列1與隊列2交換
 Queue<Integer> tmp = queue2;
 queue2 = queue1;
 queue1 = tmp;
 return queue2.poll();//返回隊列2的隊列頭元素,隊列2也只有一個元素
 }
 public int top() {
 return top;
 }
 public boolean empty() {
 return queue1.isEmpty();//隊列1決定了棧是否為空
 }
}

方法二:

每次入隊時反轉隊列即可,只有入棧需要特殊操作,出棧、取棧頂元素、是否空都按照隊列正常出隊列、取隊列頭元素、是否空方法操作。下面是入棧時代碼:

Queue<Integer> queue = new LinkedList<>();
public void push(int x) {
 queue.add(x);
 int sz = queue.size();//獲得隊列長度
 while (sz > 1) {//反轉次數(shù)為隊列長度減一
 queue.add(queue.remove());//反轉
 sz--;
 }
}

Python:

Python語言沒有棧和隊列數(shù)據(jù)結構,只能用數(shù)組 List 或雙端隊列 deque 實現(xiàn)。

這類編程語言就壓根不需要 用隊列實現(xiàn)?;蛴脳崿F(xiàn)隊列這種問題,因為棧和隊列本身就必須借助List、deque實現(xiàn)。

所以這道題在這種語言中這就非常簡單了,可以說是作弊。

class MyStack:
 def __init__(self):
 self.stack = []
 def push(self, x: int) -> None:
 self.stack.append(x)
 def pop(self) -> int:
 return self.stack.pop(-1)
 def top(self) -> int:
 return self.stack[-1]
 def empty(self) -> bool:
 return not self.stack

關于Python中怎么用隊列實現(xiàn)棧就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI