void create(struct node *head); //declaring functions
void display(struct node *head);
struct node //creating a struct datatype for nodes
{
int coeff;
int power;
struct node* next;
};
struct node* poly1=NULL; //head pointer for a sample polynomial
void main()
{
int coeff,power,deg,i;
clrscr(); //main function
printf("n enter polynomial 1 ");
create(poly1);
display(poly1);
getch();
}
void create(struct node *head)
{
struct node *newnode,*temp;
int exp,num,n,i;
printf("n enter no. of terms in your expression=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newnode=(struct node*)malloc(sizeof(newnode));
newnode->next=NULL;
printf("n enter power=");
scanf("%d",&exp);
newnode->power=exp;
printf("n enter coefficient=");
scanf("%d",&num);
newnode->coeff=num;
if(head==NULL)
head=newnode;
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
}
void display(struct node *head)
{
struct node *temp;
temp=head;
while(temp->next!=NULL)
{
printf("%dx^%d",temp->coeff,temp->power);
temp=temp->next;
}
}
我的代码编译显示没有错误或警告,但我无法打印我的多项式函数。显示功能只打印 0 x^ 0 或根本没有值,我已经尝试了很多东西但它不起作用,有人可以指出我应该怎么做来纠正它。我正在使用C语言。
我的代码编译没有显示错误或警告
这很奇怪。添加缺少的包含并注释掉getch
并clrscr
这是编译器输出,甚至没有启用(几乎是强制性的(标志-Wall -Wextra
:
$ gcc main.c
main.c:4:20: warning: ‘struct node’ declared inside parameter list will not be visible outside of this definition or declaration
4 | void create(struct node *head); //declaring functions
| ^~~~
main.c:5:21: warning: ‘struct node’ declared inside parameter list will not be visible outside of this definition or declaration
5 | void display(struct node *head);
| ^~~~
main.c: In function ‘main’:
main.c:19:11: warning: passing argument 1 of ‘create’ from incompatible pointer type [-Wincompatible-pointer-types]
19 | create(poly1);
| ^~~~~
| |
| struct node *
main.c:4:26: note: expected ‘struct node *’ but argument is of type ‘struct node *’
4 | void create(struct node *head); //declaring functions
| ~~~~~~~~~~~~~^~~~
main.c:20:12: warning: passing argument 1 of ‘display’ from incompatible pointer type [-Wincompatible-pointer-types]
20 | display(poly1);
| ^~~~~
| |
| struct node *
main.c:5:27: note: expected ‘struct node *’ but argument is of type ‘struct node *’
5 | void display(struct node *head);
| ~~~~~~~~~~~~~^~~~
main.c: At top level:
main.c:24:8: error: conflicting types for ‘create’
24 | void create(struct node *head)
| ^~~~~~
main.c:4:6: note: previous declaration of ‘create’ was here
4 | void create(struct node *head); //declaring functions
| ^~~~~~
main.c:53:7: error: conflicting types for ‘display’
53 | void display(struct node *head)
| ^~~~~~~
main.c:5:6: note: previous declaration of ‘display’ was here
5 | void display(struct node *head);
您的编译器似乎已严重过时。
您的代码似乎存在许多问题,但一个明显的问题是:
newnode=(struct node*)malloc(sizeof(newnode));
铸造是不好的做法,但不应该引起任何实际问题,但对大小的论点是错误的。它应该是:
newnode=malloc(sizeof(*newnode));
这里你按值传递指针
create(poly1);
到您的函数
void create(struct node *head)
然后你设置head
head=newnode;
但只是修改局部变量head
,而你想修改poly1
。
一种可能性是像这样传递双指针:
create(&poly1);
...
void create(struct node **head)
...
if (*head == NULL)
*head = newnode;
现在你真的修改了全局变量poly1
,这是你想要的。