Java隊列的幾種實現(循環隊列,阻塞隊列,順序隊列,鏈式隊列)

順序隊列

public class ArrayQueue {
        // 數組:items,數組大小:n
        private String[] items;
        private int n = 0;
        // head表示隊頭下標,tail表示隊尾下標
        private int head = 0;
        private int tail = 0;

        // 申請一個大小爲capacity的數組
        public ArrayQueue(int capacity) {
            items = new String[capacity];
            n = capacity;
        }

        // 入隊
        public boolean enqueue(String item) {
// 如果tail == n 表示隊列已經滿了
            if (tail == n) return false;
            items[tail] = item;
            ++tail;
            return true;
        }

        // 出隊
        public String dequeue() {
// 如果head == tail 表示隊列爲空
            if (head == tail) return null;
// 爲了讓其他語言的同學看的更加明確,把--操作放到單獨一行來寫了
            String ret = items[head];
            ++head;
            return ret;
        }

        public void printAll() {
            for (int i = head; i < tail; ++i) {
                System.out.print(items[i] + " ");
            }
            System.out.println();
        }
}

鏈式隊列

/**
	* 基於鏈表實現的隊列
	*
	* Author: Zheng
	*/
	public class QueueBasedOnLinkedList {
	
	// 隊列的隊首和隊尾
	private Node head = null;
	private Node tail = null;
	
	// 入隊
	public void enqueue(String value) {
	if (tail == null) {
	Node newNode = new Node(value, null);
	head = newNode;
	tail = newNode;
	} else {
	tail.next = new Node(value, null);
	tail = tail.next;
	}
	}
	
	// 出隊
	public String dequeue() {
	if (head == null) return null;
	
	String value = head.data;
	head = head.next;
	if (head == null) {
	tail = null;
	}
	return value;
	}
	
	public void printAll() {
	Node p = head;
	while (p != null) {
	System.out.print(p.data + " ");
	p = p.next;
	}
	System.out.println();
	}
	
	private static class Node {
	private String data;
	private Node next;
	
	public Node(String data, Node next) {
	this.data = data;
	this.next = next;
	}
	
	public String getData() {
	return data;
	}
	}
	
	}

循環隊列

/**
	* 基於鏈表實現的隊列
	*
	* Author: Zheng
	*/
	public class QueueBasedOnLinkedList {
	
	// 隊列的隊首和隊尾
	private Node head = null;
	private Node tail = null;
	
	// 入隊
	public void enqueue(String value) {
	if (tail == null) {
	Node newNode = new Node(value, null);
	head = newNode;
	tail = newNode;
	} else {
	tail.next = new Node(value, null);
	tail = tail.next;
	}
	}
	
	// 出隊
	public String dequeue() {
	if (head == null) return null;
	
	String value = head.data;
	head = head.next;
	if (head == null) {
	tail = null;
	}
	return value;
	}
	
	public void printAll() {
	Node p = head;
	while (p != null) {
	System.out.print(p.data + " ");
	p = p.next;
	}
	System.out.println();
	}
	
	private static class Node {
	private String data;
	private Node next;
	
	public Node(String data, Node next) {
	this.data = data;
	this.next = next;
	}
	
	public String getData() {
	return data;
	}

阻塞隊列

public class DynamicArrayQueue {
	// 數組:items,數組大小:n
	private String[] items;
	private int n = 0;
	// head表示隊頭下標,tail表示隊尾下標
	private int head = 0;
	private int tail = 0;
	
	// 申請一個大小爲capacity的數組
	public DynamicArrayQueue(int capacity) {
	items = new String[capacity];
	n = capacity;
	}
	
	// 入隊操作,將item放入隊尾
	public boolean enqueue(String item) {
	// tail == n表示隊列末尾沒有空間了
	if (tail == n) {
	// tail ==n && head==0,表示整個隊列都佔滿了
	if (head == 0) return false;
	// 數據搬移
	for (int i = head; i < tail; ++i) {
	items[i-head] = items[i];
	}
	// 搬移完之後重新更新head和tail
	tail -= head;
	head = 0;
	}
	
	items[tail] = item;
	tail++;
	return true;
	}
	
	// 出隊
	public String dequeue() {
	// 如果head == tail 表示隊列爲空
	if (head == tail) return null;
	// 爲了讓其他語言的同學看的更加明確,把--操作放到單獨一行來寫了
	String ret = items[head];
	++head;
	return ret;
	}
	
	public void printAll() {
	for (int i = head; i < tail; ++i) {
	System.out.print(items[i] + " ");
	}
	System.out.println();
	}
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章