-1

My code:

    #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;
            }
        }
    }

OUTPUT:

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

Got this output . Tried many times using the typedef keyword also. But does not work . Thanks in advance !

SaeX
  • 17,240
  • 16
  • 77
  • 97
  • [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). In your case, it is very much applicable, as yoy did not include `stdlib.h` – Sourav Ghosh Nov 26 '15 at 07:34
  • Also, be aware when looking at documentation on linked-lists, you may run into two different types. (1) the standard *head-tail* list that includes a *head* node (that may/may not contain data) and where the last item, or tail, is set to `NULL`, and (2) a *circular* list where the first node is always a data containing node and the last node `->next` pointer points back to the first node. (allowing traversal of all nodes beginning at any node) The list type is not always explicitly stated in the various documentation you may find on the web, so look carefully at the type. – David C. Rankin Nov 26 '15 at 08:37
  • http://ideone.com/QsMuRv – BLUEPIXY Nov 26 '15 at 08:54

1 Answers1

0

.....Your code is messy......

You need to add

 typedef struct node node;

before the forward declaration of functions so that the type node is known by the compiler. Also, remove the typedef from

  typedef struct node *head , *p , *c;
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Looks like she is trying to typedef the struct when she defines the struct. But she has "typedef struct node {...};" when it should be "typedef struct {...} node;" . Edit: OTOH, this probably isn't the only thing wrong with this code, maybe not worth arguing about the best way to typedef :P – AlexPogue Nov 26 '15 at 07:34