-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkList protocols.c
121 lines (104 loc) · 3.32 KB
/
linkList protocols.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
1. Initialization
Purpose: Initialize a new linked list and set the node count to zero.
Steps:
Allocate memory for a new linked list node.
Set the total_nodes field to zero.
Call the node initializer function.
*/
// Initialization
struct st_node *list = malloc(sizeof(struct st_node));
list->total_nodes = 0;
list = cs_node_initializer(list);
/*
2. Build the Node Chain
Purpose: Create a chain of nodes within a loop to prepare for adding pages.
Steps:
Initialize an index variable to zero.
Use a loop to add nodes to the chain.
Increment the index after adding each node.
Optionally, reset the current node pointer to the head of the list.
*/
// Build the Node Chain
int index = 0;
int node_quantity = ...; // Define the number of nodes needed.
while (index < node_quantity) {
list = cs_add(list); // Add a new node
++index;
}
index = 0;
list = cs_element_at(list, index); // Reset to the first node if needed.
/*
3. Add Pages to Nodes
Purpose: Populate each node in the linked list with data (pages).
Steps:
Access the current node.
Allocate memory for the page data.
Populate the page data.
Optionally, move to the next node within the loop.
*/
// Add Pages to Nodes
list = cs_element_at(list, 0); // Start at the first node
index = 0;
while (index < list->total_nodes) {
// Allocate memory for the page data
struct st_number *number = malloc(sizeof(struct st_number));
list->page = number;
// Populate the page data
((struct st_number*)list->page)->number = index * 100;
// Move to the next node if needed
++index;
if (index < list->total_nodes)
list = cs_element_at(list, index);
}
index = 0;
list = cs_element_at(list, index); // Reset to the first node if needed.
/*
4. Combined Node Adding and Page Adding
Purpose: Simplify node and page creation within a loop by combining steps 2 and 3.
Steps:
Initialize an index variable to zero.
Determine the number of nodes and pages needed.
Use a loop to add nodes and populate pages.
Increment the index after each iteration.
*/
// Combined Node Adding and Page Adding
int index = 0;
int pages_needed = ...; // Define the total number of nodes and pages needed.
while (index < pages_needed) {
// Add a new node
list = cs_add(list);
// Allocate memory for the page data
struct st_number *number = malloc(sizeof(struct st_number));
list->page = number;
// Populate the page data if needed
// Increment the index
++index;
}
/*
5. Free Pages from Nodes
Purpose: Properly release memory allocated for pages within the nodes.
Steps:
Access each node within a loop.
Free memory allocated for the page data.
Optionally, reset the current node pointer to the head of the list.
*/
// Free Pages from Nodes
index = 0;
while (index < list->total_nodes) {
list = cs_element_at(list, index);
free((struct st_number*)list->page); // Free page data
++index;
}
index = 0;
list = cs_element_at(list, index); // Reset to the first node if needed.
/*
6. Destroy the Node Chain
Purpose: Free memory associated with the entire linked list and its nodes.
Steps:
Use a function (e.g., cs_clear_nodes) to release memory for the linked list and nodes.
Optionally, free the main list structure itself.
*/
// Destroy the Node Chain
cs_clear_nodes(list); // Free memory for the entire linked list and its nodes
free(list); // Optionally free the main list structure