-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtut14.c
43 lines (35 loc) · 833 Bytes
/
tut14.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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};
void traversal(struct node *ptr){
while (ptr!=NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}
int main()
{
struct node* head;
struct node* second;
struct node* third;
//Allocate memory for nodes 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));
//Link first and second nodes
head->data = 7;
head->next = second;
//Link second and third nodes
second->data = 10;
second->next = third;
//TTerminate the list at third node
third->data = 2;
third->next = NULL;
traversal(head);
return 0;
}