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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
| #include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "singly.h"
struct singly_node {
struct singly_node *next;
double data;
};
void singly_print(const struct singly_list *list) {
struct singly_node *it = NULL;
int width = sizeof(it) + 1;
assert(list);
printf("%*s | %*s | %s\n", width, "Node", width, "Next", "Value");
it = list->head;
while (it != NULL) {
fprintf(stdout, "%*p | %*p | %lf\n", width, it, width, it->next, it->data);
it = it->next;
}
fprintf(stdout, "\n");
}
int singly_empty(const struct singly_list *list) {
assert(list);
return list->head == NULL;
}
size_t singly_size(const struct singly_list *list) {
struct singly_node *it;
size_t size;
assert(list);
it = list->head;
while (it != NULL) {
it = it->next;
++size;
}
return size;
}
void singly_clear(struct singly_list *list) {
assert(list);
while (!singly_empty(list))
singly_pop_front(list);
}
int singly_push_front(struct singly_list *list, double value) {
struct singly_node *inserted = NULL;
assert(list);
if ((inserted = (struct singly_node *)malloc(sizeof(*inserted))) == NULL) {
fprintf(stderr, "ERROR: Failure to memory allocate\n");
return EXIT_FAILURE;
}
inserted->next = singly_empty(list) ? NULL : list->head;
inserted->data = value;
list->head = inserted;
return EXIT_SUCCESS;
}
int singly_push_back(struct singly_list *list, double value) {
struct singly_node *inserted = NULL;
struct singly_node *tail = NULL;
assert(list);
if ((inserted = (struct singly_node *)malloc(sizeof(*inserted))) == NULL) {
fprintf(stderr, "ERROR: Failure to memory allocate\n");
return EXIT_FAILURE;
}
inserted->next = NULL;
inserted->data = value;
if (singly_empty(list)) {
list->head = inserted;
}
else {
tail = list->head;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = inserted;
}
return EXIT_SUCCESS;
}
int singly_pop_front(struct singly_list *list) {
struct singly_node *removed = NULL;
assert(list);
if (singly_empty(list)) {
fprintf(stderr, "WARNING: List is already empty\n");
return EXIT_FAILURE;
}
removed = list->head;
list->head = list->head->next;
free(removed);
return EXIT_SUCCESS;
}
int singly_pop_back(struct singly_list *list) {
struct singly_node *removed = NULL;
struct singly_node *prev = NULL;
assert(list);
if (singly_empty(list)) {
fprintf(stderr, "WARNING: List is already empty\n");
return EXIT_FAILURE;
}
removed = list->head;
while (removed->next != NULL) {
prev = removed;
removed = removed->next;
}
if (prev != NULL)
prev->next = NULL;
else
list->head = NULL;
free(removed);
return EXIT_SUCCESS;
}
int singly_insert(struct singly_list *list, size_t position, double value) {
struct singly_node *inserted = NULL;
struct singly_node *prev = NULL;
assert(list);
if ((inserted = (struct singly_node *)malloc(sizeof(*inserted))) == NULL) {
fprintf(stderr, "ERROR: Failure to memory allocate\n");
return EXIT_FAILURE;
}
inserted->data = value;
inserted->next = NULL;
if (singly_empty(list)) {
list->head = inserted;
}
else {
prev = list->head;
while (prev->next != NULL && position--) {
prev = prev->next;
}
inserted->next = prev->next;
prev->next = inserted;
}
return EXIT_SUCCESS;
}
int singly_erase(struct singly_list *list, size_t position) {
struct singly_node *removed = NULL;
struct singly_node *prev = NULL;
assert(list);
if (singly_empty(list)) {
fprintf(stderr, "List is already empty\n");
return EXIT_FAILURE;
}
removed = list->head;
while (removed->next != NULL && position--) {
prev = removed;
removed = removed->next;
}
if (prev != NULL && prev->next != NULL)
prev->next = removed->next;
else
list->head = list->head->next;
free(removed);
return EXIT_SUCCESS;
} |