ITeye博客

集合操作类--包(单向链表实现)

┿┅мīSS · 2009-08-07 14:23 · J2SE · OpenSource · Markdown

节点类


package com.opensource.nodes;

/**
 * 一个IntNode为链表提供一个节点,每个节点包含整形数据。链表可以具有任何长度,
 * 仅受堆中空闲内存空间的限制。但是当超出Integer.MAX_VALUE时,listLengh将
 * 因为算术溢出而不正确
 */
public class IntNode 
{
	private int data;  //储存在这个节点中的元素
	private  IntNode link; //指向链表中的下一个节点
	
	public IntNode(int initialData,IntNode initialLink)
	{
		data = initialData;
		link = initialLink;
	}
	
	/**
	 * 在当前节点之后添加新节点
	 * @param element  
	 */
	public void addNodeAfter(int element)
	{
		link =new IntNode(element,link);
	}
	
	/**
	 * 获取节点中储存的数值
	 * @return
	 */
	public int getData()
	{
		return data;
	}
	
	/**
	 * 获取指向当前节点的下一个节点的指针
	 */
	public IntNode getLink()
	{
		return link;
	}
	
	/**
	 * 复制链表
	 * @param source 将要复制的链表的头指针
	 * @return
	 */
	public static IntNode listCopy(IntNode source)
	{
		IntNode copyHead;
		IntNode copyTail;
		
		if(source == null)
			return null;
		
		copyHead = new IntNode(source.data,null);
		copyTail = copyHead;
		while(source.link != null)
		{
			source = source.link;
			copyTail.addNodeAfter(source.data);
			copyTail = copyTail.link;
		}
		
		return copyHead;
	}
	
	/**
	 * 复制链表,同时返回副本的头指针和为指针
	 * @param source  将要复制的链表的头指针
	 * @return
	 */
	public static IntNode[] listCopyWithTail(IntNode source)
	{
		IntNode copyHead;
		IntNode copyTail;
		IntNode[] answer = new IntNode[2];
		
		if(source == null)
			return null;
		
		copyHead = new IntNode(source.data , null);
		copyTail = copyHead;
		while(source.link != null)
		{
			source = source.link;
			copyTail.addNodeAfter(source.data);
			copyTail = copyTail.link;
		}
		answer[0] = copyHead;
		answer[1] = copyTail;
		
		return answer;
	}
	
	/**
	 * 获取链表的长度
	 * @param head 
	 * @return
	 */
	public static int listLength(IntNode head)
	{
		IntNode cursor;
		int answer = 0;
		for(cursor = head;cursor !=null;cursor = head.link)
		{
			answer++;
		}
		
		return answer;
	}
	
	/**
	 * 复制链表的一部分,同时提供副本的头指针和尾指针
	 * @param start
	 * @param end
	 * @return
	 */
	public static IntNode[] listPart(IntNode start, IntNode end)
	{
		IntNode copyHead;
		IntNode copyTail;
		IntNode[] answer = new IntNode[2];
		
		if(start == null)
			throw new IllegalArgumentException("start is null");
		if(end == null)
			throw new IllegalArgumentException("end is null");
		
		copyHead = new IntNode(start.data,null);
		copyTail = copyHead;
		while(start != end)
		{
			start = start.link;
			if(start ==null)
				throw new IllegalArgumentException
				  ("end node was not found on the list");
			copyTail.addNodeAfter(start.data);
			copyTail = copyTail.link;
		}
		answer[0] = copyHead;
		answer[1] = copyTail;
		return answer;
	}
	
	/**
	 * 查找位于链表中特定位置的节点
	 * @param head
	 * @param position
	 * @return
	 */
	public static IntNode listPosition(IntNode head,int position)
	{
		IntNode cursor;
		int i;
		
		if(position <= 0)
			throw new IllegalArgumentException("position is not positive.");
		cursor = head;
		for(i = 1;(i < position)&&(cursor != null);i++)
			cursor = cursor.link;
		
		return cursor;
	}
	
	/**
	 * 查找链表中的特定数据
	 * @param head
	 * @param target
	 * @return
	 */
	public static IntNode listSearch(IntNode head,int target)
	{
		IntNode cursor;
		for(cursor = head;cursor != null;cursor = cursor.link)
		{
			if(target == cursor.data)
				return cursor;
		}
		return null;
	}
	
	/**
	 * 删除当前节点的下一个节点
	 */
	public void removeAfter()
	{
		link = link.link;
	}
	
	/**
	 * 设置当前节点中的数据
	 * @param newData
	 */
	public void setData(int newData)
	{
		data = newData;
	}
	
	/**
	 * 设置指向当前节点的下一个节点的指针
	 * @param newLink
	 */
	public void setLink(IntNode newLink)
	{
		link = newLink;
	}
}


包操作类


package com.opensource.collections;

import com.opensource.nodes.IntNode;

/**
 * 整形集合元素操作
 * 单向链表实现
 */
public class IntLinkedBag implements Cloneable 
{
	//包ADT的不变式:
	//1.包中的元素存储在链表中.
	//2.链表的头指针位于实例变量head中.
	//3.链表中的元素总数位于实例变量manyNodes;
	
	private IntNode head;  //链表的头指针
	private int manyNodes; //链表的节点数
	
	public IntLinkedBag()
	{
		head = null;
		manyNodes = 0;
	}
	
	/**
	 * 添加一个新元素到包中
	 * @param element
	 */
	public void add(int element)
	{
		head = new IntNode(element,head);
		manyNodes++;
	}
	
	/**
	 * 将另一个包中的内容添加到当前包中
	 * @param addend
	 */
	public void addAll(IntLinkedBag addend)
	{
		IntNode[] copyInfo;
		if(addend == null)
			throw new IllegalArgumentException("addend is null.");
		if(addend.manyNodes > 0)
		{
			copyInfo = IntNode.listCopyWithTail(addend.head);
			copyInfo[1].setLink(head);
			head = copyInfo[0];
			manyNodes += addend.manyNodes;
		}
	}
	
	/**
	 * 生成当前包的副本
	 */
	public Object clone()
	{
		IntLinkedBag answer;
		try
		{
			answer = (IntLinkedBag)super.clone();
		}catch(CloneNotSupportedException e)
		{
			throw new RuntimeException
			  ("This class does not implement Cloneable.");
		}
		answer.head = IntNode.listCopy(head);
		
		return answer;
	}
	
	/**
	 * 统计特定元素在当前包中出现的次数
	 * @param target
	 * @return
	 */
	public int countOccurrences(int target)
	{
		int answer = 0;
		IntNode cursor = IntNode.listSearch(head, target);
		while(cursor != null)
		{
			answer++;
			cursor = cursor.getLink();
			cursor = IntNode.listSearch(cursor, target);
		}
		
		return answer;
	}
	
	/**
	 * 从当前包中检索一个随机元素
	 * @return
	 */
	public int grab()
	{
		int i;
	    IntNode cursor;
	    
	    if(manyNodes == 0)
	    	throw new IllegalArgumentException("Bag size is zero.");
	    i = (int)(Math.random()*manyNodes+1);
	    cursor = IntNode.listSearch(head, i);
	    return cursor.getData();
	}
	
	/**
	 * 从包中指定元素
	 * @param target
	 * @return
	 */
	public boolean remove(int target)
	{
		IntNode targetNode;
		targetNode = IntNode.listSearch(head, target);
		if(targetNode == null)
			return false;
		else
		{
			targetNode.setData(head.getData());
			head = head.getLink();
			manyNodes--;
			return true;
		}
	}
	
	/**
	 * 获取当前包中元素的个数
	 * @return
	 */
	public int size()
	{
		return manyNodes;
	}
	
	/**
	 * 创建一个新包,它包含来自其他两个包中的所有元素
	 * @param b1
	 * @param b2
	 * @return
	 */
	public static IntLinkedBag union(IntLinkedBag b1,IntLinkedBag b2)
	{
		if(b1 == null)
			throw new IllegalArgumentException("b1 is null.");
		if(b2 == null)
			throw new IllegalArgumentException("b2 is null.");
		IntLinkedBag answer = new IntLinkedBag();
		answer.addAll(b1);
		answer.addAll(b2);
		return answer;
	}
}


评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。