[] Data structures and algorithms, and the queues and queues implemented chain

About queue, we first think of an example of a life, that is, queue up for tickets, first come, first buy, then the only place in the queue tail, advanced, first out, this is a typical queue structure.

A queue is a linear table to limit access point. Queue element can only be inserted from one end of the table, the other end of the deletion. Delete allows the head end is called the team, and the other end, that is, permit insertion of the tail end is called, into the first element to leave the queue, the queue is operating according to the FIFO principle.

Order queue

With sequential storage queue structure is formed to the queues.
The queues need to allocate a contiguous area to store queue elements, requires prior knowledge or estimation of the size of the queue.

Implement the queues based array

public class ArrayQueue {
    /**
     * 数组items
     */
    private String[] items;
    /**
     * 数组大小n
     */
    private int n=0;
    /**
     * head表示队头下标 tail表示队尾下标
     */
    private int head=0;
    private int tail=0;

    /**
     * 申请一个大小为capacity的数组
     * @param capacity
     */
    public ArrayQueue(int capacity){
        items=new String[capacity];
        n=capacity;
    }

    /**
     * 入队
     * @param item
     * @return
     */
    public boolean enqueue(String item){
        if(tail==n){
            return false;
        }
        items[tail]=item;
        ++tail;
        return true;
    }

    /**
     * 出队
     * @return
     */
    public String dequeue(){
        if(head==tail){
            return null;
        }
        String ret=items[head];
        ++head;
        return ret;
    }
}

Testing the queues

public class Test {
	public static void main(String[] args) {
	    ArrayQueue arrayQueue=new ArrayQueue(5);
        arrayQueue.enqueue("a");
        arrayQueue.enqueue("b");
        arrayQueue.enqueue("c");
        arrayQueue.dequeue();
        arrayQueue.dequeue();
    }  
}

Queue changes

Here Insert Picture Description

Chain queue

Chain queue is a queue chain implementation is simplification of the list.

Based on the list achieved stack chain

public class LinkedQueue {
    public class Node{
        private String value;
        private Node next;
        public Node(String value,Node node){
            this.value=value;
            this.next=node;
        }
    }

    /**
     * 队列的队首和队尾
     */
    private Node head=null;
    private Node tail=null;
    
    /**
     * 入队
     * @param value
     */
    public void enqueue(String value){
        Node newNode=new Node(value,null);
        if(tail==null){
            head=newNode;
            tail=newNode;
        }else{
            tail.next=newNode;
            tail=tail.next;
        }
    }

    /**
     * 出队
     * @return
     */
    public String dequeue(){
        if(head==null){
            return null;
        }
        String value = head.value;
        head=head.next;
        if(head==null){
            tail=null;
        }
        return value;
    }

}

Chain stack test

public class Test {
	public static void main(String[] args) {
	    LinkedQueue linkedQueue=new LinkedQueue();
        linkedQueue.enqueue("a");
        linkedQueue.enqueue("b");
        linkedQueue.enqueue("c");
        linkedQueue.dequeue();
        linkedQueue.dequeue();
    }  
}

Conversion queue

Here Insert Picture Description

Published 241 original articles · won praise 105 · Views 150,000 +

Guess you like

Origin blog.csdn.net/cxh6863/article/details/103990926
Recommended