关闭

链表的基本操作

295人阅读 评论(0) 收藏 举报
分类:
package com.zyf.data;

import java.util.Hashtable;

import org.junit.Test;

/**
 *链表的基本操作 
 * @author zyf
 *
 */
public class MyLinkedList{
	Node head = null;//链表头引用
	public void addNode(int d){
		Node newNode = new Node(d);
		if(head == null){
			head = newNode;
			return;
		}
		Node tmp = head;
		while(tmp.next != null){
			tmp = tmp.next;
		}
		//add node to end
		tmp.next = newNode;
	}
	/**
	 * @param index:删除第index个结点
	 * @return 成功返回true, 失败返回false
	 */
	public Boolean deleteNode(int index){
		if(index <1 || index > length()){
			return false;
		}
		//删除链表第一个元素
		if(index == 1){
			head = head.next;
			return true;
		}
		int i = 1;
		Node preNode = head;
		Node curNode = preNode.next;
		while(curNode != null){
			if(i == index){
				preNode.next = curNode.next;
				return true;
			}
			preNode = curNode;
			curNode = curNode.next;
			i++;
		}
		return true;
	}
	/**
	 * @return 返回结点的长度
	 */
	public int length(){
		int length = 0;
		Node tmp = head;
		while(tmp != null){
			length ++;
			tmp = tmp.next;
		}
		return length;
	}
	/**
	 * 对链表进行排序
	 * 返回排序后的头结点
	 */
	public Node orderList(){
		Node nextNode = null;
		int temp = 0;
		Node curNode = head;
		while(curNode.next != null){
			nextNode = curNode.next;
			while(nextNode != null){
				if(curNode.data > nextNode.data){
					temp = curNode.data;
					curNode.data = nextNode.data;
					nextNode.data = temp;
				}
				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;
		}
	}
	@Test
	public static void main(String[] args){
		MyLinkedList list = new MyLinkedList();
		list.addNode(5);
		list.addNode(3);
		list.addNode(1);
		list.addNode(3);
		System.out.println("listLen = " + list.length());
		System.out.println("before order:");
		list.printList();
		list.orderList();
		System.out.println("after order:");
		list.printList();
		//findElem(1,2);
	}
	//从链表中删除重复的数据
	public void deleteDuplecate(Node head){
		Hashtable<Integer,Integer> table = new Hashtable<Integer,Integer>();
		Node tmp = head;
		Node pre = null;
		while(tmp!=null){
			if(table.containsKey(tmp.data))
				pre.next = tmp.next;
			else{
				table.put(tmp.data, 1);
				pre=tmp;
			}
			tmp = tmp.next;
	}
	}
	
	public void deleteDuplecate_1(Node head){
		Node p = head;
		while(p != null){
			Node q = p;
			while(q.next != null){
				if(p.data == q.next.data){
					q.next = q.next.next;
				}else
					q = q.next;
			}
			p = p.next;
		}
	}
	//如何找出单链表中的倒数第k个元素
	@Test
	public Node findElem(Node head,int k){
		if(k<1 || k >this.length())
			return null;
		Node p1 = head;
		Node p2 = head;
		for(int i =0 ;i < k-1;i++)
			p1 = p1.next;
		while(p1 != null){
			p1 = p1.next;
			p2 = p2.next;
		}
		return p2;
	}
	//如何实现链表的反转
	public void ReverseIteratively(Node head){
		Node pReversedHead = head;
		Node pNode = head;
		Node pPrev = null;
		while(pNode != null){
			Node pNext = pNode.next;
			if(pNext == null)
				pReversedHead = pNode;
			pNode.next = pPrev;
			pPrev = pNode;
			pNode = pNext;
		}
		this.head = pReversedHead;
	}
	//如何从尾到头输出单链表
	public void printListReversely(Node pListHead){
		if(pListHead != null){
			printListReversely(pListHead.next);
			System.out.println(pListHead.data);
		}
	}
	//如何寻找单链表的中间结点
	public Node SearchMid(Node head){
		Node p = this.head;
		Node q = this.head;
		while(p != null && p.next != null && p.next.next != null){
			p = p.next.next;
			q = q.next;
		}
		return q;
	}
	//如何检测一个链表是否有环
	 public boolean IsLoop(Node head){
		 Node fast = head;
		 Node slow = head;
		 if(fast == null){
			 return false;
		 }
		 while(fast!=null && fast.next != null){
			 fast = fast.next.next;
			 slow = slow.next;
			 if(fast == slow){
				 return true;
			 }
		 }
		 return !(fast == null || fast.next == null);
	 }
	 public Node FindLoopPort(Node head){
		 Node slow = head,fast = head;
		 while(fast != null && fast.next != null){
			 slow = slow.next;
			 fast = fast.next.next;
			 if(slow == fast) break;
		 }
		 if(fast == null || fast.next == null)
			 return null;
		 slow = head;
		 while(slow != fast){
			 slow = slow.next;
			 fast = fast.next;
		 }
		 return slow;
	 }
	 //如何在不知道头指针的情况下删除指定结点
	 public boolean deleteNode(Node n){
		 if(n == null || n.next == null)
			 return false;
		 int tmp = n.data;
		 n.data = n.next.data;
		 n.next.data = tmp;
		 n.next = n.next.next;
		 return true;
	 }
	 //如何判断两个链表是否相交
	 public boolean isIntersect(Node h1,Node h2){
		 if(h1 == null || h2 == null)
			 return false;
		 Node tail1 = h1;
		 while(tail1.next != null)
			 tail1 = tail1.next;
		 Node tail2 = h2;
		 while(tail2.next != null){
			 tail2 = tail2.next;
		 }
		 return tail1 == tail2;
	 }
	 
	 public static Node getFirstMeetNode(Node h1,Node h2){
		 if(h1 == null || h2 == null)
			 return null;
		 Node tail = h1;
		 int len1 = 1;
		 //找到链表h1的最后一个结点
		 while(tail.next != null){
			 tail = tail.next;
			 len1 ++;
		 }
		 Node tail2 = h2;
		 int len2 = 1;
		 while(tail2.next != null){
			 tail2 = tail2.next;
			 len2 ++;
		 }
		 
		 if(tail != tail2){
			 return null;
		 }
		 Node t1 = h1;
		 Node t2 = h2;
		 //找出较长的链表,先遍历
		 if(len1 > len2){
			 int d = len1 = len2;
			 while(d != 0){
				 t1 = t1.next;
				 d--;
			 }
		 }
		 else{
			 int d = len2 - len1;
			 while(d != 0){
				 t2 = t2.next;
				 d--;
			 }
		 }
		 while(t1 != t2){
			 t1 = t1.next;
			 t2 = t2.next;
		 }
		 return t1;
	 }
	 //.
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:117905次
    • 积分:3818
    • 等级:
    • 排名:第8351名
    • 原创:270篇
    • 转载:26篇
    • 译文:0篇
    • 评论:9条
    最新评论