print linked lists in c

Solutions on MaxInterview for print linked lists in c by the best coders in the world

showing results for - "print linked lists in c"
Kinsley
16 Oct 2020
1/*
2* node - new struct
3* @value: data for the node
4* @*next: points to the next node
5*
6* Desc: contains value and points to *next 
7*/
8struct node{
9	int value;
10	struct node *next;
11};
12typedef struct node node_t;
13
14/**
15* printlist - prints the linked list
16* @temp: temporary variable used as a counter throughout the linked list
17*
18* Desc: print the value of the list if not NULL then update temp
19* Return: nothing
20*/
21void printlist(node_t *head)
22{
23        node_t *temp = head;
24
25        while (temp != NULL)
26        {
27                printf("%d - ", temp->value);
28                temp = temp->next;
29        }
30        printf("\n");
31
32}
33
34/*
35* main - start of this program
36* @n1: node 1
37* @n2: node 2
38* @n3: node 3
39* @head: start of linked list
40* 
41* Desc: assigns value to the nodes and link them
42* Return: 0 on success
43*/
44int main()
45{
46	node_t n1, n2, n3;
47	node_t *head;
48
49	n1.value = 1;
50	n2.value = 2;
51	n3.value = 3;
52
53	head  = &n2;
54	n2.next = &n1;
55	n1.next = &n3;
56	n3.next = NULL;
57
58	printlist(head);
59	return (0);
60}