1. 程式人生 > >java 實現迴圈連結串列

java 實現迴圈連結串列


 


public class CircleList {
	CircleNode currentNode;
	public CircleList(long size){
		currentNode=new CircleNode();
		CircleNode cur=currentNode;
		for(long i=1;i<size;++i){
			CircleNode next=new CircleNode();
			cur.setNext(next);
			next.setLast(cur);
			cur=next;
		}
		currentNode.setLast(cur);
		cur.setNext(currentNode);
	}
	public CircleNode next(){
		CircleNode result=currentNode;
		currentNode=currentNode.getNext();
		return result;
	}
	static class CircleNode{
		private CircleNode next;
		private CircleNode last;
		private String label;
		public CircleNode(){
			super();
		}
		public CircleNode getNext() {
			return next;
		}
		public void setNext(CircleNode next) {
			this.next = next;
		}
		public CircleNode getLast() {
			return last;
		}
		public void setLast(CircleNode last) {
			this.last = last;
		}
		public String getLabel() {
			return label;
		}
		public void setLabel(String label) {
			this.label = label;
		}
		
	}
}