java循环单链表实现约瑟夫环问题

package SingleLinkList;

//节点类
class Node{
	private int data;
	private Node next;
	public int getData() {
		return data;
	}
	public void setData(int data) {
		this.data = data;
	}
	public Node getNext() {
		return next;
	}
	public void setNext(Node next) {
		this.next = next;
	}
	
	public Node(int data,Node next) {
		this.data=data;
		this.next=next;
	}
	
}

//链表类
public class YueSeFu {
	private Node head;//头节点
	private Node tail;//尾指针
	private int length;
	
	public YueSeFu(){
		head=new Node(-1,null);
	
		length=0;
	}
	
	//头插法建立单链表
	public void addhead(int item){
		Node node=new Node(item,null);//node=(Node *)malloc(sizeof(node))
		
		node.setNext(head.getNext());//node->next=head->next;
		
		head.setNext(node);//head->next=node;
		
		length++;
		
	}
	
	//尾插法建立单链表
	public void addtail(int item){

		Node node=new Node(item,null);
		if(length==0){
			head.setNext(node);
			tail=node;
//			tail.setNext(null);
			length++;
			return;
		}
//		while(tail.getNext()!=null){
//			tail=tail.getNext();
//		}
		tail.setNext(node);
		tail=node;
		length++;
	}
	
	
	public void addcircleLink(int item){
		Node node=new Node(item,head.getNext());
		if(length==0){
			head.setNext(node);
			tail=node;
			length++;
			return;
		}
		tail.setNext(node);
		tail=node;
		
		
		length++;
	}
	
	//输出单链表
	public void printList(){
		Node temp=head.getNext();
		while(temp!=null){
			System.out.print(temp.getData()+" ");
			temp=temp.getNext();
		}
		System.out.println("length="+length);
	}
	
	//输出循环链表
	public void printcircleList(){
		Node temp=head.getNext();
		if(length==1){
			System.out.print(temp.getData()+" ");
			return;
		}
		int i=0;
		while(i

你可能感兴趣的:(javaSE,有关编程方面的)