gpt4 book ai didi

c - DevC++ 中未声明“Node”(在此函数中首次使用)

转载 作者:行者123 更新时间:2023-11-30 17:15:46 25 4
gpt4 key购买 nike

我有以下代码在 LinkedList 中插入元素。我一直在关注教程,但无法发现此代码中的错误。

我正在使用 DEVC++,它给了我一个编译时错误: [错误]“节点”未声明(在此函数中首次使用)

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

struct Node{
int data;
struct Node* next;
};
struct Node* head; //global variable
int main(){
head = NULL; //empty list
int n, i, x;
printf("How many numbers would you like to enter");
scanf("%d", &n);
for(i=0; i<n; i++){
printf("Enter the number you want to add to list:");
scanf("%d", &x);
Insert(x);
Print();
}
}


void Insert(int x){
Node* temp= (Node*)malloc(sizeof(struct Node)); //I get error here
temp->data = x;
//temp->next = NULL; //redundant
//if(head!= NULL){
temp->next = head; //temp.next will point to null when head is null nd otherwise what head was pointing to
//}
head = temp;
}

void Print(){
struct Node* temp1 = head; //we dont want tomodify head so store it in atemp. bariable and then traverse
while(temp1 != NULL){
printf(" %d", temp1->data);
temp1= temp1->next;
}
printf("\n");
}

最佳答案

Node* temp= (Node*)malloc(sizeof(struct Node)) 更改为 struct Node* temp = (struct Node*)malloc(sizeof(struct Node))

关于c - DevC++ 中未声明“Node”(在此函数中首次使用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29874448/

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