循环队列 链式队列 的jJAVA实现

<strong><span style="font-size:24px;">对节点的移除插入删除操作,一定要注意在表头和表尾边界进行处理的过程 出队列时
注意只有一个元素的情况,此时队尾和对头指针应该都为空,别忘了修改队尾指针。</span></strong>
<span style="font-size:18px;">class CircleQueue<T>
{
	private final int DEFAULTCAPACITY=4; 
	private T[]array;
	private int head;
	private int tail;
	private int size;
	private int currentCapacity;
	public CircleQueue()
	{
		array=(T[])new Object[DEFAULTCAPACITY];
		head=0;
		currentCapacity=DEFAULTCAPACITY;
		tail=0;
	}
	public int size()
	{
		return size;
	}
	public T front()
	{
		if(isEmpty())
			return null;
		return array[head];
	}
	public boolean isEmpty()
	{
		if(head==tail)
			return true;
		return  false; 
	}
	public boolean isFull()
	{
		if((tail+1)%currentCapacity==head)
			return true;
		return false;
	}
	public void enqueue(T a)
	{
		if(isFull())
			return ;
		array[tail++]=a;
		tail=tail%currentCapacity;
		size++;
	}
	public T dequeue()
	{
		if(isEmpty())
			return null;
		size--;
		T a= array[head++];
		head=head%currentCapacity;
		return a;
	}
}
/**
 * 本结构体链表为双向链表
 *
 */
class LinkedQueue<T>{
	private static class Node<T>{
		private T e;
		private Node<T> pre;
		private Node<T> next;
		public Node(T e,Node<T> pre,Node<T> next)
		{
			this.e=e;
			this.pre=pre;
			this.next=next;
		}
	}
	private Node<T> head;
	private Node<T> tail;
	private int size;
	public LinkedQueue()
	{
		head=new Node<T>(null,null,null);
		tail=head;
		size=0;
	}
	public boolean isEmpty()
	{
		if(size==0)
			return true;
		return false;
	}
	public int size()
	{
		return size;
	}
	public T front()
	{
		if(isEmpty())
			return null;
		return head.e;
	}
	public T dequeue()
	{
		if(isEmpty())
			return null;
		T a=head.e;
	    head=head.next;
            if(size==1)
               tail=head;
                size--;
		return a;
	}
	public void enqueue(T e)
	{
		tail=new Node<T>(e,tail,null);
		if(size==0)
			head=tail;
		size++;
		tail.pre.next=tail;
	}
}
public class Queue {
	public static void main(String args[])
	{
	    LinkedQueue<Integer> a=new LinkedQueue<Integer>();
	    a.enqueue(2);
	    a.enqueue(3);
	    a.enqueue(4);
	    a.enqueue(5);
	    System.out.println(a.size()+" ");
	    System.out.println(a.dequeue()+" ");
	    System.out.println(a.size()+" ");
	}
	

}
</span>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值