标题:从文件中读取数据建立顺序表,求两个集合的并运算 有一个错误,不知道为什 ...
只看楼主
花开半夏12
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-3-1
结帖率:0
 问题点数:0 回复次数:2 
从文件中读取数据建立顺序表,求两个集合的并运算 有一个错误,不知道为什么,求大神解答!
#include <stdio.h>
#include <stdlib.h>
#include  <malloc.h>
typedef struct SeqList
{
    int data;
    struct SeqList *next;
}seqlist;
void Print(seqlist *p)
{
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
void Addlist(seqlist *la,seqlist *lb)
{
    seqlist *lc,*ld;
    lc=(seqlist *)malloc(sizeof(seqlist));
    while(la!=NULL&&lb!=NULL)
    {
        if(la->data<lb->data)
        {
            ld=(seqlist *)malloc(sizeof(seqlist));
            ld->data=la->data;
            lc->next=ld;
            lc=ld;
            la=la->next;
        }
        else if(la->data>lb->data)
        {

            ld=(seqlist *)malloc(sizeof(seqlist));
            ld->data=lb->data;
            lc->next=ld;
            lc=ld;
            lb=lb->next;
        }
        else
        {
             ld=(seqlist *)malloc(sizeof(seqlist));
             ld->data=la->data;
             lc->next=ld;
             lc=ld;
             la=la->next;
             lb=lb->next;
        }
    }
    while(la==NULL)
    {
        ld=(seqlist *)malloc(sizeof(seqlist));
        ld->data=lb->data;
        lc->next=ld;
        lc=ld;
        lb=lb->next;
    }
    while(lb==NULL)
    {
        ld=(seqlist *)malloc(sizeof(seqlist));
        ld->data=la->data;
        lc->next=ld;
        lc=ld;
        la=la->next;
    }
    lc->next=NULL;
    Print(lc);
}
int main()
{
    seqlist *p,*q,*pp,*qq,*ppp,*qqq;
    p=(seqlist *)malloc(sizeof(seqlist));
    q=(seqlist *)malloc(sizeof(seqlist));
    ppp=p;
    qqq=q;
    FILE *fpa,*fpb;
    int c,i;
    if((fpa=fopen("a.txt","r"))==NULL)
    {
        printf("can't open file a.txt\n");
        exit(1);
    }
    if((fpb=fopen("b.txt","r"))==NULL)
    {
        printf("can't open file b.txt\n");
        exit(1);
    }
    fscanf(fpa,"%d\n",&c);
    printf("%d\n",c);
    for(i=0;i<c;i++)
    {
        pp=(seqlist *)malloc(sizeof(seqlist));
        fscanf(fpa,"%d ",&pp->data);
        printf("%d ",pp->data);
        p->next=pp;
        p=pp;
    }
    fclose(fpa);
    printf("\n");
    fscanf(fpb,"%d\n",&c);
    printf("%d\n",c);
    for(i=0;i<c;i++)
    {
        qq=(seqlist *)malloc(sizeof(seqlist));
        fscanf(fpb,"%d ",&qq->data);
        printf("%d ",qq->data);
        q->next=qq;
        q=qq;
    }
    fclose(fpb);
    printf("\n");
    Addlist(ppp->next,qqq->next);
    return 0;
 }
搜索更多相关主题的帖子: malloc data next printf sizeof 
2018-03-02 12:01
Jeyyion_han
Rank: 5Rank: 5
等 级:职业侠客
威 望:4
帖 子:207
专家分:301
注 册:2018-1-22
得分:0 
Print(lc);   是不是 Printf(lc);
2018-03-02 12:18
花开半夏12
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-3-1
得分:0 
回复 2楼 Jeyyion_han
是调用Print()这个函数,不能是printf吧
2018-03-02 13:41



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-484946-1-1.html




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

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