循环单向链表
程序员文章站
2024-03-06 22:04:56
...
public class LinkQueue {
private class Node{//构造节点内部类
public String data;
public Node next;
public Node(String data,Node next){//初始化全部属性的构造器
this.data=data;
this.next=next;
}
}
private Node front;
private Node rear;
private int size;
public LinkQueue(String data){//以指定数据元素来创建链队列,该链队列只有一个元素
front=new Node(data,null);
rear=front;
size++;
}
public void add(String data){
if(front==null){
front=new Node(data,null);
rear=front;
}else{
Node newNode=new Node(data,null);
rear=rear.next;
rear=newNode;
}
}
public String remove(){
Node oldFront=front;
front=front.next;
oldFront.next=null;
size--;
return oldFront.data;
}
public static void main(String[] args) {
LinkQueue l=new LinkQueue("A");
l.add("B");
l.add("C");
System.out.println(l.remove());
}
}