簡単な方法でデータ構造を説明する-キュー

キュー

概念

キュー:一方の端でのみデータを挿入し、もう一方の端でデータを削除できる特別な線形テーブル。キューにはFIFO(First In First Out)があります。挿入操作の終わりはTail / Rear)Out ofキュー:削除操作の終了は、キューの先頭(Head / Front)と呼ばれます。

インターフェース関係

ここに画像の説明を挿入

メソッドの説明

方法 例外をスローする 特別な値を返す
エンキュー add(e) オファー(e)
デキュー 修正する() poll()
リーダー要素 素子() ピーク()

メソッドの実現

ここに画像の説明を挿入

class Node {
    
    
    public int val;
    public Node next;

    public Node(int val) {
    
    
        this.val = val;
    }
}

public class MyQueue {
    
    
    public Node first;
    public Node last;

    //入栈
    public boolean offer(int val) {
    
    
        Node node = new Node(val);
        if(this.first == null) {
    
    
            this.first = node;
            this.last = node;
        }else {
    
    
            this.last.next = node;
            this.last = node;
        }
        return true;
    }

    //判断栈是否为空
    public boolean isEmpty() {
    
    
        if(this.first == null && this.last == null) {
    
    
            return true;
        }
        return false;
    }

    //出栈
    public int poll() throws RuntimeException{
    
    
        if(isEmpty()) {
    
    
            throw new RuntimeException("队列为空");
        }
        int ret = this.first.val;
        this.first = this.first.next;
        return ret;
    }

    //得到队头元素
    public int peek() {
    
    
        if(isEmpty()) {
    
    
            throw new RuntimeException("队列为空");
        }
        return this.first.val;
    }

}

スタックを使用してキューを実装する

Likouリンク

class MyQueue {
    
    
    Stack<Integer> a;
    Stack<Integer> b;

    /** Initialize your data structure here. */
    public MyQueue() {
    
    
        a = new Stack<>();
        b = new Stack<>();
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
    
    
        a.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
    
    
        if(b.empty()) {
    
    
            int size = a.size();
            for(int i = 0; i < size; i++) {
    
    
                b.push(a.pop());
            }
        }
        return b.pop();
    }
    
    /** Get the front element. */
    public int peek() {
    
    
        if(b.empty()) {
    
    
            int size = a.size();
            for(int i = 0; i < size; i++) {
    
    
                b.push(a.pop());
            }
        }
        return b.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
    
    
        return a.empty() && b.empty();
    }
}

循環キュー

Likouリンク

ソリューション

質問を投げます:キューを配列として実装できますか?

回答:はい。配列をリングと考える場合は、配列がいっぱいになったときに配列の先頭にデータを挿入して、循環キューを形成します。
ここに画像の説明を挿入

質問1:フロントとリアが出会うとき、それらは空ですか、それともいっぱいですか?

解決策:
スペースを犠牲にして、スペースがいっぱいかどうかを判断します。
現在のリアの次のリアがフロントかどうかを判断します。


質問2:前面と背面の両方が範囲外の問題に直面していますか?

解決策:
条件付き判定を追加します。
(rear + 1)%len == frontの場合、スペース変換が発生します。


コード

class MyCircularQueue {
    
    
    public int[] arr;
    public int rear;    //队尾
    public int front;   //队首
    public int len;

    public MyCircularQueue(int k) {
    
    
        arr = new int[k+1];
        len = k+1;
    }
    
    public boolean enQueue(int value) {
    
    
        if(isFull()) return false;
        arr[rear%len] = value;
        rear++;
        return true;
    }
    
    public boolean deQueue() {
    
    
        if(isEmpty()) return false;
        front++;
        return true;
    }
    
    public int Front() {
    
    
        if(isEmpty()) return -1;
        return arr[front%len];
    }
    
    public int Rear() {
    
    
        if(isEmpty()) return -1;
        return arr[(rear-1)%len]; 
    }
    
    public boolean isEmpty() {
    
    
        return rear == front;
    }
    
    public boolean isFull() {
    
    
        return (rear+1)%len == (front)%len;
    }
}

おすすめ

転載: blog.csdn.net/starry1441/article/details/114706413