注册 登录
编程论坛 数据结构与算法

为什么我的程序编译链接都没有错,得出来的结果却是零,而且还有一个NULL pointer assignment,大神帮帮忙

我是个大文盲 发布于 2018-09-23 20:31, 1370 次点击
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define NULL 0
typedef struct{
   int coaf;
   int expn;
}polym;
typedef struct LNode
{
    polym data;
    struct LNode *next;
}Node,*sqlist;
void  inistlist(sqlist *a,int n)
{
      a=(sqlist*)malloc(n*sizeof(polym)) ;
      printf("Please input the 0 Node:");
      scanf("%d%d",(*a)->data.coaf,(*a)->data.expn);
      (*a)->next=NULL;
}
void creatlist(sqlist *a,int n)
{
     int i;
     Node *p=NULL;
     inistlist(a,n);
     for(i=1;i<n;i++)
     {
       printf("Please input the %d Node:",i);
       scanf("%d%d",&p->data.coaf,&p->data.expn);
       (*a)->next=p;
     }
      p->next=NULL;
}
double sumarr(sqlist *a,int x)
{
       int i;
       double sum=0,temp;
       while((*a)->next!=NULL)
       {
            temp=pow(x,(*a)->data.expn);
            sum+= (*a)->data.coaf*temp;
            *a=(*a)->next;
       }
       return sum;
}
int main()
{
    sqlist *a=NULL;
    int n,x;
    double sum;
    printf("Please input the n,x:");
    scanf("%d%d",&n,&x);
    creatlist(a,n);
    sum=sumarr(a,x);
    printf("Sum=%f\n",sum);
    return 0;
}
2 回复
#2
林月儿2018-09-23 21:12
(*a)代表结构体对象吧?后面加箭头是指针的写法。
先打印这个n,x是否被读取,再检查这个链表创建流程

createlist好像没有像int函数那样申请内存的操作,也看不出是头插法还是尾插法创建
输出过程变量确认下细节吧
#3
MeandC2018-09-24 17:59
#define NULL 0;是不需要的,我用DEV C++编译时这行出现错误
NULL本来就表示空
还有void creatlist这个函数里的输入语句有问题
要像这样scanf("%d",&(p->data)),取址符后面要用括号括起来,亲测没括号运行时输入会出现问题。希望对你有用。
1