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;
}
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();
if(current.lData==value){
result=current;
current=temp;
}
}while(current!=temp);
return result;
}
public void step(){
if(current!=null){
current=current.next;
}
}
public void display(){
if(current!=null){
Link temp =current;
do{
step();
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) {
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();
}
}