-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraversal.c
52 lines (39 loc) · 1.01 KB
/
Traversal.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 {
int data;
struct node * next;
};
void linkedListTraversal(struct node* ptr)
{
while(ptr!=NULL)
{
printf("Element: %d\n", ptr->data);
ptr = ptr->next;
}
};
int main(){
struct node* head;
struct node* second;
struct node* third;
struct node* fourth;
// Allocate memory for node in the linked list in Heap
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 first and second Node
head->data = 7;
head->next = second;
// Link Second and third node
second->data = 11;
second->next = third;
// Link Second and third node
third->data = 51;
third->next = fourth;
//Terminate the list at third node
fourth->data = 15;
fourth->next = NULL;
linkedListTraversal(head);
return 0;
};