-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.h
More file actions
154 lines (122 loc) · 2.92 KB
/
Copy pathlinkedlist.h
File metadata and controls
154 lines (122 loc) · 2.92 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define NODE_nodeSignature 110
#define ERROR_MALLOC_FAILED 2
#define ERROR_NODE_NULL 3
typedef struct
{
void* nodeData;
struct NODE* nextNode;
char nodeSignature;
} NODE;
/// Debug purpose
void print_nodes_int(NODE** head)
{
NODE* current = *head;
while(current != NULL)
{
printf("node value: %d\n",(int)current->nodeData);
current = current->nextNode;
}
printf("\n\n");
}
void print_nodes_float(NODE** head)
{
NODE* current = *head;
while(current != NULL)
{
printf("node value: %f\n",(int)current->nodeData);
current = current->nextNode;
}
printf("\n\n");
}
void print_nodes_char(NODE** head)
{
NODE* current = *head;
while(current != NULL)
{
printf("node value: %c\n",(int)current->nodeData);
current = current->nextNode;
}
printf("\n\n");
}
void print_nodes_string(NODE** head)
{
NODE* current = *head;
while(current != NULL)
{
printf("node value: %s\n",(char*)current->nodeData);
current = current->nextNode;
}
printf("\n\n");
}
/// Clears the nodes from begin to end
void delete_all_nodes(NODE** head)
{
NODE* current = *head;
while(current != NULL)
{
NODE* temp = current;
current = current->nextNode;
delete_node(temp);
}
}
/// Creates a new node with a given value
int create_node(void* node_nodeData, NODE** _node)
{
NODE* node = (NODE*) malloc(sizeof(NODE));
if(node == NULL)
{
return ERROR_MALLOC_FAILED;
}
node->nodeData = node_nodeData;
node->nodeSignature = NODE_nodeSignature;
node->nextNode = NULL;
*_node = node;
return TRUE;
}
/// Creates a node with nodeData set to NULL
int create_node_empty(NODE** node)
{
return create_node(NULL,node);
}
/// Frees the memory the node has being using, sets the value to NULL
int delete_node(NODE* node)
{
if(node == NULL || node->nodeSignature != NODE_nodeSignature)
return ERROR_NODE_NULL;
node->nodeData = NULL;
node->nextNode = NULL;
node->nodeSignature = NULL;
free(node);
return TRUE;
}
/// Adds a node to the begin of the list, makes the added node the new head
int push(NODE** head, void* nodeData)
{
if((*head)->nodeData == NULL)
{
/// No need to create a new node since the head has no data
(*head)->nodeData = nodeData;
return TRUE;
}
NODE* new_head;
int result = nodeData == NULL ? create_node_empty(&new_head) : create_node(nodeData,&new_head);
/// Something bad :(
if(result > 1)
return result;
new_head->nextNode = *head;
*head = new_head;
return TRUE;
}
/// Removes the first item in the list, update the head node
int pop(NODE** head)
{
NODE* current = *head;
NODE* new_head = current->nextNode;
(*head) = new_head;
delete_node(current);
return TRUE;
}