创建并显示单链表

时间:2015-11-26 07:26:10

标签: 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 , *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;
            }
        }
    }

输出

User@Sujata:~/Desktop]$ gcc ll.c 
ll.c:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:4: error: expected ‘)’ before ‘*’ token
ll.c:10: warning: useless storage class specifier in empty declaration
ll.c:11: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:12: error: expected ‘)’ before ‘*’ token
ll.c: In function ‘main’:
ll.c:19: error: expected identifier or ‘(’ before ‘=’ token
ll.c:20: error: expected expression before ‘head’
ll.c: At top level:
ll.c:24: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:41: error: expected ‘)’ before ‘*’ token

得到了这个输出。多次尝试使用typedef关键字。但是不起作用。提前谢谢!

1 个答案:

答案 0 :(得分:0)

.....你的代码很乱......

您需要添加

 typedef struct node node;
在函数的前向声明之前

,以便编译器知道类型node。另外,从

中删除typedef
  typedef struct node *head , *p , *c;