| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 869 人关注过本帖
标题:链表的输入问题
只看楼主 加入收藏
_077张帅
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2021-8-31
结帖率:0
收藏
已结贴  问题点数:10 回复次数:0 
链表的输入问题
    代码的功能就是完成一元多项式的加法运算,问题出现在读取链表时,读取一个链表时代码并没有出现问题,但是当读取两个链表时,第一个链表在释放头结点时出现错误:unknown signal,第二链表读取无误,两个链表读取完成之后第一个链表尾指针发生了改变,导致报错,现在想请教一下各位大神代码报错原因。
代码如下:
#include<stdio.h>
#include<stdlib.h>

typedef struct PolyNode *Polynomial;
struct PolyNode
{
    int coef;
    int expon;
    Polynomial link;
};

int Compare(int e1,int e2)
{
    if (e1 > e2)
    {
        return 1;
    }
    else if (e1 < e2)
    {
        return -1;
    }
    else
    {
        return 0;
    }
}

void Attach(int c,int e,Polynomial *pRear)
{
    Polynomial P;

    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->coef = c;
    P->expon = e;
    (*pRear)->link = P;
    *pRear = P;
}

Polynomial ReadPoly()
{
    Polynomial Head,Rear,t;
    int c,e,i,N;
    scanf("%d",&N);

    Head = (Polynomial)malloc(sizeof(struct PolyNode));
    Head = Rear;
    for ( i = 0; i < N; i++)
    {
        scanf("%d %d",&c,&e);
        Attach(c,e,&Rear);
    }
   
    Rear->link = NULL;
    t = Head;
    Head = Head->link;
    free(t);
    t = NULL;

    return Head;
}

void PrintPoly(Polynomial P)
{
    int flag = 0;

    while (P)
    {
        if (!flag)
        {
            flag = 1;
        }
        else
        {
            printf(" ");
        }
        printf("%d %d",P->coef,P->expon);
        P = P->link;
    }
    printf("\n");
}

Polynomial PolyAdd(Polynomial P1,Polynomial P2)
{
    Polynomial front,rear,temp;
    int sum;

    front = (Polynomial)malloc(sizeof(struct PolyNode));
    rear = front;
    while (P1 && P2)
    {
        switch (Compare(P1->expon,P2->expon))
        {
        case 1:
            Attach(P1->coef,P1->expon,&rear);
            P1 = P1->link;
            break;
        case -1:
            Attach(P2->coef,P2->expon,&rear);
            P2 = P2->link;
            break;
        case 0:
            sum = P1->coef + P2->coef;
            Attach(sum,P1->expon,&rear);
            P1 = P1->link;
            P2 = P2->link;
            break;
        }
    }

    while (P1)
    {
        Attach(P1->coef,P1->expon,&rear);
        P1 = P1->link;
    }
     while (P2)
    {
        Attach(P2->coef,P2->expon,&rear);
        P2 = P2->link;
    }
    rear->link = NULL;
    temp = front;
    front = front->link;
    free(temp);

    return front;
}

int main()
{
    Polynomial P1,P2,PAdd,PMultiple;

    P1 = ReadPoly();
    P2 = ReadPoly();
    PAdd = PolyAdd(P1,P2);
    PrintPoly(PAdd);

    return 0;
}
搜索更多相关主题的帖子: 链表 link Head return int 
2021-08-31 11:16
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:10 
//online parser: https://www.bccn.net/run/
#include<stdio.h>
#include<stdlib.h>

typedef struct PolyNode *Polynomial;
struct PolyNode
{
    int coef;
    int expon;
    Polynomial link;
};

int Compare(int e1,int e2)
{
    if (e1 > e2)
    {
        return 1;
    }
    else if (e1 < e2)
    {
        return -1;
    }
    else
    {
        return 0;
    }
}

void Attach(int c,int e,Polynomial *pRear)
{
    Polynomial P;

    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->coef = c;
    P->expon = e;
    //follow
    printf(" (*pRear)->link = %p\n", (*pRear)->link);
    //(*pRear)->link = P;

    *pRear = P;
}

Polynomial ReadPoly()
{
    Polynomial Head,Rear,t;
    int c,e,i,N;
    N = 2;

    Head = (Polynomial)malloc(sizeof(struct PolyNode));
    Head = Rear;
    //for ( i = 0; i < N; i++)
    //{
        c = 25;
        e = 125;
        //follow
        printf("Rear = %p\n", &Rear);
        Attach(c,e,&Rear);

    //}
   
    //Rear->link = NULL;
    //t = Head;
    //Head = Head->link;
    //free(t);
    //t = NULL;

    return Head;
}

void PrintPoly(Polynomial P)
{
    int flag = 0;

    while (P)
    {
        if (!flag)
        {
            flag = 1;
        }
        else
        {
            printf(" ");
        }
        printf("%d %d",P->coef,P->expon);
        P = P->link;
    }
    printf("\n");
}

Polynomial PolyAdd(Polynomial P1,Polynomial P2)
{
    Polynomial front,rear,temp;
    int sum;

    front = (Polynomial)malloc(sizeof(struct PolyNode));
    rear = front;
    while (P1 && P2)
    {
        switch (Compare(P1->expon,P2->expon))
        {
        case 1:
            Attach(P1->coef,P1->expon,&rear);
            P1 = P1->link;
            break;
        case -1:
            Attach(P2->coef,P2->expon,&rear);
            P2 = P2->link;
            break;
        case 0:
            sum = P1->coef + P2->coef;
            Attach(sum,P1->expon,&rear);
            P1 = P1->link;
            P2 = P2->link;
            break;
        }
    }

    while (P1)
    {
        Attach(P1->coef,P1->expon,&rear);
        P1 = P1->link;
    }
     while (P2)
    {
        Attach(P2->coef,P2->expon,&rear);
        P2 = P2->link;
    }
    rear->link = NULL;
    temp = front;
    front = front->link;
    free(temp);

    return front;
}

int main()
{
    Polynomial P1,P2,PAdd,PMultiple;

    P1 = ReadPoly();
    //P2 = ReadPoly();
    //PAdd = PolyAdd(P1,P2);
    //PrintPoly(PAdd);

    puts("end of main");
    return 0;
}
2021-08-31 11:31
快速回复:链表的输入问题
数据加载中...
 
   



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

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