栈和队列相互模拟

1、用队列模拟栈

python

 1 class MyStack:
 2 
 3     def __init__(self):
 4         """
 5         Initialize your data structure here.
 6         """
 7         self.queue1 = collections.deque()
 8         self.queue2 = collections.deque()
 9 
10 
11     def push(self, x: int) -> None:
12         """
13         Push element x onto stack.
14         """
15         self.queue2.append(x)
16         while self.queue1:
17             self.queue2.append(self.queue1.popleft())
18         self.queue1, self.queue2 = self.queue2, self.queue1
19 
20 
21     def pop(self) -> int:
22         """
23         Removes the element on top of the stack and returns that element.
24         """
25         return self.queue1.popleft()
26 
27 
28     def top(self) -> int:
29         """
30         Get the top element.
31         """
32         return self.queue1[0]
33 
34 
35     def empty(self) -> bool:
36         """
37         Returns whether the stack is empty.
38         """
39         return not self.queue1

 

java

 1 class MyStack {
 2     Queue<Integer> queue1;
 3     Queue<Integer> queue2;
 4 
 5     /** Initialize your data structure here. */
 6     public MyStack() {
 7         queue1 = new LinkedList<Integer>();
 8         queue2 = new LinkedList<Integer>();
 9     }
10     
11     /** Push element x onto stack. */
12     public void push(int x) {
13         queue2.offer(x);
14         while (!queue1.isEmpty()) {
15             queue2.offer(queue1.poll());
16         }
17         Queue<Integer> temp = queue1;
18         queue1 = queue2;
19         queue2 = temp;
20     }
21     
22     /** Removes the element on top of the stack and returns that element. */
23     public int pop() {
24         return queue1.poll();
25     }
26     
27     /** Get the top element. */
28     public int top() {
29         return queue1.peek();
30     }
31     
32     /** Returns whether the stack is empty. */
33     public boolean empty() {
34         return queue1.isEmpty();
35     }
36 }

 

GO

 1 type MyStack struct {
 2     queue1, queue2 []int
 3 }
 4 
 5 /** Initialize your data structure here. */
 6 func Constructor() (s MyStack) {
 7     return
 8 }
 9 
10 /** Push element x onto stack. */
11 func (s *MyStack) Push(x int) {
12     s.queue2 = append(s.queue2, x)
13     for len(s.queue1) > 0 {
14         s.queue2 = append(s.queue2, s.queue1[0])
15         s.queue1 = s.queue1[1:]
16     }
17     s.queue1, s.queue2 = s.queue2, s.queue1
18 }
19 
20 /** Removes the element on top of the stack and returns that element. */
21 func (s *MyStack) Pop() int {
22     v := s.queue1[0]
23     s.queue1 = s.queue1[1:]
24     return v
25 }
26 
27 /** Get the top element. */
28 func (s *MyStack) Top() int {
29     return s.queue1[0]
30 }
31 
32 /** Returns whether the stack is empty. */
33 func (s *MyStack) Empty() bool {
34     return len(s.queue1) == 0
35 }

 

C++

 1 class MyStack {
 2 public:
 3     queue<int> queue1;
 4     queue<int> queue2;
 5 
 6     /** Initialize your data structure here. */
 7     MyStack() {
 8 
 9     }
10 
11     /** Push element x onto stack. */
12     void push(int x) {
13         queue2.push(x);
14         while (!queue1.empty()) {
15             queue2.push(queue1.front());
16             queue1.pop();
17         }
18         swap(queue1, queue2);
19     }
20     
21     /** Removes the element on top of the stack and returns that element. */
22     int pop() {
23         int r = queue1.front();
24         queue1.pop();
25         return r;
26     }
27     
28     /** Get the top element. */
29     int top() {
30         int r = queue1.front();
31         return r;
32     }
33     
34     /** Returns whether the stack is empty. */
35     bool empty() {
36         return queue1.empty();
37     }
38 };

 

 

2、用栈模拟队列

python

 1 class MyQueue(object):
 2 
 3     def __init__(self):
 4         self.stack1 = []
 5         self.stack2 = []
 6 
 7     def push(self, x):
 8         """
 9         :type x: int
10         :rtype: None
11         """
12         self.stack1.append(x)
13 
14     def pop(self):
15         """
16         :rtype: int
17         """
18         if self.stack2:
19             return self.stack2.pop()
20         while self.stack1:
21             x = self.stack1.pop()
22             self.stack2.append(x)
23         return self.stack2.pop()
24 
25     def peek(self):
26         """
27         :rtype: int
28         """
29         if self.stack2:
30             return self.stack2[-1]
31 
32         while self.stack1:
33             x = self.stack1.pop()
34             self.stack2.append(x)
35         return self.stack2[-1]
36 
37     def empty(self):
38         """
39         :rtype: bool
40         """
41         return not self.stack1 and not self.stack2

 

java

 1 class MyQueue {
 2     Deque<Integer> inStack;
 3     Deque<Integer> outStack;
 4 
 5     public MyQueue() {
 6         inStack = new LinkedList<Integer>();
 7         outStack = new LinkedList<Integer>();
 8     }
 9     
10     public void push(int x) {
11         inStack.push(x);
12     }
13     
14     public int pop() {
15         if (outStack.isEmpty()) {
16             in2out();
17         }
18         return outStack.pop();
19     }
20     
21     public int peek() {
22         if (outStack.isEmpty()) {
23             in2out();
24         }
25         return outStack.peek();
26     }
27     
28     public boolean empty() {
29         return inStack.isEmpty() && outStack.isEmpty();
30     }
31 
32     private void in2out() {
33         while (!inStack.isEmpty()) {
34             outStack.push(inStack.pop());
35         }
36     }
37 }

 

GO

 1 type MyQueue struct {
 2     inStack, outStack []int
 3 }
 4 
 5 func Constructor() MyQueue {
 6     return MyQueue{}
 7 }
 8 
 9 func (q *MyQueue) Push(x int) {
10     q.inStack = append(q.inStack, x)
11 }
12 
13 func (q *MyQueue) in2out() {
14     for len(q.inStack) > 0 {
15         q.outStack = append(q.outStack, q.inStack[len(q.inStack)-1])
16         q.inStack = q.inStack[:len(q.inStack)-1]
17     }
18 }
19 
20 func (q *MyQueue) Pop() int {
21     if len(q.outStack) == 0 {
22         q.in2out()
23     }
24     x := q.outStack[len(q.outStack)-1]
25     q.outStack = q.outStack[:len(q.outStack)-1]
26     return x
27 }
28 
29 func (q *MyQueue) Peek() int {
30     if len(q.outStack) == 0 {
31         q.in2out()
32     }
33     return q.outStack[len(q.outStack)-1]
34 }
35 
36 func (q *MyQueue) Empty() bool {
37     return len(q.inStack) == 0 && len(q.outStack) == 0
38 }

 

C++

 1 class MyQueue {
 2 private:
 3     stack<int> inStack, outStack;
 4 
 5     void in2out() {
 6         while (!inStack.empty()) {
 7             outStack.push(inStack.top());
 8             inStack.pop();
 9         }
10     }
11 
12 public:
13     MyQueue() {}
14 
15     void push(int x) {
16         inStack.push(x);
17     }
18 
19     int pop() {
20         if (outStack.empty()) {
21             in2out();
22         }
23         int x = outStack.top();
24         outStack.pop();
25         return x;
26     }
27 
28     int peek() {
29         if (outStack.empty()) {
30             in2out();
31         }
32         return outStack.top();
33     }
34 
35     bool empty() {
36         return inStack.empty() && outStack.empty();
37     }
38 };

 

posted @ 2021-12-28 23:33  r1-12king  阅读(56)  评论(0)    收藏  举报