1. 程式人生 > >資料結構----Java中棧與佇列相互實現

資料結構----Java中棧與佇列相互實現

棧:先進後出;佇列:先進先出,FIFO

利用兩個佇列實現棧的功能

//利用兩個佇列實現棧
import java.util.Queue;
import java.util.LinkedList;

public class QueueToStack{
    Queue<Integer> queue1 = new LinkedList<Integer>();
    Queue<Integer> queue2 = new LinkedList<Integer>();
    
    //佇列模擬進棧
    public static void push(int item){
        if(queue1.isEmpty() && queue2.isEmpty()){
            queue1.add(item);
            return;
        }
        if(queue1.isEmpty()){
            queue2.add(item);
            return;
        }
        if(queue2.isEmpty()){
            queue1.add(item);
            return;
        }    
    }
    
    //佇列模擬出棧
    public static int pop(){
        if(queue1.isEmpty() && queue2.isEmpty()){
            try{
                throw new Exception("棧為空");
            }catch(Exception e){
                e.printStackTrace();
            }
        }    
        if(queue1.isEmpty()){
            while(queue2.size() > 1){
                queue1.add(queue2.poll());
            }
            return queue2.poll();
        }
        if(queue2.isEmpty()){
            while(queue1.size() > 1){
                queue2.add(queue1.poll());
            }
            return queue1.poll();
        }
        return 0;
    }
}

利用兩個棧實現佇列的功能

//利用兩個棧實現佇列的功能
import java.util.Stack;
public class StackToQueue{
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
    //棧實現元素進佇列
    public static void add(int item){
        if(stack1.isEmpty() && stack2.isEmpty()){
            stack1.push(item);
            return;
        }
        if(stack1.isEmpty()){
            stack2.push(item);
            return;
        }
        if(stack2.isEmpty()){
            stack1.push(item);
            return;
        }
    }

    //棧實現元素出佇列
    public static void poll(){
        if(stack1.isEmpty() && stack2.isEmpty()){
            try{
                throw new Exception("佇列為空");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        if(stack1.isEmpty()){
            while(stack2.size() > 1){
                stack1.push(stack2.pop());
            }
            return stack2.pop();
        }
        if(stack2.isEmpty()){
            while(stack1.size() > 1){
                stack2.push(stack1.pop());
            }
            return stack1.pop();
        }
    }
}

相關推薦

資料結構----Java佇列相互實現

棧:先進後出;佇列:先進先出,FIFO 利用兩個佇列實現棧的功能 //利用兩個佇列實現棧 import java.util.Queue; import java.util.LinkedList;

資料結構實驗之佇列二:一般算術表示式轉換成字尾式(SDUT 2132)

題目連結 #include <bits/stdc++.h> using namespace std; typedef long long ll; int ok(char ch, char sh) { if(sh == '(')return 1; if((ch ==

資料結構實驗之佇列六:下一較大值(二)(SDUT 3333)

#include <bits/stdc++.h> using namespace std; int a[1000006]; int b[1000006]; int sta[100006]; int main() { int t,n,i,j,top; while(~sc

資料結構實驗之佇列五:下一較大值(一)(SDUT 3332)

#include <bits/stdc++.h> using namespace std; int a[1005]; int main() { int t,n,i,j; while(~scanf("%d",&t)) { while(t-

資料結構實驗之佇列三:字尾式求值(SDUT 2133)

題解:把每一步計算的答案再存在棧裡面,直到計算結束。           如果是運算元 那麼直接入棧;如果是運算子,那麼把棧裡面最頂部的兩個運算元拿出來進行運算,運算結果再放入到棧裡面,計算完所有的(#

資料結構實驗之佇列四:括號匹配(SDUT 2134)

#include <bits/stdc++.h> using namespace std; typedef long long ll; char s[100]; char a[100]; int main() { int i,j,k,f,top,len; while(

資料結構實驗之佇列五:下一較大值(一)

資料結構實驗之棧與佇列五:下一較大值(一) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 對於包含n(1<=n<=1000)個整數的序列,對於序列中的每一元素,

資料結構實驗之佇列五:下一較大值(一,二)

資料結構實驗之棧與佇列五:下一較大值(一,二) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 對於包含n(1<=n<=1000)個整數的序列,對於序

【大話資料結構】04 佇列 筆記

《大話資料結構》 ——程傑 共463頁 筆記圈點主要內容,也請多多支援大話資料結構該書作者。 第 4 章 棧與佇列 111頁_定義 棧是限定在表尾進行插入和刪除操作的線性表。 佇列是隻允

資料結構實驗之佇列七:出序列判定

Problem Description 給一個初始的入棧序列,其次序即為元素的入棧次序,棧頂元素可以隨時出棧,每個元素只能入棧依次。輸入一個入棧序列,後面依次輸入多個序列,請判斷這些序列是否為所給入棧序列合法的出棧序列。 例如序列1,2,3,4,5是某棧的壓入順序,序列4

資料結構實驗之佇列六:下一較大值(二)(因為資料量大所以用來操作)

資料結構實驗之棧與佇列六:下一較大值(二) Time Limit: 150 ms Memory Limit: 8000 KiB Problem Description 對於包含n(1<=n<=100000)個整數的序列,對於序列中的每一元素,在序列中查詢

資料結構實驗之佇列八:的基本操作

Problem Description 堆疊是一種基本的資料結構。堆疊具有兩種基本操作方式,push 和 pop。push一個值會將其壓入棧頂,而 pop 則會將棧頂的值彈出。現在我們就來驗證一下堆疊的

資料結構實驗之佇列九:行編輯器

Problem Description 一個簡單的行編輯程式的功能是:接受使用者從終端輸入的程式或資料,並存入使用者的資料區。 由於使用者在終端上進行輸入時,不能保證不出差錯,因此,若在編輯程式中,“每接受一個字元即存入使用者資料區”的做法顯然不是最恰當的。較好

資料結構實驗之佇列十:走迷宮

Problem Description 一個由n * m 個格子組成的迷宮,起點是(1, 1), 終點是(n, m),每次可以向上下左右四個方向任意走一步,並且有些格子是不能走動,求從起點到終點經過每個

資料結構實驗之佇列六:下一較大值(二)

Time Limit: 150 ms Memory Limit: 8000 KiB Problem Description 對於包含n(1<=n<=100000)個整數的序列,對於序列中的每一元素,在序列中查詢其位置之後第一個大於它的值,如果找到,輸出所找到的

資料結構實驗之佇列一:進位制轉換(SDUT 2131)

題目連結 題解: 特判一下n==0的時候。 #include <bits/stdc++.h> using namespace std; int a[1000]; int main() {

資料結構實驗之佇列一:進位制轉換

Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 輸入一個十進位制非負整數,將其轉換成對應的 R (2 <= R <= 9) 進位制數,並

資料結構實驗之佇列八:的基本操作(new)

#include <stdio.h> #include <stdlib.h> typedef struct node { int *base; int *top; int stacksize; } sqstack;

[OJ.2131]資料結構實驗之佇列一:進位制轉換

                                   資料結構實驗之棧與佇列一:進位制轉換                                            Time Limit: 1000 ms                     

【OJ.2132】資料結構實驗之佇列二:一般算術表示式轉換成字尾式

                            資料結構實驗之棧與佇列二:一般算術表示式轉換成字尾式                              Time Limit: 1000 ms