-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedListCreationTraversal.c
52 lines (40 loc) · 1.17 KB
/
linkedListCreationTraversal.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
52
#include <stdio.h>
#include <stdlib.h>
struct node
{
/* data */
int data;
struct node *next;
};
void linkListTraversal(struct node *ptr)
{
while (ptr != NULL)
{
// here the ptr is pointing towards first node which is head
/* code */
printf("%d->", ptr->data);
ptr = ptr->next;
}
}
int main()
{
struct node *head = (struct node *)malloc(sizeof(struct node));
struct node *second = (struct node *)malloc(sizeof(struct node));
struct node *third = (struct node *)malloc(sizeof(struct node));
struct node *fourth = (struct node *)malloc(sizeof(struct node));
struct node *fifth = (struct node *)malloc(sizeof(struct node));
// putting data in head and pointing head to second
head->data = 4;
head->next = second;
// putting data in second and pointing second to third
second->data = 5;
second->next = third;
third->data = 6;
third->next = fourth;
fourth->data = 7;
fourth->next = fifth;
fifth->data = 8;
fifth->next = NULL;
// traversing the Array and showing all the linked list
linkListTraversal(head);
}