儿童室外摄影道具:各位程序高手,请帮忙设计个C++程序!小女孩好急啊!

来源:百度文库 编辑:高考问答 时间:2024/05/10 19:22:51
题目:设计一个集合类,用来处理整型数
设计要求:
1) 具有为集合增加元素的功能。
2) 具有处理集合的交集、并集的功能,并用 “+” 表示并集, “*” 表示交集。
3) 要求用链表来存储集合的元素。
4)编写一个main()函数,测试你的集合类的各种功能。
谢谢!谢谢!

#include<iostream.h>
#include<stdlib.h>
typedef int ElemType;

struct LNode
{
ElemType data;
LNode* next;
};

void InitList(LNode*& HL)//初始化单链表
{
HL=NULL;
}

int ListEmpty(LNode *HL)//检查单链表是否为空
{
return(HL!=NULL);
}

void TraverseList(LNode*&HL)//遍历一个单链表
{
LNode* p=HL;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}

void InsertRear(LNode*& HL,const ElemType& item)//向单链表的末尾插入一个元素
{
LNode* newptr;
newptr=new LNode;
if( newptr==NULL)
{
cerr<<"Memory allocation failare!"<<endl;
exit(1);
}
newptr->data=item;
newptr->next=NULL;
if(HL==NULL)
HL=newptr;
else{
LNode *p=HL;
while(p->next!=NULL)
p=p->next;
p->next=newptr;
}
}

void InsertFront(LNode *&HL,const ElemType &item)//向单链表的表头插入一个元素
{
LNode* newptr;
newptr=new LNode;
if(newptr==NULL)
{
cerr<<"Memory allocation failare!"<<endl;
exit(1);
}
newptr->data=item;
newptr->next=HL;
HL=newptr;
}

void Insert(LNode *&HL,const ElemType& item)//向单链表中满足条件的位置插入一个元素
{
LNode* newptr;
newptr=new LNode;
if(newptr==NULL)
{
cerr<<"Memory allocation failare!"<<endl;
exit(1);
}
newptr->data=item;
LNode* cp;
LNode* ap;
ap=NULL;cp=HL;
while(cp!=NULL)
if(item<cp->data)
break;
else
{
ap=cp;
cp=cp->next;
}
if(ap==NULL)
{
newptr->next=HL;
HL=newptr;
}
else{
newptr->next=cp;
ap->next=newptr;}
}

ElemType DeleteFront(LNode *& HL)//从单链表中删除表头元素
{
if(HL==NULL)
{
cerr<<"Memory allocation failare!"<<endl;
exit(1);
}
LNode* p=HL;
HL=HL->next;
ElemType temp=p->data;
delete p;
return temp;
}

int Delete(LNode *& HL,const ElemType& item)//从单链表中 删除等于给定值的第一个元素
{
if(HL==NULL)
{
cerr<<"Hl is HULL!"<<endl;
return 0;
}
LNode *ap,*cp;
ap=NULL;
cp=HL;
while(cp!=NULL)
if(cp->data==item)
break;
else
{
ap=cp;
cp=cp->next;
}
if(cp==NULL)
HL=HL->next;
else
ap->next=cp->next;
delete cp;
return 1;
}

void main()
{
LNode* head;
InitList(head);
int i,j;int a[20];
for(i=0;i<20;i++)
{
j=rand()%10;
a[i]=j;
InsertRear(head,j);
}
TraverseList(head);
cout<<endl;

int x=30;

Insert(head,x);
TraverseList(head);

cout<<endl;
cout<<ListSize(head)<<endl;

Updata(head,1);
TraverseList(head);

}