高手们帮小弟看一下小弟写的关于用链表实现集合的合.并.差出现异常,在线等

lan_6373836 2008-03-22 03:18:36

#include <iostream.h>
#include <stdlib.h>
typedef struct List//节点的定义
{
int data;
struct List *next;
}List,* Link;
void CreateList(Link head)//创建链表
{
head=(Link)malloc(sizeof(List));
head->next=NULL;
while (1)
{
Link pointor=(Link)malloc(sizeof(List));
cin>>pointor->data;
if(!cin)//判断是否为字符
{
cin.clear();
cin.get();
continue;
}
else
{
break;
}
pointor->next=head->next;
head->next=pointor;
}
}
void PrintList(Link head)
{
Link newhead=head->next;
do
{
cout<<newhead->data<<" ";
newhead=newhead->next;
}while (newhead->next!=NULL);
}
void SetIntersection(Link head1,Link head2)//集合的交集
{
for(Link p=head1->next;p!=NULL;p=p->next)
for(Link q=head2->next;q!=NULL;q=q->next)
{
if (p->data==q->data)
{
cout<<p->data<<" ";
}
}
cout<<endl;
}
void SetAltogeter(Link head1,Link head2)//集合并集
{
for (Link p=head1->next;p!=NULL;p=p->next)
{
for (Link q=head2->next;q!=NULL;q=q->next)
{
if(p->data!=q->data)
{
cout<<p->next<<" ";
}
}
}
PrintList(head2);
cout<<endl;
}
void SetMission(Link head1,Link head2)//集合的差
{
cout<<"Head1-Head2:"<<endl;
for (Link p=head1->next;p!=NULL;p=p->next)
{
for (Link q=head2->next;q!=NULL;q=q->next)
{
if (p->data!=q->data)
{
cout<<p->data<<" ";
}
}
}
cout<<endl;
cout<<"Head2-Head1"<<endl;
for (Link m=head2->next;m!=NULL;m=m->next)
{
for (Link n=head1->next;n!=NULL;n=n->next)
{
if (m->data!=n->data)
{
cout<<m->data<<" ";
}
}
}
cout<<endl;
}
void main()
{
Link head1;
Link head2;
cout<<"创建列表Head1:"<<endl;
CreateList(head1);
cout<<"创建列表Head2:"<<endl;
CreateList(head2);
cout<<"集合的交集:"<<endl;
SetIntersection(head1,head2);
cout<<"集合的并集:"<<endl;
SetAltogeter(head1,head2);
cout<<"集合的差:"<<endl;
SetMission(head1,head2);
}

小弟初学,编译能通过就是建立第一个链表后第二个就出现异常了啊!望高手帮忙指正,谢谢!
编译情况:
创建列表Head1:
1 2 3 4
创建列表Head2:
集合的交集:
Press any key to continue
...全文
87 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lan_6373836 2008-03-22
  • 打赏
  • 举报
回复
怎么米有人愿意帮忙啊
lan_6373836 2008-03-22
  • 打赏
  • 举报
回复
各位大哥帮一下忙啊,好运啊
lan_6373836 2008-03-22
  • 打赏
  • 举报
回复
没有写过这样的吗?呵呵,帮一下忙啊!我们才学啊,怎么能给你们比啊
mousear 2008-03-22
  • 打赏
  • 举报
回复
你输入时1234中间不能加空格
iihero 2008-03-22
  • 打赏
  • 举报
回复
sigh,你这代码简直一团糟。
建议你还是先学会怎么cin再弄吧。
或者你干脆不用cin,
给你两个固定的数组A[] = {1, 2, 3, 4,5}为其建立链表,再弄那个cin吧。
创建的过程也不对,cin也没用对。

33,027

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧