Use arrays, linked lists to manually implement queues

Array implementation queue (polling data migration once)

/**
 * 思路:
 * 用数组实现队列
 * 入队:判断队列是否满了。没满,加入元素,下标后移
 * 出队:判断队列是否为null。出队的是下标0的元素。不为null,数组整体前移1。更新tail
 */
public class Arr_PollMove {
    
    

    public static void main(String[] args) {
    
    
        Arr_PollMove queue = new Arr_PollMove(2);
        queue.offer("1");
        queue.offer("2");

        boolean offer3 = queue.offer("3");
        System.out.println(offer3);//false

        String poll1 = queue.poll();
        System.out.println(poll1);//1

        String poll2 = queue.poll();
        System.out.println(poll2);//2

        String poll3 = queue.poll();
        System.out.println(poll3);//null
    }

    String[] arr;
    int tail;

    public Arr_PollMove(int capacity){
    
    
        arr=new String[capacity];
        tail=0;
    }
    public boolean offer(String value){
    
    
        //队满
        if (tail==arr.length)return false;
        arr[tail++]=value;
        return true;
    }

    public String poll(){
    
    
        //队空
        if (tail==0)return null;
        String pollS = arr[0];
        System.arraycopy(arr,1,arr,0,arr.length-1);
        //更新tail
        tail--;
        return pollS;
    }

}

Array to achieve queue (data migration during offer)

/**
 * 思路:
 * 入队:
 * 判断队列是否已经满了,判断队列的head指针是否在0位置。
 * 如果满了,并且head==0说明队列真的满了。
 * 如果满了,head不在0位置,说明数据有剩余空间,进行数据迁移。迁移后更新head,tail
 * 如果队列不满,加入数据,后移tail
 * 出队:
 * 判断队列是否为null。不为null,不进行数据迁移,让head指针向后指
 */
public class Arr_OfferMove {
    
    

    public static void main(String[] args) {
    
    
        Arr_OfferMove queue = new Arr_OfferMove(2);
        queue.offer("1");
        queue.offer("2");


        String poll1 = queue.poll();
        System.out.println(poll1);//1

        boolean offer3 = queue.offer("3");
        System.out.println(offer3);//true
    }

    String[] arr;
    int head,tail;

    public Arr_OfferMove(int capacity){
    
    
        arr=new String[capacity];
        tail=0;
        head=0;
    }
    public boolean offer(String value){
    
    
        if (tail==arr.length&&head!=0) {
    
    
            System.arraycopy(arr, head, arr, 0, arr.length - head);
            // 搬移完之后重新更新 head 和 tail
            tail-=head;
            head=0;
        }
        //队满
        if (tail==arr.length)return false;
        arr[tail++]=value;
        return true;
    }

    public String poll(){
    
    
        //队空
        if (tail==head)return null;
        String pollS = arr[head++];
        return pollS;
    }
}

Realize with linked list

/**
 * 思路:
 * 链表实现
 * offer入队:如果的第一个节点,赋值给head和tail。如果不是,tail指向该节点,新节点变为tail
 * poll出队:head出队,并更新head
 */
public class Linked_queue {
    
    
    public static void main(String[] args) {
    
    
        Linked_queue queue = new Linked_queue();
        queue.offer("1");
        queue.offer("2");
        String poll = queue.poll();
        System.out.println(poll);//1
    }

    class Node{
    
    
        String value;
        Node next;
        public Node(String value){
    
    
            this.value=value;
        }
    }

    Node head,tail;
    public void offer(String s){
    
    
        if(tail==null){
    
    
            Node current=new Node(s);
            head=current;
            tail=current;
        }else {
    
    
            tail.next=new Node(s);
            tail=tail.next;
        }
    }

    public String poll(){
    
    
        if (head==null)return null;
        String value = head.value;
        head=head.next;
        if (head==null)tail=null;
        return value;
    }
}

Guess you like

Origin blog.csdn.net/qq_38783664/article/details/111300671