链表部分在前面文章
此文只阐述交并部分。
代码
/**
* 计算类
* @author 焦焱-16060211104
*主要用于计算交并集运算
*/
public class Calculation {
/**
* 并运算
* @param a
* @param b 两个链表
* @return 并后的链表
*/
static Link Combine(Link a,Link b)
{
Link link = new Link();
Node temp = a.getHead();
Node temp1= b.getHead();
for(int i=0;i<a.getNum();i++)
{
temp=temp.getNext();
link.add(temp.getData());
}
for(int i=0;i<b.getNum();i++)
{
temp1 = temp1.getNext();
if(a.Select(temp1.getData())==null)
{
link.add(temp1.getData());
}
}
return link;
}
/**
* 交运算
* @param a
* @param b 两个链表
* @return 交后的链表
*/
static Link Intersection (Link a,Link b)
{
Link link = new Link();
Node temp = a.getHead();
for(int i=0;i<a.getNum();i++)
{
temp = temp.getNext();
if(b.Select(temp.getData())!=null)
{
link.add(temp.getData());
}
}
return link;
}
}