-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23 jan 2024 basic creation of a linkend list.c
84 lines (68 loc) · 1.59 KB
/
23 jan 2024 basic creation of a linkend 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
point 1 - linked list is a linear data structure
point 2 - it is of 3 type - singly , doubly , circular (doubly circular is also possible)
point 3 - 2 ways of creation - 1st - array 2nd - structure + pointers
structure of a singular inked list
struct node
{
int data ;
struct node* next;
}
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void approach1()
{
// brut force approach for a linked list
struct node head;
struct node node1;
struct node node2;
struct node node3;
struct node node4;
struct node node5;
// giving data and connecting nodes
head.data = 0;
head.next = &node1;
node1.data = 1;
node1.next = &node2;
node2.data = 2;
node2.next = &node3;
node3.data = 3;
node3.next = &node4;
node4.data = 4;
node4.next = &node5;
node5.data = 5;
node5.next = NULL;
// a temp pointer to traverse through the list
struct node *temp = &head;
// for printing the data in the nodes
for (int i = 0; i < 6; i++)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
int approach2()
{
struct node *head, *one, *temp;
head = (struct node *)malloc(sizeof(struct node));
one = (struct node *)malloc(sizeof(struct node));
head->data = 5;
head->next = one;
one->data = 10;
one->next = NULL;
temp = head; // for traversing data through nodes...
for (int i = 0; i < 2; i++)
{
printf("%d\n", temp->data);
temp = temp->next;
}
free(head);
free(one);
struct node *head, *one, *temp;
}