C中的数据结构-在列表的开头插入节点

w

我是C语言学习数据结构的新手。在跟随youtube视频教程时,我按如下方式编写了代码:

#include "stdlib.h"
#include "stdio.h"

struct Node {
    int data;
    struct Node* next;
}; 

struct Node* head;

void Insert(int x){
    struct Node* temp = (Node*)malloc(sizeof(struct Node));
    temp->data = x;
    temp->next = head;
    head = temp;
}

void Print(){
    struct Node* temp = head;
    printf("Now the list is: \n");
    while(temp != NULL){
        printf("%d\n", temp->data);
        temp = temp->next;
    }
}

int main(){
    head = NULL;
    printf("How many numbers?\n");
    int n,x,i;
    scanf("%d", &n);
    for(i=0;i<n;i++){
        printf("Enter the number \n");
        scanf("%d", &x);
        Insert(x);
        Print();
    }
}

但它一直在抱怨

aaa.c: In function ‘Insert’:
aaa.c:12:23: error: ‘Node’ undeclared (first use in this function)
  struct Node* temp = (Node*)malloc(sizeof(struct Node));
                       ^
aaa.c:12:23: note: each undeclared identifier is reported only once for each function it appears in
aaa.c:12:28: error: expected expression before ‘)’ token
  struct Node* temp = (Node*)malloc(sizeof(struct Node));

我对C和数据结构都相当陌生。谁能告诉我问题出在哪里?该代码主要是在列表的开头插入节点。提前致谢。

戈皮

下一行中的Node没有任何意义(struct Node *)

struct Node* temp = (Node*)malloc(sizeof(struct Node));//Wrong



struct Node* temp = (struct Node*)malloc(sizeof(struct Node));//Right way to use

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在c中使用自定义类型从节点列表中读取数据结构

来自分类Dev

图数据结构中的节点值

来自分类Dev

python中的事件列表数据结构

来自分类Dev

转换数据结构中的列表

来自分类Dev

C ++-错误列表的数据结构

来自分类Dev

在列表的开头插入节点

来自分类Dev

在列表的开头插入节点

来自分类Dev

它是C中的通用堆栈数据结构链接列表实现吗?

来自分类Dev

安全节点的数据结构?

来自分类Dev

JS中的树状数据结构允许最快的节点查找?

来自分类Dev

在HTS R中创建层级数据结构,节点

来自分类Dev

计算trie数据结构中节点的出现次数

来自分类Dev

节点js,替换嵌套数据结构中的出现

来自分类Dev

删除双向链表中的节点(数据结构)

来自分类Dev

在树数据结构中添加节点时递归

来自分类Dev

C ++设置保持插入顺序的数据结构

来自分类Dev

Haskell中树数据结构的邻接列表

来自分类Dev

从R中的列表制作数据结构

来自分类Dev

应用中的可修改列表使用什么数据结构?

来自分类Dev

在单链接列表(C)的开头添加节点

来自分类Dev

在没有实际插入的情况下在排序的数字列表或数组中找到插入位置的最快数据结构和/或算法是什么(在 c# 中)

来自分类Dev

C ++中的数据结构

来自分类Dev

MongoDB:插入复杂的数据结构

来自分类Dev

列表和子列表数据结构

来自分类Dev

从bing映射中提取数据以插入到自定义数据结构中

来自分类Dev

类似于Javascript列表的数据结构

来自分类Dev

链接列表数据结构理解

来自分类Dev

Swift DoublyLinked数据结构列表

来自分类Dev

追加列表/选择数据结构