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

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值