| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1653 人关注过本帖
标题:两链表求交集
只看楼主 加入收藏
studythec
Rank: 1
来 自:安徽阜阳
等 级:新手上路
帖 子:31
专家分:5
注 册:2010-3-13
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
两链表求交集
题目是:建立两个链表,A,B找交集,放入C中
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#define NULL 0                                 //宏定义NULL为0
#define SIZ sizeof(struct stu)                 //宏定义 SIZ为sizeof(struct stu)
struct stu                                      //结构体
{
    int num;
    stu *next;
};                                                     
void main()
{
    stu *Ha=NULL; stu *Hb=NULL; stu *Hc=NULL; //定义头指针
    stu *r,*s;
    struct stu *p,*q;                                  //定义指针p,q
    int x;
    cout<<"in put A:"<<endl;                      //建立单链表A
    cin>>x;
    r=Ha;
    for(;x;cin>>x)                              
    {                       
        s=(stu*) malloc(SIZ);
        s->num=x;
        if(Ha==NULL)
            Ha=s;
        else
            r->next=s;
        r=s;
    }                           
    if(r!=NULL)
        r->next=NULL;
    r=Ha;                                     //输出单链表A
    while(r!=NULL)
    {
        cout<<r->num<<" ";
        r=r->next;
    }
     cout<<endl;
    cout<<"in put B:"<<endl;                      //建立单链表B
    cin>>x;
    r=Hb;
    for(;x;cin>>x)
    {
        s=(stu*)malloc(SIZ);
        s->num=x;
        if(Hb==NULL)                     
            Hb=s;
        else
            r->next=s;
        r=s;
    }
    if(r!=NULL)
        r->next=NULL;
    r=Hb;
    while(r!=NULL)                            //输出单项链表B
    {
        cout<<r->num<<" ";
        r=r->next;
    }
     cout<<endl;
     p=Ha;q=Hb;                               //建立链表C
     r=Hc;
     for(;p&&q;p=p->next,q=q->next)
         if(p->num=q->num)
         {
             s=(stu*) malloc(SIZ);
             s->num=p->num;
             if(Hc==NULL)
             {
                 Hc=s;
                 r=Hc;
             }
             else
             {
                 r->next=s;
                 r=s;
             }
         }
    if(r!=NULL)
        r->next=NULL;
    r=Hc;                                   //输出链表C
    while(r!=NULL)
    {
        cout<<r->num<<" ";
        r=r->next;
    }
    cout<<endl;
}
搜索更多相关主题的帖子: 交集 链表 
2010-04-11 19:19
asdjc
Rank: 6Rank: 6
来 自:武汉
等 级:侠之大者
威 望:7
帖 子:98
专家分:487
注 册:2010-1-22
收藏
得分:4 
你要问什么?
2010-04-11 19:26
sjz_zdf
Rank: 2
等 级:论坛游民
帖 子:63
专家分:14
注 册:2008-6-22
收藏
得分:4 
展示这个这个程序是否正确?应该没错吧。
2010-04-12 14:09
studythec
Rank: 1
来 自:安徽阜阳
等 级:新手上路
帖 子:31
专家分:5
注 册:2010-3-13
收藏
得分:0 
错了啊,出不了结果

菜鸟一个,在校学习,多多指教!
2010-04-12 15:55
画圈成句点
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:62
专家分:143
注 册:2010-3-28
收藏
得分:4 
搞不懂
2010-04-12 18:54
韩明海
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:253
专家分:749
注 册:2010-4-3
收藏
得分:0 
typedef struct student
{
    int num;
    struct student *next;
}stu;
以后就用stu定义结构体 变量就行了
再去检查一下你的if语句
2010-04-14 11:39
快速回复:两链表求交集
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015829 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved