Explain the data structure in a simple way-queue

queue

concept

Queue: A special linear table that only allows inserting data at one end and deleting data at the other end. The queue has FIFO (First In First Out) into the queue: the end of the insert operation is called Tail/Rear ) Out of the queue: the end of the delete operation is called the head of the queue (Head/Front)

Interface relationship

Insert picture description here

Method description

method Throw an exception Return special value
Enqueue add(e) offer(e)
Dequeue remeve () poll()
Leader element element() peek()

Method realization

Insert picture description here

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;
    }

}

Implement queues with stacks

Likou link

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();
    }
}

Circular queue

Likou link

Solutions

Throws the question: Can the queue be implemented as an array?

Answer: Yes, if you think of the array as a ring, insert the data at the head of the array when the array is full, thus forming a circular queue.
Insert picture description here

Question 1: When front and rear meet, are they empty or full?

Solution:
Sacrifice a space to determine whether it is full.
Determine whether the next one of the current rear is front


Question 2: Both front and rear face an out-of-bounds problem?

Solution:
Add a conditional judgment.
When (rear+1)%len == front, space conversion will occur


Code

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;
    }
}

Guess you like

Origin blog.csdn.net/starry1441/article/details/114706413