forked from NitulKalita/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingly linked list.c
More file actions
51 lines (44 loc) · 1.01 KB
/
singly linked list.c
File metadata and controls
51 lines (44 loc) · 1.01 KB
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>
// Creating a node
struct node {
int value;
struct node* next;
};
// prints linked list value
void printLinkedlist(struct node* p) {
while (p != NULL) {
printf("%d ", p->value);
p = p->next;
}
}
int main() {
// Initialize nodes
struct node* head;
struct node* one = NULL;
struct node* two = NULL;
struct node* three = NULL;
struct node* four = NULL;
struct node* five = NULL;
// Allocate memory
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
four = malloc(sizeof(struct node));
five = malloc(sizeof(struct node));
// Assigning value
one->value = 1;
two->value = 2;
three->value = 3;
four->value = 4;
five->value = 5;
// Connect nodes
one->next = two;
two->next = three;
three->next = four;
four->next = five;
five->next = NULL;
// printing node-value
head = one;
printLinkedlist(head);
}