我无法使用链表打印多项式表达式 void创建(结构节点*head)//声明函数 无效显示(结构节点*头部); 结构节点//为节点创建结构数据类型 { 内因系数; 整数幂; 结构节点*下一步; }; 结构节点*poly1=NULL//样本多项式的头指针 void main() { 内部系数,功率,度,i; clrsc();//主函数 printf(“\n输入多项式1”); 创建(poly1); 显示器(1); getch(); } 创建空(结构节点*头) { 结构节点*newnode,*temp; int exp,num,n,i; printf(“\n在表达式中输入术语的编号=”); scanf(“%d”和“&n”); 对于(i=0;不精确=NULL; printf(“\n输入power=”); scanf(“%d”,和exp); newnode->power=exp; printf(“\n输入系数=”); scanf(“%d”和&num); newnode->coeff=num; if(head==NULL) 头=新节点; 其他的 { 温度=水头; while(临时->下一步!=NULL) { 温度=温度->下一步; } temp->next=newnode; } } } 无效显示(结构节点*头部) { 结构节点*temp; 温度=水头; while(临时->下一步!=NULL) { printf(“%dx^%d”,温度->系数,温度->功率); 温度=温度->下一步; } }
我的代码编译没有显示错误或警告,但我无法打印多项式函数。显示函数仅打印0 x^0或根本没有值,我尝试了许多方法,但都不起作用,有人能指出我应该如何更正它吗?我使用的是C语言。这里您可以按值传递指针我无法使用链表打印多项式表达式 void创建(结构节点*head)//声明函数 无效显示(结构节点*头部); 结构节点//为节点创建结构数据类型 { 内因系数; 整数幂; 结构节点*下一步; }; 结构节点*poly1=NULL//样本多项式的头指针 void main() { 内部系数,功率,度,i; clrsc();//主函数 printf(“\n输入多项式1”); 创建(poly1); 显示器(1); getch(); } 创建空(结构节点*头) { 结构节点*newnode,*temp; int exp,num,n,i; printf(“\n在表达式中输入术语的编号=”); scanf(“%d”和“&n”); 对于(i=0;不精确=NULL; printf(“\n输入power=”); scanf(“%d”,和exp); newnode->power=exp; printf(“\n输入系数=”); scanf(“%d”和&num); newnode->coeff=num; if(head==NULL) 头=新节点; 其他的 { 温度=水头; while(临时->下一步!=NULL) { 温度=温度->下一步; } temp->next=newnode; } } } 无效显示(结构节点*头部) { 结构节点*temp; 温度=水头; while(临时->下一步!=NULL) { printf(“%dx^%d”,温度->系数,温度->功率); 温度=温度->下一步; } },c,pointers,data-structures,linked-list,polynomials,C,Pointers,Data Structures,Linked List,Polynomials,我的代码编译没有显示错误或警告,但我无法打印多项式函数。显示函数仅打印0 x^0或根本没有值,我尝试了许多方法,但都不起作用,有人能指出我应该如何更正它吗?我使用的是C语言。这里您可以按值传递指针 void create(struct node *head); //declaring functions void display(struct node *head); struct node
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;
}
}
发挥你的作用
create(poly1);
然后设置head
void create(struct node *head)
但是只修改局部变量head
,而要修改poly1
一种可能是像这样传递双指针:
head=newnode;
现在您真正修改了全局变量poly1,这正是您想要的
我的代码编译没有显示错误或警告
这很奇怪。添加missing includes并注释掉getch
和CLRSC
后,这是编译器输出,它甚至没有启用(几乎是强制性的)标志-Wall-Wextra
:
create(&poly1);
...
void create(struct node **head)
...
if (*head == NULL)
*head = newnode;
您的编译器似乎已严重过时
您的代码似乎存在许多问题,但一个明显的问题是:
$ 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);
铸造是不好的做法,但不应导致任何实际问题,但sizeof的论点是错误的。它应该是:
newnode=(struct node*)malloc(sizeof(newnode));
没有警告?我收到了大量关于您的代码的警告。欢迎使用堆栈溢出!请将您的代码减少到问题的一部分。您当前的代码包含了许多与您的问题无关的内容-最小的样本通常看起来类似于良好的单元测试:仅执行一个任务,输入值指定用于再现性。
newnode=malloc(sizeof(*newnode));