集合链表存储结构的实现和操作
2013-09-15 10:03
483 查看
Set接口:
节点Node类:
链表具体实现类LinkSet:
测试类:
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(); } }
相关文章推荐
- bo3-4.c 用单链表的基本操作实现链队列(存储结构由c3-2.h定义)的基本操作(9个)
- 单链表的存储结构及其基本操作的实现
- 数据结构_串_用链表做存储结构实现KMP算法_C++实现
- 数据结构:字符串的堆分配存储结构,基本操作实现和测试。
- 数据结构之---C语言实现广义表头尾链表存储表示
- 数据结构——二叉树的链式存储操作集合
- 采用中序线索链表存储结构,实现中序遍历
- IBM AIX存储层结构及操作命令实现
- bo2-2.cpp 带有头结点的单链表(存储结构由c2-2.h定义)的基本操作(12个)
- 数据结构基础 之 单链表 各类操作、思想与实现
- JAVA实现队列 队列的链式存储结构及操作
- IBM AIX存储层结构及操作命令实现
- 第四周【项目6-2 - 多项式加法在链表存储结构下的实现】
- 树的基本结构,以及利用链表实现树的各项操作(创建、添加/删除/打印树节点、销毁等等)
- 集合的链式存储及操作实现
- javascript实现数据结构与算法系列:线性表的静态单链表存储结构
- 数据结构(13)二叉树的动态链表存储和遍历的实现
- 数据结构(二)链表1:链式存储的基本操作
- 采用二叉链表存储结构,实现其各种遍历的方法
- 线性表的链接存储结构(单链表)的实现