<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>
循环队列 链式队列 的jJAVA实现
最新推荐文章于 2022-04-23 16:36:05 发布