java循环队列配对_循环队列 链式队列 的jJAVA实现

对节点的移除插入删除操作,一定要注意在表头和表尾边界进行处理的过程 出队列时

注意只有一个元素的情况,此时队尾和对头指针应该都为空,别忘了修改队尾指针。

class CircleQueue

{

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{

private static class Node{

private T e;

private Node pre;

private Node next;

public Node(T e,Node pre,Node next)

{

this.e=e;

this.pre=pre;

this.next=next;

}

}

private Node head;

private Node tail;

private int size;

public LinkedQueue()

{

head=new Node(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(e,tail,null);

if(size==0)

head=tail;

size++;

tail.pre.next=tail;

}

}

public class Queue {

public static void main(String args[])

{

LinkedQueue a=new LinkedQueue();

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()+" ");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值