队列的实现

1、 循环队列的实现

import java.util.Arrays;

public class LoopQueue<T>
{
	private int DEFAULT_SIZE = 10;
	//保存数组的长度。
	private int capacity;
	//定义一个数组用于保存循环队列的元素
	private Object[] elementData;
	//保存循环队列中元素的当前个数
	private int front = 0;
	private int rear = 0;
	//以默认数组长度创建空循环队列
	public LoopQueue()
	{
		capacity = DEFAULT_SIZE;
		elementData = new Object[capacity];
	}
	//以一个初始化元素来创建循环队列
	public LoopQueue(T element)
	{
		this();
		elementData[0] = element;
		rear++;
	}
	/**
	 * 以指定长度的数组来创建循环队列
	 * @param element 指定循环队列中第一个元素
	 * @param initSize 指定循环队列底层数组的长度
	 */
	public LoopQueue(T element , int initSize)
	{
		this.capacity = initSize;
		elementData = new Object[capacity];
		elementData[0] = element;
		rear++;
	}
	//获取循环队列的大小
	public int length()
	{
		if (empty())
		{
			return 0;
		}
		return rear > front ? rear - front : capacity - (front - rear);
	}
	//插入队列
	public void add(T element)
	{
		if (rear == front && elementData[front] != null)
		{
			throw new IndexOutOfBoundsException("队列已满的异常");
		}
		elementData[rear++] = element;
		//如果rear已经到头,那就转头
		rear = rear == capacity ? 0 : rear;
	}
	//移除队列
	public T remove()
	{
		if (empty())
		{
			throw new IndexOutOfBoundsException("空队列异常");
		}
		//保留队列的rear端的元素的值
		T oldValue = (T)elementData[front];
		//释放队列的rear端的元素
		elementData[front++] = null; 
		//如果front已经到头,那就转头
		front = front == capacity ? 0 : front;
		return oldValue;
	}
	//返回队列顶元素,但不删除队列顶元素
	public T element()
	{
		if (empty())
		{
			throw new IndexOutOfBoundsException("空队列异常");
		}
		return (T)elementData[front];
	}
	//判断循环队列是否为空队列
	public boolean empty()
	{
		//rear==front且rear处的元素为null
		return rear == front 
			&& elementData[rear] == null;
	}
	//清空循环队列
	public void clear()
	{
		//将底层数组所有元素赋为null
		Arrays.fill(elementData , null);
		front = 0;
		rear = 0;
	}
	public String toString()
	{
		if (empty())
		{
			return "[]";
		}
		else
		{
			//如果front < rear,有效元素就是front到rear之间的元素
			if (front < rear)
			{
				StringBuilder sb = new StringBuilder("[");
				for (int i = front  ; i < rear ; i++ )
				{
					sb.append(elementData[i].toString() + ", ");
				}
				int len = sb.length();
				return sb.delete(len - 2 , len).append("]").toString();
			}
			//如果front >= rear,有效元素为front->capacity之间、0->front之间的
			else
			{
				StringBuilder sb = new StringBuilder("[");
				for (int i = front  ; i < capacity ; i++ )
				{
					sb.append(elementData[i].toString() + ", ");
				}
				for (int i = 0 ; i < rear ; i++)
				{
					sb.append(elementData[i].toString() + ", ");
				}
				int len = sb.length();
				return sb.delete(len - 2 , len).append("]").toString();
			}
		}
	}
	public static void main(String[] args) {
		LoopQueue<String> queue = new LoopQueue<String>("aaaa",3);
		queue.add("bbbb");
		queue.add("cccc");
		System.out.println(queue);
		queue.remove();
		System.out.println(queue);
		queue.add("dddd");
		System.out.println(queue.length());


	}
}

链队列的实现

 public class LinkQueue<T> {
	 
	// 定义一个内部类Node,Node实例代表链队列的节点。
	private class Node {
		// 保存节点的数据
		private T data;
		// 指向下个节点的引用
		private Node next;

		// 无参数的构造器
		public Node() {
		}

		// 初始化全部属性的构造器
		public Node(T data, Node next) {
			this.data = data;
			this.next = next;
		}
	}

	// 保存该链队列的头节点
	private Node front;
	
	// 保存该链队列的尾节点
	private Node rear;
	
	// 保存该链队列中已包含的节点数
	private int size;

	// 创建空链队列
	public LinkQueue() {
		// 空链队列,front和rear都是null
		front = null;
		rear = null;
	}

	// 以指定数据元素来创建链队列,该链队列只有一个元素
	public LinkQueue(T element) {
		front = new Node(element, null);
		// 只有一个节点,front、rear都指向该节点
		rear = front;
		size++;
	}

	// 返回链队列的长度
	public int length() {
		return size;
	}

	// 将新元素加入队列
	public void add(T element) {
		// 如果该链队列还是空链队列
		if (front == null) {
			front = new Node(element, null);
			// 只有一个节点,front、rear都指向该节点
			rear = front;
		} else {
			// 创建新节点
			Node newNode = new Node(element, null);
			// 让尾节点的next指向新增的节点
			rear.next = newNode;
			// 以新节点作为新的尾节点
			rear = newNode;
		}
		size++;
	}

	// 删除队列front端的元素
	public T remove() {
		Node oldFront = front;
		front = front.next;
		oldFront.next = null;
		size--;
		return oldFront.data;
	}

	// 访问链式队列中最后一个元素
	public T element() {
		return rear.data;
	}

	// 判断链式队列是否为空队列
	public boolean empty() {
		return size == 0;
	}

	// 清空链队列
	public void clear() {
		// 将front、rear两个节点赋为null
		front = null;
		rear = null;
		size = 0;
	}

	public String toString() {
		// 链队列为空链队列时
		if (empty()) {
			return "[]";
		} else {
			StringBuilder sb = new StringBuilder("[");
			for (Node current = front; current != null; current = current.next) {
				sb.append(current.data.toString() + ", ");
			}
			int len = sb.length();
			return sb.delete(len - 2, len).append("]").toString();
		}
	}
	public static void main(String[] args) {
		LinkQueue<String> queue = new LinkQueue<String>("aaaa");
		queue.add("bbbb");	
		queue.add("cccc");
		queue.add("dddd");
		System.out.println(queue);
		queue.remove();
		System.out.println(queue);
		queue.add("eeeee");
		System.out.println(queue);
		queue.remove();
		System.out.println(queue);
		
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值