Stacks and Queues (Summary)

Recently, I started to get in touch with knowledge about stacks and queues. I hereby make a summary of stacks and queues based on the existing knowledge. The following blog is only a summary of my personal learning process. It would be a great honor to be of help to all bloggers.
This blog will briefly introduce the relevant knowledge and what are the precautions. I will only make a summary of myself, and I will make supplementary modifications later with the in-depth study.

stack:

concept

The stack is a special kind of linear table, and the operation on the node is restricted, that is, the restricted linear table mentioned in the previous blog. Nodes can only be inserted and deleted from a fixed section. Generally speaking, the end that allows insertion and deletion operations is called the top of the stack, and the corresponding end is called the bottom of the stack. The operation of inserting a node into the top of the stack is called pushing, and the operation of deleting the top node of the stack is called popping.
His characteristic is last in, first out——insert image description here

insert image description here

Queue (Queue):

concept

Like the previous stack, the queue is also a restricted linear table, which is deleted at the front end of the restricted table and inserted at the back end of the table. Generally speaking, the deletion end is called the head of the queue, and the insertion end is called the tail of the queue.
Its characteristic is, first in first out——

insert image description here
insert image description here

Double-ended queue (Deque)

concept:

The double-ended queue allows insertion and deletion operations at both ends. It can realize the operation of the stack and the operation of the queue

Implementing stacks and queues

  • Imitation (java.util.impl.Queue) Queue interface
package imp;

import java.util.NoSuchElementException;
//仿写(java.util.impl.Queue)的 Queue 接口

public interface Queue {
    
    
    //通过返回特殊值来通知错误的一组方法
    //插入(true 成功,false 失败)
    boolean offer(Integer e);
    //查看,返回队首元素,但不删除(返回null为空列表)
    Integer peek();
    //删除,返回并删除并删除首元素(返回null为空列表)
    Integer poll();

    //通过抛出异常来通知错误的一组方法
    //插入(永远返回true)
    default boolean add(Integer e){
    
    
        if(!offer(e)){
    
    
            throw new IllegalStateException();
        }
        return offer(e);
    }
    //查看,返回队首元素,但不删除
    default Integer element(){
    
    
        if(peek() == null){
    
    
            throw new NoSuchElementException();
        }
        return peek();
    }
    //删除,返回并删除并删除首元素
    default Integer remove(){
    
    
        if(poll() == null){
    
    
            throw new NoSuchElementException();
        }
        return poll();
    }
}
  • Define a Deque interface and let it inherit the Queue interface
package imp;

import java.util.NoSuchElementException;

public interface Deque extends Queue {
    
    
    //通过返回特殊值来通知错误的一组方法
    boolean offerFirst(Integer e);
    Integer peekFirst();
    Integer pollFirst();
    boolean offerLast(Integer e);
    Integer peekLast();
    Integer pollLast();

    //通过抛出异常来通知错误的一组方法
    default boolean addFirst(Integer e){
    
    
        if(!offerFirst(e)){
    
    
            throw new IllegalStateException();
        }
        return offerFirst(e);
    }

    default boolean addLast(Integer e){
    
    
        if(!offerLast(e)){
    
    
            throw new IllegalStateException();
        }
        return offerLast(e);
    }

    default Integer getFirst(){
    
    
        if(peekFirst() == null){
    
    
            throw new NoSuchElementException();
        }
        return peekFirst();
    }

    default Integer getLast(){
    
    
        if(peekLast() == null){
    
    
            throw new NoSuchElementException();
        }
        return peekLast();
    }

    default Integer removeFirst(){
    
    
        if(pollFirst() == null){
    
    
            throw new NoSuchElementException();
        }
        return pollFirst();
    }

    default Integer removeLast(){
    
    
        if(pollLast() == null){
    
    
            throw new NoSuchElementException();
        }
        return pollLast();
    }

    //继承自 Queue 的方法
    default boolean offer(Integer e){
    
    
        return offerLast(e);
    }
    //查看,返回队首元素,但不删除(返回null为空列表)
    default Integer peek(){
    
    
        return  getFirst();
    }
    //删除,返回并删除并删除首元素(返回null为空列表)
    default Integer poll(){
    
    
        return pollFirst();
    }

    //为 stack 准备的方法
    default void push(Integer e){
    
    
        addFirst(e);
    }
    default Integer pop(){
    
    
        return removeFirst();
    }
}

  • Define a LinkedList class to implement the Deque interface
package imp;

 public class LinkedList implements Deque{
    
    
    private static class Node{
    
    
        private Integer v;
        Node previous;
        Node next;
        Node(Integer e){
    
    
            v = e;
        }
        @Override
        public String toString() {
    
    
            return v + " ";
        }
    }
    private Node head;
    private Node tail;
    private int size;
    @Override
    public boolean offerFirst(Integer e) {
    
    
        Node node = new Node(e);
        if (size == 0) {
    
    
            head = node;
            tail = node;
        }else {
    
    
            head.previous = node;
            node.next = head;
            head = node;
        }
        size++;
        return true;
    }

    @Override
    public Integer peekFirst() {
    
    
        if(head == null){
    
    
            return null;
        }
        return head.v;
    }

    @Override
    public Integer pollFirst() {
    
    
        if(size == 0){
    
    
            return null;
        }
        int num = head.v;
        head = head.next;
        if(head != null){
    
    
            head.previous = null;
        }else{
    
    
            tail = null;
        }
        size--;
        return num;
    }

    @Override
    public boolean offerLast(Integer e) {
    
    
        Node node = new Node(e);
        if(size == 0){
    
    
            head = node;
        }else{
    
    
            node.previous = tail;
            tail.next = node;
        }
        tail = node;
        size++;
        return true;
    }

    @Override
    public Integer peekLast() {
    
    
        if(tail == null){
    
    
            return null;
        }
        return tail.v;
    }

    @Override
    public Integer pollLast() {
    
    
        if(size == 0){
    
    
            return null;
        }
        int num = tail.v;
        tail = tail.previous;
        if(tail != null){
    
    
            tail.next = null;
        }else{
    
    
            head = null;
        }
        size--;
        return num;
    }
 }

  • Debug self-implemented Stack and Queue
package imp;

package imp;

public class MyStack {
    
    
    public static void main(String[] args) {
    
    
        Deque myStack = new LinkedList();
        myStack.push(1);
        System.out.print(myStack.peek()+" ");
        myStack.push(2);
        System.out.print(myStack.peek()+" ");
        myStack.push(3);
        System.out.print(myStack.peek()+" ");
        myStack.push(4);
        System.out.print(myStack.peek()+" ");
        myStack.push(5);
        System.out.println(myStack.peek()+" ");
        myStack.pop();
        System.out.println(myStack.peek());
    }
}

insert image description here

package imp;

public class MyQueue {
    
    
    public static void main(String[] args) {
    
    
        Deque myQueue = new LinkedList();
        myQueue.add(1);
        System.out.print(myQueue.element()+" ");
        myQueue.add(2);
        System.out.print(myQueue.element()+" ");
        myQueue.add(3);
        System.out.print(myQueue.element()+" ");
        myQueue.add(4);
        System.out.print(myQueue.element()+" ");
        myQueue.add(5);
        System.out.println(myQueue.remove());
        System.out.print(myQueue.element()+" ");
    }
}

insert image description here
insert image description here
The above is the summary of stacks and queues in this blog. With the deepening of follow-up learning, the content will be supplemented and modified synchronously. It will be a great honor to help bloggers. Please correct me

Guess you like

Origin blog.csdn.net/m0_46233999/article/details/109514767