数据结构与算法之栈入门题目
生活随笔
收集整理的這篇文章主要介紹了
数据结构与算法之栈入门题目
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據結構與算法之棧題目
目錄
用數組實現大小固定的隊列和棧
實現一個特殊的棧,在實現棧的基礎功能上,再實現返回棧中最小元素的操作
如果僅用棧結構實現隊列結構和如何僅用隊列結構實現棧結構
1. 用數組實現大小固定的隊列和棧
(一)數組實現大小固定的棧思路
(二)數組實現大小固定的隊列思路
(三)代碼實現
package class_03;public class Array_To_Stack_Queue {public static class ArrayStack {private Integer[] arr;private Integer size;public ArrayStack(int initSize) {if (initSize < 0) {throw new IllegalArgumentException("The init size is less than 0");}arr = new Integer[initSize];size = 0;}public void push(int obj) {if (size == arr.length) {throw new ArrayIndexOutOfBoundsException("The queue is full");}arr[size++] = obj;}public Integer peek() {if (size == 0) {throw new ArrayIndexOutOfBoundsException("The queue is null");}return arr[size - 1];}public Integer pop() {if (size == 0) {throw new ArrayIndexOutOfBoundsException("The queue is null");}return arr[--size];}}public static class ArrayQueue {private Integer start;private Integer end;private Integer size;private Integer[] arr;public ArrayQueue(int initSize) {if (initSize < 0) {System.out.println("initSize不能小于0");return;}start = 0;end = 0;size = 0;arr = new Integer[initSize];}public void push(int num) {if (size == arr.length) {System.out.println("隊列滿");}size++;arr[end] = num;end = end == arr.length - 1 ? 0 : end + 1;}public Integer pop() {if (size == 0) {return null;}size--;int temp = arr[start];start = start == arr.length - 1 ? 0 : start + 1;return temp;}public Integer peek() {if (size == 0) {return null;}return arr[start];}}public static void main(String[] args) {} }2. 實現一個特殊的棧,在實現棧的基礎功能上,再實現返回棧中最小元素的操作
(一)思路分析
以myStack2為例
(二)代碼實現
public class GetMinStack {public static class MyStack1 {private Stack<Integer> stackData;private Stack<Integer> stackMin;public MyStack1() {this.stackData = new Stack<Integer>();this.stackMin = new Stack<Integer>();}public void push(int newNum) {if (this.stackMin.isEmpty()) {this.stackMin.push(newNum);} else if (newNum <= this.getmin()) {this.stackMin.push(newNum);}this.stackData.push(newNum);}public int pop() {if (this.stackData.isEmpty()) {throw new RuntimeException("Your stack is empty.");}int value = this.stackData.pop();if (value == this.getmin()) {this.stackMin.pop();}return value;}public int getmin() {if (this.stackMin.isEmpty()) {throw new RuntimeException("Your stack is empty.");}return this.stackMin.peek();}}public static class MyStack2 {private Stack<Integer> stackData;private Stack<Integer> stackMin;public MyStack2() {this.stackData = new Stack<Integer>();this.stackMin = new Stack<Integer>();}public void push(int newNum) {if (this.stackMin.isEmpty()) {this.stackMin.push(newNum);} else if (newNum < this.getmin()) {this.stackMin.push(newNum);} else {int newMin = this.stackMin.peek();this.stackMin.push(newMin);}this.stackData.push(newNum);}public int pop() {if (this.stackData.isEmpty()) {throw new RuntimeException("Your stack is empty.");}this.stackMin.pop();return this.stackData.pop();}public int getmin() {if (this.stackMin.isEmpty()) {throw new RuntimeException("Your stack is empty.");}return this.stackMin.peek();}}public static void main(String[] args) {MyStack1 stack1 = new MyStack1();stack1.push(3);System.out.println(stack1.getmin());stack1.push(4);System.out.println(stack1.getmin());stack1.push(1);System.out.println(stack1.getmin());System.out.println(stack1.pop());System.out.println(stack1.getmin());System.out.println("=============");MyStack1 stack2 = new MyStack1();stack2.push(3);System.out.println(stack2.getmin());stack2.push(4);System.out.println(stack2.getmin());stack2.push(1);System.out.println(stack2.getmin());System.out.println(stack2.pop());System.out.println(stack2.getmin());} }3. 如果僅用棧結構實現隊列結構和如何僅用隊列結構實現棧結構
思路
(一)用棧結構實現隊列結構
(二)用隊列結構實現棧結構
4. 由隊列先進先出,棧后進先出知道。我們可以用兩個隊列實現棧。創建兩個隊列分別為queue,help,queue隊列用來存放元素,help用來輔助。
5. 當執行push操作時,直接添加到queue隊列即可。
6. 當執行pop,peek操作時,把queue隊列中的元素移到help棧,queue只剩下一個即為要返回的數據。
(三)代碼實現
import java.util.LinkedList; import java.util.Queue; import java.util.Stack;public class Code_03_StackAndQueueConvert {public static class TwoStacksQueue {private Stack<Integer> stackPush;private Stack<Integer> stackPop;public TwoStacksQueue() {stackPush = new Stack<Integer>();stackPop = new Stack<Integer>();}public void push(int pushInt) {stackPush.push(pushInt);}public int poll() {if (stackPop.empty() && stackPush.empty()) {throw new RuntimeException("Queue is empty!");} else if (stackPop.empty()) {while (!stackPush.empty()) {stackPop.push(stackPush.pop());}}return stackPop.pop();}public int peek() {if (stackPop.empty() && stackPush.empty()) {throw new RuntimeException("Queue is empty!");} else if (stackPop.empty()) {while (!stackPush.empty()) {stackPop.push(stackPush.pop());}}return stackPop.peek();}}public static class TwoQueuesStack {private Queue<Integer> queue;private Queue<Integer> help;public TwoQueuesStack() {queue = new LinkedList<Integer>();help = new LinkedList<Integer>();}public void push(int pushInt) {queue.add(pushInt);}public int peek() {if (queue.isEmpty()) {throw new RuntimeException("Stack is empty!");}while (queue.size() != 1) {help.add(queue.poll());}int res = queue.poll();help.add(res);swap();return res;}public int pop() {if (queue.isEmpty()) {throw new RuntimeException("Stack is empty!");}while (queue.size() > 1) {help.add(queue.poll());}int res = queue.poll();swap();return res;}private void swap() {Queue<Integer> tmp = help;help = queue;queue = tmp;}}}總結
以上是生活随笔為你收集整理的数据结构与算法之栈入门题目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构与算法之基数排序
- 下一篇: 数据结构与算法之猫狗队列