泛型单链表环的操作

package genericlinklist;
/*
 * 泛型单链表环的操作
 * */

class TestLink1<T>{
	class Entry<T>{
		T data;
		Entry<T> next;
		public Entry(){
			data = null;
			next = null;
			}
		public Entry(T val){
			data = val;
			next = null;
			}	
		}
	private Entry<T> head;
	//将来指向头结点处  标志
	public TestLink1(){
		head = new Entry<T>();
		}
	//头插法
	public void insertHead(T val){
		Entry<T> entry = new Entry<T>(val);
		entry.next = head.next;
		head.next = entry;
		}	
	//尾插法	
	public void insertTail(T val){
		Entry<T> entry = new Entry<T>(val);
		//找到这个尾巴
		Entry<T> cur = head;
		while(cur.next != null){	
			cur = cur.next;	
			}	
		cur.next = entry;
		}
	//得到长度  存储数据节点的个数
	public int getLength(){	
	Entry<T> cur = head.next;
	int len = 0;	
	while(cur != null){	
		len++;	
		cur = cur.next;	
		}	
	return len;
	}	
    //创建一个带环的链表
    public void createLoop(){
    	Entry<T> cur = head;
    	while(cur.next !=null){
    		cur = cur.next;
    	}
    	cur.next = head.next.next;
    }
    //判断单链表是否有环
    public boolean isLoop() {
    	Entry<T> fast = head;
    	Entry<T> slow = head;
    	while(fast!=null&&fast.next!=null) {
    		fast = fast.next.next;//快的走两步
    		slow = slow.next;//慢的走一步
    		if(fast == slow) {//相遇则证明有环
    			return true;
    		}
    	}
    	return false;
    }

   //判断环的入口点
    public T getEntryLoop(){ 
    	Entry<T> fast = head; 	
    	Entry<T> slow = head; 	
    	if(!isLoop()){//判断是否有环 	
    		return null; 		
    		} 		
    	while(fast != null && fast.next != null){ 
    		fast = fast.next.next; 		
    		slow = slow.next; 		
    		if(fast == slow){ 	
    			break;//找到第一个相遇点 	
    			} 	
    		} 	
    	slow = head;//这个引用回到起点
    	while(fast != slow){ 	//同时出发直到相遇 
    		fast = fast.next; 	
    		slow = slow.next; 	
    		}
    	
    	return slow.data;	
    }
    	
    //第一次相遇和第二次相遇之间的路程为环的长度

    public int getLoopLength(){
    	int len = 0;
    	boolean flag = false;//标志是否为第一次相遇 
    	if(!isLoop()){
    		return -1;
    		} 
    	Entry<T> fast = head; 
    	Entry<T> slow = head;
    	while(fast != null && fast.next != null){ 
    		fast = fast.next.next; 
    		slow = slow.next; 	
    		if(slow == fast && flag == false){ 
    			flag = true; 
    			} 
    		if(flag = true){ 	
    			len++;//长度 		
    			} 	
    		if(slow == fast && flag == true){ 	
    			break; 	
    			} 
    		} 	
    	return len; 
    	
    	
    	
    }
	
    

    public void show(){	
	   Entry<T> cur = head.next;
	   while(cur != null){	
	 System.out.println("data: "+cur.data);
		cur = cur.next;	
		}
	}
    public void show2(Entry<T> entry){	
	  Entry<T> cur = entry;	
	  while(cur.next != null){
		System.out.println("data: "+cur.data);	
		cur = cur.next;	
		}
	}	


public Entry<T> getHead(){	
	return head;
	}
}
public class GenericLinkList3 {
    
	public static void main(String[] args) {
		
		TestLink1<Integer> k1 = new TestLink1<Integer>();
		for(int i = 0;i < 10;i++){		
			k1.insertHead(i);		
			}	
		k1.show();//9 8 7 6 5 4 3 2 1 0
		k1.createLoop();
		if(k1.isLoop()) {
			System.out.println("is loop");
		}else{
			System.out.println("is not loop");
		}
		int num = k1.getEntryLoop();
		System.out.println(num);
		System.out.println("len:"+k1.getLoopLength());
	}

}

猜你喜欢

转载自blog.csdn.net/qq_41974391/article/details/80535589