栈&队列

1、用两个栈实现队列

 

add:压入栈后pushToPop

poll:pushToPop后弹出栈

peek:pushToPop后得到队首元素

 1 public class TwoStacksQueue {
 2         public Stack<Integer> stackPush;
 3         public Stack<Integer> stackPop;
 4 
 5         public TwoStacksQueue() {
 6             stackPush = new Stack<Integer>();
 7             stackPop = new Stack<Integer>();
 8         }
 9 
10         private void pushToPop() {
11             if (stackPop.empty()) {
12                 while (!stackPush.empty()) {
13                     stackPop.push(stackPush.pop());
14                 }
15             }
16         }
17 
18         public void add(int pushInt) {
19             stackPush.push(pushInt);
20             pushToPop();
21         }
22 
23         public int poll() {
24             if (stackPop.empty() && stackPush.empty()) {
25                 throw new RuntimeException("Queue is empty!");
26             }
27             pushToPop();
28             return stackPop.pop();
29         }
30 
31         public int peek() {
32             if (stackPop.empty() && stackPush.empty()) {
33                 throw new RuntimeException("Queue is empty!");
34             }
35             pushToPop();
36             return stackPop.peek();
37         }
38 }

 

2、用一个队列实现栈

 

push:入队列后把前面的n个元素依次换到后面去

pop:把队首元素弹出

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

 

3、用两个队列实现栈

 

push:入队queue

pop:将队列的前面元素依次换到help队列里面去(只保留一个元素在queue队列),弹出queue队列唯一的元素res,交换队列指针并返回res

peek:将队列的前面元素依次换到help队列里面去(只保留一个元素在queue队列),弹出queue队列唯一的元素res然后将其入help队列,交换队列指针并返回res

 1 public static class TwoQueuesStack {
 2         private Queue<Integer> queue;
 3         private Queue<Integer> help;
 4 
 5         public TwoQueuesStack() {
 6             queue = new LinkedList<Integer>();
 7             help = new LinkedList<Integer>();
 8         }
 9 
10         public void push(int pushInt) {
11             queue.add(pushInt);
12         }
13 
14         public int peek() {
15             if (queue.isEmpty()) {
16                 throw new RuntimeException("Stack is empty!");
17             }
18             while (queue.size() != 1) {
19                 help.add(queue.poll());
20             }
21             int res = queue.poll();
22             help.add(res);
23             swap();
24             return res;
25         }
26 
27         public int pop() {
28             if (queue.isEmpty()) {
29                 throw new RuntimeException("Stack is empty!");
30             }
31             while (queue.size() > 1) {
32                 help.add(queue.poll());
33             }
34             int res = queue.poll();
35             swap();
36             return res;
37         }
38 
39         private void swap() {
40             Queue<Integer> tmp = help;
41             help = queue;
42             queue = tmp;
43         }
44 
45 }

 

4、设计一个有 getMin 功能的栈

 

法一:

两个栈:data和min,待压入的数据:newnum

push:对于data,压入;对于min,当newnum<=min.top()或min栈空,压入

pop:对于data,弹出栈顶元素value;对于min,当value=min.top(),弹出,当value>min.top(),不弹出。返回value

getMin:返回min.top()

 1 public static class MyStack1 {
 2         private Stack<Integer> stackData;
 3         private Stack<Integer> stackMin;
 4 
 5         public MyStack1() {
 6             this.stackData = new Stack<Integer>();
 7             this.stackMin = new Stack<Integer>();
 8         }
 9 
10         public void push(int newNum) {
11             if (this.stackMin.isEmpty()) {
12                 this.stackMin.push(newNum);
13             } else if (newNum <= this.getmin()) {
14                 this.stackMin.push(newNum);
15             }
16             this.stackData.push(newNum);
17         }
18 
19         public int pop() {
20             if (this.stackData.isEmpty()) {
21                 throw new RuntimeException("Your stack is empty.");
22             }
23             int value = this.stackData.pop();
24             if (value == this.getmin()) {
25                 this.stackMin.pop();
26             }
27             return value;
28         }
29 
30         public int getmin() {
31             if (this.stackMin.isEmpty()) {
32                 throw new RuntimeException("Your stack is empty.");
33             }
34             return this.stackMin.peek();
35         }
36 }

 

法二:

两个栈:data和min,待压入的数据:newnum

push:对于data,压入;对于min,当newnum<min.top()或栈空,压入newnum,当newnum>=min.top(),压入min.top()

pop:对于min,弹出;对于data,弹出并返回

getMin:返回min.top()

 1 public static class MyStack2 {
 2         private Stack<Integer> stackData;
 3         private Stack<Integer> stackMin;
 4 
 5         public MyStack2() {
 6             this.stackData = new Stack<Integer>();
 7             this.stackMin = new Stack<Integer>();
 8         }
 9 
10         public void push(int newNum) {
11             if (this.stackMin.isEmpty()) {
12                 this.stackMin.push(newNum);
13             } else if (newNum < this.getmin()) {
14                 this.stackMin.push(newNum);
15             } else {
16                 int newMin = this.stackMin.peek();
17                 this.stackMin.push(newMin);
18             }
19             this.stackData.push(newNum);
20         }
21 
22         public int pop() {
23             if (this.stackData.isEmpty()) {
24                 throw new RuntimeException("Your stack is empty.");
25             }
26             this.stackMin.pop();
27             return this.stackData.pop();
28         }
29 
30         public int getmin() {
31             if (this.stackMin.isEmpty()) {
32                 throw new RuntimeException("Your stack is empty.");
33             }
34             return this.stackMin.peek();
35         }
36 }
View Code

 

法三:

 1 class MinStack {
 2         stack<int> x_stack;
 3         stack<int> min_stack;
 4     public:
 5         MinStack() {
 6             min_stack.push(INT_MAX);
 7         }
 8 
 9         void push(int x) {
10             int min = min_stack.top()<x? min_stack.top() : x;
11             x_stack.push(x);
12             min_stack.push(min);
13         }
14 
15         void pop() {
16             x_stack.pop();
17             min_stack.pop();
18         }
19 
20         int top() {
21             return x_stack.top();
22         }
23 
24         int min() {
25             return min_stack.top();
26         }
27 };
View Code

 

 

5、验证栈的压入、弹出序列

 

压入一个,弹出若干

 1 public boolean validateStackSequences(int[] pushed, int[] popped) {
 2     Stack<Integer> stack = new Stack<>();
 3     int i = 0;
 4     for(int num : pushed) {
 5         stack.push(num); // num 入栈
 6         while(!stack.isEmpty() && stack.peek() == popped[i]) { // 循环判断与出栈
 7             stack.pop();
 8             i++;
 9         }
10     }
11     return stack.isEmpty();
12 }

 

 

6、用一个栈实现另一个栈的排序

 

想模型:设置一个主栈stack,一个辅助站help,将元素从:stack->help->stack

 1 public static void sortStackByStack(Stack<Integer> stack) {
 2     Stack<Integer> help = new Stack<Integer>();
 3     while (!stack.isEmpty()) {
 4         int cur = stack.pop();
 5         while (!help.isEmpty() && help.peek() < cur) {
 6             stack.push(help.pop());
 7         }
 8         help.push(cur);
 9     }
10     while (!help.isEmpty()) {
11         stack.push(help.pop());
12     }
13 }

 

7、仅用递归函数和栈操作逆序一个栈

 

 1 public static int getAndRemoveLastElement(Stack<Integer> stack) {
 2     int result = stack.pop();
 3     if (stack.isEmpty()) {
 4         return result;
 5     } else {
 6         int last = getAndRemoveLastElement(stack);
 7         stack.push(result);
 8         return last;
 9     }
10 }
11 public static void reverse(Stack<Integer> stack) {
12     if (stack.isEmpty()) {
13         return;
14     }
15     int i = getAndRemoveLastElement(stack);
16     reverse(stack);
17     stack.push(i);
18 }

 

 

8、逆波兰表达式求值(后缀表达式->中缀表达式->求值)

 

 1 public int evalRPN(String[] tokens) {
 2     Stack<Integer> stack = new Stack<>();
 3     int a = 0, b = 0;
 4     for (String val:tokens) {
 5         if ("+-*/".contains(val)) {
 6             a = stack.pop();
 7             b = stack.pop();
 8         }
 9         switch(val) {
10             case "+": stack.push(b + a);break;
11             case "-": stack.push(b - a);break;
12             case "*": stack.push(b * a);break;
13             case "/": stack.push(b / a);break;
14             default : stack.push(new Integer(val));
15         } 
16     }
17     return stack.pop();
18 }

 

posted on 2020-08-05 21:46  小小字节  阅读(106)  评论(0编辑  收藏  举报