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()+" ");
}
}
總結
以上是生活随笔為你收集整理的java循环队列配对_循环队列 链式队列 的jJAVA实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java写入大字符串到oracle数据库
- 下一篇: java 连接kafka超时_java