链表

package myList;

import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Hashtable;

public class MyLinkedList
{
	class Node{
		int data;
		Node next = null;
		Node(int data){
			this.data = data;
		}
	}

	static Node head = null;
	
	public int length(){
		int length = 0;
		Node tmp = head;
		while(tmp != null){
			tmp = tmp.next;
			length++;
		}
		return length;
	}
	
	public void addint(int data){
		Node node = new Node(data);
				
		if(head==null){
			head = node;
			return;
		}
		
		Node tmp = head;
		while(tmp.next!=null){
			tmp = tmp.next;
		}
		tmp.next = node;
		return;
	}
	
	public boolean deleteNode(int index){
		if(index<1||index>length()){
			return false;
		}
		
		if(index == 1){			 
			head = head.next;			
			return true;
		}
		
		int curIndex = 2;
		Node prev = head;
		Node cur = prev.next;
		while(curIndex<index){
			prev = cur;
			cur = cur.next;
			curIndex++;
		}
		
		prev.next = cur.next;
		cur = null;
		return true;
	}
	
	public Node order(){
		Node curNode = head;
		Node nextNode = null;
		int tmp = 0;
		while(curNode.next!=null){
			nextNode = curNode.next;
			while(nextNode!=null){
				if(nextNode.data>curNode.data){
					tmp = nextNode.data;
					nextNode.data = curNode.data;
					curNode.data = tmp;					
				}
				nextNode = nextNode.next;
			}
			curNode = curNode.next;
		}
		return head;
	}
	
	public void printList(){
		Node tmp = head;
		while(tmp!=null){
			System.out.println(tmp.data);
			tmp = tmp.next;
		}
	}
	
	public void print2File(){
		 try
		{
			System.setOut(new PrintStream(new FileOutputStream("c:/file.txt")));
			printList();
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 finally{
			 System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
		 }
	}
	
	public void deleteDuplicate(Node head){
		Hashtable<Integer,Integer> table = new Hashtable<>();
		if(head==null) return;
		table.put(head.data, 1);
		Node cur = head.next;
		Node prev = head;
		while(cur!=null){
			if(table.containsKey(cur.data)){
				prev.next = cur.next;//注意成功删除和添加进表的这一步是大不相同的
			}
			else{
				table.put(cur.data, 1);
				prev = cur;
			}			
			cur = cur.next;
		}
	}
	
	public Node findEle(int k,Node head){
		if(k<1) {
			System.out.println("索引有误,索引小于1");
			return null;
		}
		//顺数第k个
		Node tmp = head;
		int index=1;
		while(tmp!=null){
			tmp = tmp.next;
			index++;
			if(index == k)
				break;
		}
		if(tmp == null) {
			System.out.println("索引有误,索引大于长度");
			return null;
		}
		Node first = head;
		Node last = tmp;
		while(last.next!=null){
			last = last.next;
			first = first.next;
		}
		return first;
	}

	public void printListReversely(Node pListHead){
		if(pListHead!=null){
			printListReversely(pListHead.next);
			System.out.println(pListHead.data);
		}
	}
	
	public Node searchMid(){
		Node first = head;
		Node last = head;
		while(first!=null&&first.next!=null&&first.next.next!=null){
			first = first.next.next;
			last = last.next;
		}
		return last;
	}
	
	public boolean deleteNode(Node node){
		if(node==null||node.next == null){
			return false;
		}
				
		node.data = node.next.data;
		node.next = node.next.next;
		return true;
	}
	
	public Node firstMeetNode(Node h1,Node h2){
		int length1= 0;		
		while(h1!=null){
			length1++;
			h1 = h1.next;
		}
		
		int length2 = 0;
		while(h2!=null){
			length2++;
			h2 = h2.next;
		}
		
		if(h1!=h2) return null;
		
		int dis = Math.abs((length1-length2));
		
		if(length1>length2){
			while(dis--!=0){
				h2 = h2.next;
			}
		}
		else{
			while(dis--!=0){
				h1 = h1.next;
			}
		}
		
		while(h1!=null){
			h1 = h1.next;
			h2 = h2.next;
			if(h1==h2)	break;
		}
		System.out.println(h1.data);
		System.out.println(h2.data);
		return h1;		
	}
	
	public static boolean IsLoop(Node head){
		Node fast = head;
		Node last = head;
		if(fast == null){
			return false;
		}
		while(fast!=null&&fast.next!=null){
			fast = fast.next.next;
			last = last.next;
			if(fast==last){
				return true;
			}
		}
		return !(fast==null||fast.next==null);
	}
	
	public static Node ReverseIteratively(Node head){
		if(head==null) return null;		
		
		Node prev = head;		
		Node cur = head.next;
		prev.next = null;
		Node temp = null;
		
		while(cur != null){
			temp = cur.next;
			cur.next = prev;
			prev = cur;
			cur = temp;		//注意顺序	
		}
		
		return prev;
	}
	
	
	public static void main(String[] args)
	{
		MyLinkedList list = new MyLinkedList();
		list.addint(3);
		list.addint(1);
		list.addint(5);
		list.addint(2);
		list.deleteNode(1);
		System.out.println("listLen:"+list.length());
		System.out.println("before order & before reverse:");
		list.printList();
		list.order();
		System.out.println("after order:");
		list.printList();
		list.print2File();
		System.out.println("after reverse:");
		head = ReverseIteratively(head);
		list.printList();
		System.out.println("after reversely print:");
		list.printListReversely(head);
		System.out.println("search mid:");
		System.out.println(list.searchMid().data);
		System.out.println("delete mid");
		list.deleteNode(list.searchMid());
		list.printList();
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值