循环链表实现队列
Posted ssdut_yrp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了循环链表实现队列相关的知识,希望对你有一定的参考价值。
public class Link
public long lData;
public Link next;
public Link(long lData)
this.lData=lData;
public void displayLink()
System.out.print(lData+" ");
public class CircleList
private Link current;
private int nItems;
public CircleList()
current=null;
public void insert(long value)
Link newLink = new Link(value);
if(current==null)
current=newLink;
newLink.next=newLink;
else
newLink.next=current.next;
current.next=newLink;
current=newLink;//插入元素,current要移动要新元素
nItems++;
public long remove()
long temp = current.next.lData;
if(current.next==current)
current=null;
else
current.next=current.next.next;
nItems--;
return temp;
public long peek()
return current.next.lData;
public Link find(long value)
Link temp =current;
Link result=null;
if(current==null)
return result;
do
step();//从current的下一个元素开始
if(current.lData==value)
result=current;
current=temp;//还原current到原来的位置,这样就不会打乱插入的顺序,current指向最后插入的元素
while(current!=temp);//current到原来的位置,一周循环结束
return result;
public void step()
if(current!=null)
current=current.next;// 调用step()方法后,顺序会被打乱
public void display()
if(current!=null)
Link temp =current;
do
step();// 从current的一下个开始显示
System.out.print(current.lData + " ");
while(current!=temp);
System.out.println();
public boolean isEmpty()
return (current==null);
public int size()
return nItems;
public class Queue
private CircleList theCircleList;
private int nItems;
public Queue()
theCircleList = new CircleList();
nItems=0;
public void insert(long value)
theCircleList.insert(value);
public long remove()
return theCircleList.remove();
public long peek()
return theCircleList.peek();
public boolean isEmpty()
return (theCircleList.size()==0);
public int size()
return theCircleList.size();
public void display()
theCircleList.display();
public class QueueApp
/**
* @param args
*/
public static void main(String[] args)
// TODO Auto-generated method stub
Queue newQueue = new Queue();
newQueue.insert(10);
newQueue.insert(20);
newQueue.insert(30);
newQueue.insert(40);
newQueue.display();
newQueue.remove();
newQueue.remove();
newQueue.display();
newQueue.insert(50);
newQueue.insert(60);
newQueue.insert(70);
newQueue.display();
long n = newQueue.remove();
System.out.println("删掉:"+n);
System.out.println("队头元素是"+newQueue.peek());
newQueue.display();
以上是关于循环链表实现队列的主要内容,如果未能解决你的问题,请参考以下文章