C 创建并显示单链表

C 创建并显示单链表,c,C,我的代码: #include <stdio.h> node * create(int); void disp(node *,int); typedef struct node { int data; struct node *next; }; node * create(int); void disp(node *,int); typedef struct node *head

我的代码:

    #include <stdio.h>

    node * create(int);
    void disp(node *,int);

    typedef struct node
    {
        int data;
        struct node *next;
    };
    node * create(int);
    void disp(node *,int);
    typedef struct node *head , *p , *c;
    int i,n;
    int main()
    {
        printf("\n Enter the number of nodes:");
        scanf("%d",&n);
        c=create(n);
        disp(head,n);
        return 0;
    }

    node * create(int n)
    {
        head = (node *)malloc(sizeof(node));

        scanf("%d", &head->data);
        head->next = NULL;
        p=head;
        for(i=1;i<n;i++)
        {
                p=(node*)malloc(sizeof(node));
                scanf("%d",&p->data);
                p=p->next;
                p->next=NULL;
        }
        return head;
    }

    void disp(node *head , int n)
    {
        p=head;
        while(p!=NULL)
        {
            for(i=0;i<n;i++)
            {
                printf("%d",p->data);
                p=p->next;
            }
        }
    }
得到了这个输出。还多次尝试使用typedef关键字。但是不起作用。提前谢谢

…你的代码乱七八糟

您需要添加

 typedef struct node node;
在函数的前向声明之前,以便编译器知道类型
节点
。另外,从中删除
typedef

  typedef struct node *head , *p , *c;

看起来她在定义结构时正在尝试键入定义结构。但是她有“typedef struct node{…};”,而它应该是“typedef struct{…}node;”。编辑:OTOH,这可能不是这段代码唯一的错误,也许不值得争论输入def:P的最佳方式。在您的情况下,它非常适用,因为您没有包含
stdlib.h
。另外,请注意,在查看链接列表上的文档时,您可能会遇到两种不同的类型。(1) 标准头尾列表包括一个头部节点(可能/可能不包含数据),其中最后一项或尾部设置为
NULL
,以及(2)一个循环列表,其中第一个节点始终是一个包含数据的节点,最后一个节点
->next
指针指向第一个节点。(允许从任何节点开始遍历所有节点)列表类型并不总是在web上的各种文档中明确说明,因此请仔细查看该类型。
  typedef struct node *head , *p , *c;