读书人

高手们帮小弟看一下小弟写的关于用链表

发布时间: 2012-02-19 19:43:37 作者: rapoo

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

C/C++ code
#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


[解决办法]
sigh,你这代码简直一团糟。
建议你还是先学会怎么cin再弄吧。
或者你干脆不用cin,
给你两个固定的数组A[] = {1, 2, 3, 4,5}为其建立链表,再弄那个cin吧。
创建的过程也不对,cin也没用对。

读书人网 >软件架构设计

热点推荐