i am unable to print the linked list


i am unable to print the linked list


#include<stdio.h>
#include<stdlib.h>

main()
{
struct node
{
int data;
struct node *next;
};

struct node *first=(struct node*)malloc(sizeof(struct node));
struct node *second=(struct node*)malloc(sizeof(struct node));
struct node *third=(struct node*)malloc(sizeof(struct node));

scanf("%d %d %d",&(first->data),&(second->data),&(third->data));
first->next=second;
second->next=third;
third->next=NULL;
struct node *t=(struct node *)first;
f(t);
}

f(struct node *a)
{
while(a!=NULL)
{
printf("%d",a->data);
a= a->next;
}
}



the code above is giving a warning and an error "struct node declared inside parameter list" and "dereferencing pointer to incomplete type"



kindly help me making the code run and solving error.





Where is the structure defined? It will only be defined in that scope.
– Some programmer dude
Jun 29 at 8:29





what do you mean..?
– Shivam Singh
Jun 29 at 8:34





Perhaps you should take some time to learn about scopes? Any beginners book or tutorial or course should have mentioned it, and that things defined in one scope is only available in that (and nested) scopes.
– Some programmer dude
Jun 29 at 8:36




2 Answers
2



There are several issues.


struct node


main


f



This is what your program should look like:


#include <stdio.h>
#include <stdlib.h>

struct node // structure declared at global scope
{
int data;
struct node *next;
};

void f(struct node *a); // declare function

int main() // function has now return type int
{
struct node *first = (struct node*)malloc(sizeof(struct node));
struct node *second = (struct node*)malloc(sizeof(struct node));
struct node *third = (struct node*)malloc(sizeof(struct node));

scanf("%d %d %d", &(first->data), &(second->data), &(third->data));
first->next = second;
second->next = third;
third->next = NULL;
struct node *t = (struct node *)first;
f(t);
}

void f(struct node *a) // function has now return type void
{
while (a != NULL)
{
printf("%d", a->data);
a = a->next;
}
}



Disclaimer: this program just compiles correctly without warnings, but I didn't check if it actually makes sense.





The definition of the f function actually is outside the main function. And that is why f doesn't see the struct node declaration.
– CiaPan
Jun 29 at 8:41


f


main


f


struct node





@CiaPan oh, you're right. Edited. That's why the last point of my list is important ;-)
– Jabberwocky
Jun 29 at 8:42





I agree. I was going to answer this question too, but you were faster. And my answer was essentialy same as yours, except I put the last point at the top of the list. Programs are for computers, but the source code is for humans. So it should always be as readable as possible.
– CiaPan
Jun 29 at 8:46





@Jabberwocky it compiled and made a sense too ;-) it is a program to print a linkedlist recursively.
– Shivam Singh
Jun 29 at 8:47





@ShivamSingh yes, it seems to be correct, but there is no recursion though.
– Jabberwocky
Jun 29 at 8:48


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* ===== Define structure outside function ===== */
/*
Use `typedef` to rename the data type.
`node_t` is equal with `struct node_t`, and
`node` is equal with `struct node_t *`.
*/
typedef struct node_t
{
int data;
struct node_t *next;
} node_t, *node;

/* Define link list
*/
typedef struct list_t
{
node head;
node rear;
int size;
} list_t, *list;

list list_create()
{
list l = NULL;

if ((l = malloc(sizeof(list_t))) == NULL)
{
exit(-1);
}
memset(l, 0, sizeof(list_t));

return l;
}

int list_destroy(list *pl)
{
node cur;
node prev;

if (pl == NULL || *pl == NULL)
return -1;

cur = (*pl)->head;
while (cur != NULL)
{
prev = cur;
cur = cur->next;
free(prev);
}

free(*pl);
*pl = NULL;

return 0;
}

int list_push(list l, int data)
{
node n;

if (l == NULL)
return -1;

if ((n = malloc(sizeof(node_t))) == NULL)
{
exit(-1);
}

n->data = data;
n->next = NULL;

if (l->head == NULL)
{
l->head = n;
}
else
{
l->rear->next = n;
}

l->rear = n;
l->size++;

return 0;
}

int list_pop(list l, int *pdata)
{
if (l == NULL || l->head == NULL)
{
return -1;
}

*pdata = l->head->data;
if (l->head == l->rear)
{
l->rear = NULL;
}
l->head = l->head->next;

l->size--;

return 0;
}

void list_foreach(list l, void (*func)(int data, void *arg), void *arg)
{
node cur = l->head;

while (cur != NULL)
{
func(cur->data, arg);
cur = cur->next;
}
}


static void print_node(int data, void *arg)
{
printf("%dn", data);
}

int main(int argc, char *argv)
{
int i;
int d;
list l;

l = list_create();

for (i = 0; i < 5; i++)
{
scanf("%d", &d);
getchar();
list_push(l, d);
}

list_foreach(l, print_node, NULL);

printf("Pop: [status %d], ", list_pop(l, &d));
printf("[value %d]n", d);

printf("Pop: [status %d], ", list_pop(l, &d));
printf("[value %d]n", d);

list_foreach(l, print_node, NULL);

list_destroy(&l);

return 0;

}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift