免檒 2024-01-02 21:35 采纳率: 87.5%
浏览 5
已结题

在创建链表时结构体的使用

这是全部代码:(正确的)

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>

//尾插法
typedef struct _Node
{
    int data;
    struct _Node* next;
}Node;

Node* Createlist(int n)
{
    int i = 0;
    Node* head, * tail, * p;
    head = tail = NULL;

    for (i = 0; i < n; i++)
    {
        p = (Node*)malloc(sizeof(Node));
        p->next = NULL;
        scanf("%d", &p->data);

        if (head == NULL)
        {
            head = tail = p;
        }
        else
        {
            tail->next = p;
            tail = p;
        }
    }
    return head;
}


void Printlist(Node* head)
{
    while (head != NULL)
    {
        printf("%d -> ", head->data);
        head = head->next;
    }
    printf("NULL");
}

int main()
{
    int n;
    scanf("%d", &n);

    Node* head = NULL;
    head = Createlist(n);
    Printlist(head);

    return 0;
}

但是 ,这一段代码中:

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

这里的struct _Node* next;换为Node *next;

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

为什么编译器会报错;会出现好多错误;

  • 写回答

3条回答 默认 最新

  • mlem_init 2024-01-03 09:04
    关注

    c语言不支持这种语法,你可以将struct单独拿到下面,typedef struct node node_t;作为单独一句放到struct的上面,就可以在struct中使用node_t*这种数据类型了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月17日
  • 已采纳回答 1月9日
  • 创建了问题 1月2日

悬赏问题

  • ¥15 岛津txt格式文件转nirs格式
  • ¥15 石墨烯磁表面等离子体
  • ¥15 angular 项目无法启动
  • ¥15 安装wampserver,图标绿色,但是无法进入软件
  • ¥15 C++ MFC 标准库 加密解密解惑
  • ¥15 两条数据合并成一条数据
  • ¥15 Ubuntu虚拟机设置
  • ¥15 comsol三维模型中磁场为什么没有“速度(洛伦兹项)”这一选项
  • ¥15 electron 如何实现自定义安装界面
  • ¥15 关于#linux#的问题:子进程C运行“ls –l”命令,且显示“C运行ls-l命令”(语言-c语言)