1. 程式人生 > >java面向對象的棧 隊列 優先級隊列的比較

java面向對象的棧 隊列 優先級隊列的比較

數據 就會 對象 位置 static 插入 優先級隊列 尾指針 long

  棧 隊列 有序隊列數據結構的生命周期比那些數據庫類型的結構(比如鏈表,樹)要短得多。在程序操作執行期間他們才被創建,通常用他們去執行某項特殊的任務;當完成任務之後,他們就會被銷毀。這三個數據結構還有一個特點就是訪問是受到限制的,即在特定時刻只有一個數據項可以被讀取或者被刪除,但是所謂的移除並不是真的刪除,數據項依然在這些數據結構中,只不過因為指針已經指向其他數據項,沒有辦法訪問到,當添加新的數據項時,當初移除的數據項被替代從而永遠消失。

棧 隊列 優先級隊列的模擬思想

  1.棧:棧遵循先進後出(FILO)模式最後插入的數據項最先被移除,而且只能訪問最後的數據項,只有當移除這個數據項之後才能訪問倒數第二項的數據項。

  2.隊列:隊列遵循先進先出(FIFO)模式首先被插入的數據項最先被移除,而且只能訪問訪問第一個數據項,只有當移除這個數據項之後才能訪問第二項的數據項。特別的為了避免隊列不滿卻不能插入新數據項,可以讓隊頭隊尾指針繞回到數組開始的位置,這就是循環隊列。

  3.優先級隊列:優先級隊列和隊列的特性相同有隊頭和隊尾,除此之外在優先級隊列中,數據項按關鍵字的值有序,這樣關鍵字最小的數據項或最大的數據項(視情況而定)總是在隊頭,數據項插入的時候會按照順序插入到合適的位置以確保隊列的順序。

棧 隊列 優先級隊列的效率

  棧:插入操作需要O(1),刪除操作需要O(1)。

  隊列:插入操作需要O(1),刪除操作需要O(1)。

  優先級隊列:插入操作需要O(N),刪除操作需要O(1)。

棧 隊列 優先級隊列的指針

  棧:top= -1。

  隊列:rear = -1, front = 0。

  優先級隊列:nItems = 0。

棧 隊列 優先級隊列的java代碼

package sy;

import sy.Stack;

class Stack{
    //定義棧長度
    private int maxSize;
    //定義棧
    private long[] stackArray;
    //定義棧指針
    private int top;
    
//定義構造方法 public Stack(int n){ maxSize = n; stackArray = new long[maxSize]; top = -1; } //定義插入方法 public void push(long n){ //棧長度先增加,再向棧中壓入數據 stackArray[++top] = n; } //定義刪除方法 public long pop(){ //先向棧中壓入數據,棧長度再減少 return stackArray[top--]; } //定義查找方法 public long peek(){ return stackArray[top]; } //定義查空方法 public boolean isEmpty(){ return (top == -1); } //定義查滿方法 public boolean isFull(){ return (top == maxSize - 1); } } public class App { public static void main(String[] args){ Stack theStack = new Stack(10); theStack.push(20); theStack.push(40); theStack.push(60); theStack.push(80); while(!theStack.isEmpty()) { long value = theStack.pop(); System.out.print(value); System.out.print(" "); } System.out.println(""); } }

隊列(帶nItems)

package sy;

class Queue{
    //定義隊列長度
    private int maxSize;
    //定義隊列
    private long[] queArray;
    //定義隊首指針
    private int front;
    //定義隊尾指針
    private int rear;
    //定義數據項個數
    private int nItems;
    
    //定義構造方法
    public Queue(int n){
        maxSize = n;
        queArray = new long[maxSize];
        front = 0;
        rear = -1;
        nItems = 0;
    }
    //定義插入方法
    public void insert(long n){
        //判斷是否循環至隊首
        if(rear == maxSize - 1)
        {
            rear = -1;
        }
        //先自增再賦值
        queArray[++rear] = n;
        nItems++;    
    }
    //定義刪除方法
    public long remove(){
        //先賦值在自增
        long temp = queArray[front++];
        //判斷是否循環至隊首
        if(front == maxSize)
        {
            front = 0;
        }
        nItems--;
        return temp;
    }
    //定義查找首項方法
    public long peekFront(){
        return queArray[front];
    }
    
    public boolean isEmpty(){
        return (nItems == 0);
    }
    
    public boolean isFull(){
        return (nItems == maxSize);
    }
    
    public int size(){
        return nItems;
    }
}


public class App {
    public static void main(String[] args){
        Queue theQueue = new Queue(5);
        
        theQueue.insert(10);
        theQueue.insert(20);
        theQueue.insert(30);
        theQueue.insert(40);
        theQueue.remove();
        theQueue.remove();
        theQueue.remove();
        theQueue.insert(50);
        theQueue.insert(60);
        theQueue.insert(70);
        theQueue.insert(80);
        
        while(!theQueue.isEmpty())
        {
            long n = theQueue.remove();
            System.out.print(n);
            System.out.print(" ");
            
        }
        System.out.print("");
        
    }
}

註意:insert()和remove方法中分別遞增和遞減了nItems,當處理大量的插入和移除操作的時候,就會影響性能。

隊列(沒有nItems)

package sy;

//import aa.Queue;

class Queue{
    //定義隊列長度
    private int maxSize;
    //定義隊列
    private long[] queArray;
    //定義隊首指針
    private int front;
    //定義隊尾指針
    private int rear;
    
    //定義構造方法
    public Queue(int n){
        //這裏最大值要加一
        maxSize = n + 1;
        queArray = new long[maxSize];
        front = 0;
        rear = -1;
    }
    //定義插入方法
    public void insert(long n){
        //判斷是否循環至隊首
        if(rear == maxSize - 1)
        {
            rear = -1;
        }
        //先自增再賦值
        queArray[++rear] = n;    
    }
    //定義刪除方法
    public long remove(){
        //先賦值在自增
        long temp = queArray[front++];
        //判斷是否循環至隊首
        if(front == maxSize)
        {
            front = 0;
        }
        return temp;
    }
    //定義查找首項方法
    public long peekFront(){
        return queArray[front];
    }
    
    public boolean isEmpty(){
        return (rear + 1 == front || (front + maxSize - 1 == rear));
    }
    
    public boolean isFull(){
        return (rear + 2 == front || (front + maxSize - 2 == rear));
    }
}


public class App {
    public static void main(String[] args){
        Queue theQueue = new Queue(5);
        
        theQueue.insert(10);
        theQueue.insert(20);
        theQueue.insert(30);
        theQueue.insert(40);
        theQueue.remove();
        theQueue.remove();
        theQueue.remove();
        theQueue.insert(50);
        theQueue.insert(60);
        theQueue.insert(70);
        theQueue.insert(80);
        
        while(!theQueue.isEmpty())
        {
            long n = theQueue.remove();
            System.out.print(n);
            System.out.print(" ");
            
        }
        System.out.print("");
        
    }    
}

優先級隊列

package aa;

import java.io.IOException;
//在這個隊列中小的在後,隊列有大到小排列,這裏只能訪問最後一個數據項
class Priority{
    private int maxSize;
    private long[] queArray;
    private int nItems;
    
    public Priority(int s){
        maxSize = s;
        queArray = new long[maxSize];
        nItems = 0;
    }
    
    public void insert(long i){
        //定義一個臨時變量
        int j;
        //判斷數據項是否為零
        if(nItems == 0)
        {
            //如果為零插入數據,數據項增加
            queArray[nItems++] = i;
        }
        else
        {
            //數據項從後往前遍歷,註意j>=0
            for(j = nItems - 1; j >= 0; j--)
            {
                //判斷插入項是否大於遍歷的數據項
                if(i > queArray[j])
                {
                    //如果大於則從j的數據項開始每一項都後移一位
                    queArray[j+1] = queArray[j];                    
                }
                else
                {
                    break;
                }
            }
            //在for循環中最後一次循環的最後階段執行了j--,所以在這裏加回來,這樣for循環外的j+1相當於for循環內的判斷的j
            queArray[j + 1] = i;
            nItems++;            
        }
    }
    
    public long remove(){
        return queArray[--nItems];
    }
    
    public long peekMin(){
        return queArray[nItems - 1];
    }
    
    public boolean isEmpty(){
        return (nItems == 0);
    }
    
    public boolean isFull(){
        return (nItems == maxSize);
    }
}
public class PriorityApp {
    public static void main(String[] args) throws IOException{
        Priority thePri = new Priority(5);
        thePri.insert(30);
        thePri.insert(50);
        thePri.insert(10);
        thePri.insert(40);
        thePri.insert(20);
        
        while(!thePri.isEmpty())
        {
            long item = thePri.remove();
            System.out.print(item + " ");
        }
        System.out.println("");    
    }
}

java面向對象的棧 隊列 優先級隊列的比較