集合链表存储结构的实现和操作

Set接口:

public interface Set {

	/**
	 * @introduction 向集合中增加一个元素
	 * @param object
	 * @return
	 * @author Guo
	 */
	public boolean add(Object object);
	
	/**
	 * @introduction 从集合中删除一个元素
	 * @param object
	 * @return
	 * @author Guo
	 */
	public boolean remove(Object object);
	
	/**
	 * @introduction 判断一个元素是否属于这个集合
	 * @param object
	 * @return
	 * @author Guo
	 */
	public boolean contains(Object object);
	
	/**
	 * @introduction 返回集合中第index个元素的值
	 * @param index
	 * @return
	 * @author Guo
	 */
	public Object value(int index);
	
	/**
	 * @introduction 从集合中查找object并返回
	 * @param object
	 * @return
	 * @author Guo
	 */
	public Object find(Object object);
	
	/**
	 * @introduction  返回集合中元素的个数
	 * @return
	 * @author Guo
	 */
	public int size();
	
	/**
	 * @introduction 判断集合是否为空
	 * @return
	 * @author Guo
	 */
	public boolean isEmpty();
	
	/**
	 * @introduction 输出集合中所有的元素
	 * @author Guo
	 */
	public void output();
	
	/**
	 * @introduction 返回当前集合与参数集合set的并集
	 * @param set
	 * @return
	 * @author Guo
	 */
	public Set union(Set set);
	
	/**
	 * @introduction 返回当前集合与参数集合set的交集
	 * @param set
	 * @return
	 * @author Guo
	 */
	public Set intersection(Set set);
	
	/**
	 * @introduction 清除集合中的元素
	 * @author Guo
	 */
	public void clear();
}


 

节点Node类:

/**
 * @introduction 节点类
 * @author Guo
 *
 */
public class Node {

	/**
	 * @introduction 数据域
	 * @author Guo
	 */
	Object object;
	
	/**
	 * @introduction 指针域
	 * @author Guo
	 */
	Node next;
	
	/**
	 * @introduction 初始化指针域的构造方法
	 * @param next
	 * @author Guo
	 */
	public Node(Node next) {
		
		this.next = next;
	}
	
	/**
	 * @introduction 初始化值域和指针域的构造方法
	 * @param object
	 * @param next
	 * @author Guo
	 */
	public Node(Object object, Node next) {
		
		this.object = object;
		this.next = next;
	}
}


 

链表具体实现类LinkSet:

/**
 * @introduction 链表类
 * @author Guo
 *
 */
public class LinkSet implements Set{
	
	/**
	 * @introduction 头指针
	 * @author Guo
	 */
	private Node head;
	
	/**
	 * @introduction 链表的长度
	 * 
	 * @author Guo
	 */
	private int len;
	
	/**
	 * @introduction 无参数的构造方法,初始化长度和头指针
	 * @author Guo
	 */
	public LinkSet() {
		
		len = 0;
		head = new Node(null);
	}

	@Override
	public boolean add(Object object) {
		
		Node p = head;
		while(p.next != null) {
			
			if(p.next.object.equals(object))
				return false;
			p = p.next;
		}
		p.next = new Node(object, null);
		len++;
		return true;
	}

	@Override
	public boolean remove(Object object) {
		
		Node p = head;
		while(p.next != null) {
			
			if(p.next.object.equals(object))
				break;
			else 
				p = p.next;
		}
		
		if(p.next != null) {
			
			p.next = p.next.next;
			len--;
			return true;
		}
		return false;
	}

	@Override
	public boolean contains(Object object) {
		
		Node p = head;
		while(p.next != null) {
			
			if(p.next.object.equals(object))
				return true;
			else
				p = p.next;
		}
		return false;
	}

	@Override
	public Object value(int index) {
		
		Node p = head;
		if(index <= 0 || index > len)
			return null;
		int length = 1;
		while(p.next != null) {
			
			if(length == index)
				return p.next.object;
			else {
				p = p.next;
				length++;
			}
		}
		return null;
	}

	@Override
	public Object find(Object object) {
		
		Node p = head;
		while(p.next != null) {
			
			if(p.next.object.equals(object))
				return p.next.object;
			else
				p = p.next;
		}
		return null;
	}

	@Override
	public int size() {
		return len;
	}

	@Override
	public boolean isEmpty() {
		return len == 0;
	}

	@Override
	public void output() {

		Node p = head;
		while(p.next != null) {
			
			System.out.println(p.next.object + " ");
			p = p.next;
		}
	}

	@Override
	public Set union(Set set) {
		
		LinkSet setTemp = new LinkSet();
		Node p = head.next;
		Node q = setTemp.head;
		while(p != null) {
			
			Node node = new Node(p.object, null);
			q.next = node;
			p = p.next;
			q = node;
		}
		setTemp.len = len;
		LinkSet dset = (LinkSet)set;
		p = dset.head.next;
		while(p != null) {
			
			Object object = p.object;
			if(!contains(object)) {
				
				q.next = new Node(object, null);
				q = q.next;
				setTemp.len++;
			}
			p = p.next;
		}
		return setTemp;
	}

	@Override
	public Set intersection(Set set) {
		
		LinkSet setTemp = new LinkSet();
		LinkSet dset = (LinkSet)set;
		Node p = dset.head.next;
		Node q = setTemp.head;
		while(p != null) {
			
			Object object = p.object;
			if(contains(object)) {
				
				q.next = new Node(object, null);
				q = q.next;
				setTemp.len++;
			}
			p = p.next;
		}
		return setTemp;
	}

	@Override
	public void clear() {
		
		len = 0;
		head.next = null;
	}
}


 

测试类:

public class TestLinkSet {

	@Test
	public void testAdd() {
		
		Set set = new LinkSet();
		set.add("zhou");
		set.add("yan");
		set.add("ping");
		set.output();
	}
	
	@Test
	public void testRemove() {
		
		Set set = new LinkSet();
		set.add("xu");
		set.add("jian");
		set.add("guo");
		set.remove("xu");
		set.output();
		if(!set.remove("xu"))
			System.out.println("Not In Set");
	}
	
	@Test
	public void testContains() {
		
		Set set = new LinkSet();
		set.add("zhou");
		if(set.contains("zhou"))
			System.out.println("In Set");
		else
			System.out.println("Not In Set");
		if(set.contains("xu"))
			System.out.println("In Set");
		else
			System.out.println("Not In Set");
	}
	
	@Test
	public void testValue() {
		
		Set set = new LinkSet();
		set.add("zhou");
		set.add("xu");
		System.out.println(set.value(1));
		System.out.println(set.value(4));
	}
	
	@Test
	public void testFind() {
		
		Set set = new LinkSet();
		set.add("xu");
		set.add("zhou");
		System.out.println(set.find("zhou"));
		System.out.println(set.find("xxx"));
	}
	
	@Test
	public void testSize() {
		
		Set set = new LinkSet();
		set.add("xu");
		set.add("zhou");
		System.out.println(set.size());
	}
	
	@Test
	public void testIsEmpty() {
		
		Set set = new LinkSet();
		System.out.println(set.isEmpty());
		set.add("zhou");
		System.out.println(set.isEmpty());
	}
	
	@Test
	public void testUnion() {
		
		Set set = new LinkSet();
		set.add("xu");
		set.add("zhou");
		Set setCopy = new LinkSet();
		setCopy.add("xu");
		setCopy.add("yan");
		set.union(setCopy).output();
	}
	
	@Test
	public void testIntersection() {
		
		Set set = new LinkSet();
		set.add("xu");
		set.add("zhou");
		Set setCopy = new LinkSet();
		setCopy.add("zhou");
		set.add("guo");
		set.intersection(setCopy).output();
	}
	
	@Test
	public void testClear() {
		
		Set set = new LinkSet();
		set.add("xu");
		set.add("zhou");
		set.clear();
		set.output();
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值