gpt4 book ai didi

c - 我的代码有什么问题吗?

转载 作者:行者123 更新时间:2023-11-30 18:43:36 26 4
gpt4 key购买 nike

最近我一直在制作堆栈项目,但我的推送节点代码有问题:

void push(struct stackNode *Stackptr , int data)
{
struct stackNode *conductor;

conductor = malloc(sizeof(struct stackNode));
if(conductor !=NULL)
{
conductor->data=data;
conductor->nxt=*Stackptr;
Stackptr = conductor ;
}
else{
printf("Wrong insertion");
}

}

但是它不会编译。如果您想在这里查看我的其余代码,它是:

#include<stdio.h>
#include<stdlib.h>

struct stackNode
{
int data;
struct stackNode *nxt;
};

struct stackNode *Stack;


void instructions()
{
printf(" Please Select a Number to do the following\n\n");
printf(" [1]Push a value\n");
printf(" [2]Pop a value\n");
printf(" [2]Peek a node\n");
printf(" [3]Display\n");
printf(" [4]Exit\n ");
}

int main ()
{
int value;
int choice;
Stack = NULL;

instructions();

do
{
scanf("%d",&choice);
system("cls");


if(choice == 1)
{
printf("Enter your Desired Digit: ");

}

if(choice == 2)
{
printf(" ");
}
if(choice == 3)
{

printf(" ");
}
if(choice == 4 )
{

printf("bye!");
return 0;
}



}while(choice !=4);

getch();
}

void push(struct stackNode *Stackptr , int data)
{
struct stackNode *conductor;

conductor = malloc(sizeof(struct stackNode));
if(conductor !=NULL)
{
conductor->data=data;
conductor->nxt=*Stackptr;
Stackptr = conductor ;
}
else{
printf("Wrong insertion");
}

}

最佳答案

在推送函数中,您分配的是堆栈节点,而不是指向堆栈节点的指针。

这是错误:

test.c:76:31: error: incompatible types when assigning to type ‘struct stackNode *’ from type ‘struct stackNode’

删除 * 会分配指针,这就是您想要的。

所以这有效:

conductor->nxt=Stackptr;

但是,该程序还存在其他问题。

关于c - 我的代码有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5551194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com