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语言。
在这里按值传递指针
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
。
我的代码编译未显示任何错误或警告
这很奇怪。添加缺少的包含并注释掉getch
之后,这是编译器的输出,甚至没有启用(几乎是强制性的)标志-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:17:4: warning: implicit declaration of function ‘clrscr’ [-Wimplicit-function-declaration]
17 | clrscr(); //main function
| ^~~~~~
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));
强制转换是不好的做法,但不应引起任何实际问题,但是sizeof的论点是错误的。应该是:
newnode=malloc(sizeof(*newnode));