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

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

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

#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日