-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.Linked_list.c
51 lines (41 loc) · 1.01 KB
/
07.Linked_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<stdio.h>
#include<stdlib.h>
// Node structure
struct Node
{
int data;
struct Node * next;
};
void linkListTraversal(struct Node *ptr)
{
int count = 1;
while(ptr != NULL)
{
printf("Element %d : %d\n", count, ptr->data);
ptr = ptr->next;
count += 1;
}
}
int main()
{
struct Node *head, *second, *third, *fourth;
// Allocate memory for nodes
head = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
fourth = (struct Node *)malloc(sizeof(struct Node));
// Link head node with second node
head->data = 7;
head->next = second;
// Link second node with third node
second->data = 13;
second->next = third;
// Link third node with fourth node
third->data = 22;
third->next = fourth;
// Terminate list with fourth node
fourth->data = 66;
fourth->next = NULL;
linkListTraversal(head);
return 0;
}