| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 321 人关注过本帖
标题:新手入门之链表:错误在哪
只看楼主 加入收藏
studythec
Rank: 1
来 自:安徽阜阳
等 级:新手上路
帖 子:31
专家分:5
注 册:2010-3-13
结帖率:100%
收藏
 问题点数:0 回复次数:1 
新手入门之链表:错误在哪
#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 18:36
studythec
Rank: 1
来 自:安徽阜阳
等 级:新手上路
帖 子:31
专家分:5
注 册:2010-3-13
收藏
得分:0 
题目是输入两个链表A,B,找出它们的交集,放入C中

菜鸟一个,在校学习,多多指教!
2010-04-11 18:39
快速回复:新手入门之链表:错误在哪
数据加载中...
 
   



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

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