日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构与算法之栈入门题目

發布時間:2024/2/28 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构与算法之栈入门题目 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數據結構與算法之棧題目


目錄

  • 用數組實現大小固定的隊列和棧

  • 實現一個特殊的棧,在實現棧的基礎功能上,再實現返回棧中最小元素的操作

  • 如果僅用棧結構實現隊列結構和如何僅用隊列結構實現棧結構


  • 1. 用數組實現大小固定的隊列和棧

    (一)數組實現大小固定的棧思路
  • 創建一個變量size指向數組0位置上
  • 當執行push操作時將添加的num加到size指的位置上,然后size++
  • 當執行peek操作時只需要返回 size-1 位置上的數。
  • 當執行pop操作時,返回 --size 上的數即可。
  • (二)數組實現大小固定的隊列思路
  • 創建三個變量size,start,end 指向數組0位置上。其中size記錄隊列中的元素,start執行隊列的第一個元素,end指向下一個可以添加元素的位置
  • 當執行push操作時,size++,將元素添加到end指向的位置。然后判斷end是否溢出,即數組長度為3,end執行了第4個位置時候,將end置為0,即end = end == arr.length - 1 ? 0 : end + 1;
  • 當執行pop操作時,size–,準備個臨時變量保存要返回的隊列頭元素,然后判斷start+1后是否超過最后一個位置,超過置為0,即start = start == arr.length - 1 ? 0 : start + 1;,返回臨時變量即可。
  • 當執行peek操作時,返回start指向的元素即可。
  • (三)代碼實現
    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為例

  • 準備兩個棧,一個用來添加數據stackData,另外一個用來存放最小值stackMin
  • 當執行push操作時,如果最小棧為null,則直接將添加元素添加到最小棧,否則peek出最小棧中的元素和添加元素相比,當添加元素小時則添加進stackMin,否則添加peek出的元素。
  • 當執行pop操作時,直接彈出stackMin和stackData棧頂元素
  • (二)代碼實現
    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. 如果僅用棧結構實現隊列結構和如何僅用隊列結構實現棧結構

    思路
    (一)用棧結構實現隊列結構

  • 由隊列先進先出,棧后進先出知道。我們可以用兩個棧實現隊列。創建兩個棧,分別為stackPush(用來添加元素),stackPop(用來彈出元素)
  • 當執行push操作時,直接把元素添加進stackPush棧即可。
  • 當執行poll,peek操作時,當stackPop為null,且stackPush不為null時,將stackPush元素全部添加到stackPop里,然后返回棧頂元素即可。
  • (二)用隊列結構實現棧結構
    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;}}}

    總結

    以上是生活随笔為你收集整理的数据结构与算法之栈入门题目的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。