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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

算法练习day8——190326(队列实现栈、栈实现队列)

發(fā)布時間:2024/10/14 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 算法练习day8——190326(队列实现栈、栈实现队列) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.僅用隊列結(jié)構(gòu)實現(xiàn)棧結(jié)構(gòu)

1.1 分析:

1、所有數(shù)先入data隊列:

2、將前n-1個數(shù)入help隊列,彈出最后一個數(shù):

3、將help中的前n-2個數(shù)入data隊列,彈出最后一個數(shù):

4、重復(fù)2~3,即可按先入后出的順序彈出所有元素。

1.2 代碼實現(xiàn)

package Solution; import java.util.LinkedList; import java.util.Queue;class queueToStack{Queue<Integer> data;Queue<Integer> help;public queueToStack() {this.data=new LinkedList<Integer>();this.help=new LinkedList<Integer>();}public void push(int num) {data.add(num);}public Integer peek() {if (data.isEmpty()) {throw new RuntimeException("Stack is empty!");}while(data.size()>1)help.add(data.poll());int result=data.poll();help.add(result);//只返回不刪除swap();return result;}public Integer pop() {if (data.isEmpty()) {throw new RuntimeException("Stack is empty!");}while(data.size()>1)help.add(data.poll());int result=data.poll();//返回帶刪除swap();return result;}public boolean isEmpty() {return data.isEmpty();}public void swap() {//改變引用Queue<Integer> temp=data;data=help;help=temp;} }public class Queue_To_Stack {public static void main(String[] args) {queueToStack qts=new queueToStack();qts.push(1);qts.push(2);System.out.println(qts.pop());qts.push(3);System.out.println(qts.pop());System.out.println(qts.pop());qts.push(4);qts.push(5);System.out.println(qts.pop());} }

運行結(jié)果:

注意:隊列的先入先出!!!

2.僅用棧結(jié)構(gòu)實現(xiàn)隊列結(jié)構(gòu)

2.1 分析

用兩個棧:push棧,pop棧

  • push棧:用于入隊列
  • pop棧:用于出隊列

兩種思想:

一種思想(倒的比較頻繁):

要入隊列時,都往push棧加;

出隊列時,將push棧中的數(shù)據(jù)倒入pop棧中,從pop棧頂彈出一個元素;然后將剩余數(shù)據(jù)倒回到push棧中。

另一種思想:

push往pop倒數(shù)據(jù),需滿足:

  • 要倒就把push中的數(shù)據(jù)倒空;
  • 在pop棧中還有數(shù)據(jù)時,就不能倒
  • 2.2 代碼實現(xiàn)

    package Solution;import java.util.Stack;class StackToQueue{Stack<Integer> pushStack;Stack<Integer> popStack;public StackToQueue(){this.pushStack=new Stack<Integer>();this.popStack=new Stack<Integer>();}public void push(int num) {pushStack.push(num);}public Integer peek() {if (pushStack.isEmpty()) {throw new RuntimeException("Queue is empty!");}while(!pushStack.isEmpty())popStack.push(pushStack.pop());int result=popStack.peek();while(!popStack.isEmpty())pushStack.push(popStack.pop());return result;}public Integer poll() {if (pushStack.isEmpty()) {throw new RuntimeException("Queue is empty!");}while(!pushStack.isEmpty())popStack.push(pushStack.pop());int result=popStack.pop();//彈出,少一個while(!popStack.isEmpty())pushStack.push(popStack.pop());return result;} } public class Stack_To_Queue {public static void main(String[] args) {StackToQueue stq=new StackToQueue();stq.push(1);stq.push(2);System.out.println(stq.poll());stq.push(3);System.out.println(stq.poll());System.out.println(stq.poll());stq.push(4);stq.push(5);System.out.println(stq.poll());} }

    運行結(jié)果:

    ?

    總結(jié)

    以上是生活随笔為你收集整理的算法练习day8——190326(队列实现栈、栈实现队列)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。