高手请进啊,C++编程

来源:百度知道 编辑:UC知道 时间:2024/06/12 01:30:52
设计集合类
1.具有为集合增加元素的功能
2.具有处理集合 交、并集的功能,并用“+”表示并集,用“*”表示交 集等
3.要求用链表、存储集合的元素
4.编写一个main()函数,测试你的集合类的各种功能

#include<iostream>
using namespace std;
//定义单向链表,从表头插入
typedef struct node{
int data;
node *next;
}lnode ,*llist;
//增加元素
void insert(llist &s)
{
int i=0;
int exist;
llist t;
while(i>=0)
{
cout<<"输入一个元素:";
cin>>i;
if(i<0) break;//元素是负数则退出
//检查元素是否已存在
exist=0;
t=s;
while(t!=NULL)
{
if(i==t->data)
exist=1;
t=t->next;
}
if(!exist)
{
lnode *p=new lnode;
p->data=i;
p->next=s;
s=p;
}
}
}
//显示链表
void display(llist s)
{
while(s!=NULL)
{
cout<<s->data<<endl;
s=s->next;
}
}
//求并集
llist merge(llist s1,llist s2)
{
llist s3;
int match;
s3=s1;
llist t;
while(s2!=NULL)
{
match=0;
t=s1;