-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistnode.c
More file actions
264 lines (238 loc) · 6.37 KB
/
listnode.c
File metadata and controls
264 lines (238 loc) · 6.37 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "listnode.h"
#include <stdio.h> // used for prinf
#include <stdlib.h> // used for free, malloc,
#include <string.h> // used for strcpy, memcpy
#define ERROR 0x1
#define SUCCESS 0x0
// sentinel-style linked list
struct LN{
char* text;
struct LN* next;
struct LN* head;
struct LN* tail;
} typedef ListNode;
// initlaizes a new child node
ListNode* _create_node(ListNode* parent, const char* text);
// frees an individual node
void _destroy_node(ListNode* node);
// automatically free and allocate for char* in node
int _change_node_text(ListNode* head, const char* text);
ListNode* _create_node(ListNode* parent, const char* text){
ListNode* child = (ListNode*)malloc(sizeof(ListNode));
child->head = parent->head;
child->tail = parent->tail;
child->next = parent->tail;
child->text = NULL;
_change_node_text(child, text);
return child;
}
void _destroy_node(ListNode* node){
if (node != node->head && node != node->tail){
free(node->text);
}
free(node);
}
int _change_node_text(ListNode* node, const char* text){
if (node->text){
free(node->text);
node->text = NULL;
}
node->text = (char*)malloc(strlen(text) + 1);
strcpy(node->text, text);
return SUCCESS;
}
ListNode* new_list(void){
ListNode* tail = (ListNode*) malloc(sizeof(ListNode));
tail->tail = tail;
tail->next = NULL;
tail->text = "";
ListNode* head = (ListNode*) malloc(sizeof(ListNode));
head->head = head;
head->tail = tail;
head->next = tail;
head->text = "";
tail->head = head;
return head;
}
void destroy_list(ListNode* head){
if (head == NULL){
return;
}
destroy_list(head->next);
_destroy_node(head);
}
int push(ListNode* head, const char* text){
if (head == NULL ||
head != head->head){ // enforce correct use
return ERROR;
}
while (head->next != head->tail){
head = head->next;
}
ListNode* new_node = _create_node(head, text);
head->next = new_node;
return SUCCESS;
}
int pop(ListNode* head){
if (head == NULL ||
head != head->head){ // enforce correct use
return ERROR;
}
if (head->next == head->tail){
return SUCCESS;
}
while (head->next->next != head->tail){
head = head->next;
}
_destroy_node(head->next);
head->next = head->tail;
if (head->next == head->tail){
return SUCCESS;
}
}
int add(ListNode* head, int line_index, char* text){
if (head == NULL ||
head != head->head){
return ERROR;
}
int num_lines = length(head);
if (line_index > num_lines || line_index < 0){ // past back of last or in front of first
return ERROR;
}
// move head to target node
for (int index = 0; index < line_index; index++){
head = head->next;
}
// create new node to insert
ListNode* new_node = _create_node(head, text);
ListNode* following_node = head->next;
// update pointers
head->next = new_node;
new_node->next = following_node;
return SUCCESS;
}
int drop(ListNode* head, int start, int stop){
if (head == NULL ||
head != head->head){ // enforce correct use
return ERROR;
}
int num_lines = length(head);
if (start < 0 || stop > num_lines || stop <= start){ // bounds check
return ERROR;
}
// traverse to node before starting node
for (int index = 0; index < start; index++){
head = head->next;
}
ListNode* current_node = head->next;
ListNode* temp;
// delete nodes up to stop node
for (int index = start; index < stop; index++){
temp = current_node->next;
_destroy_node(current_node);
current_node = temp;
}
head->next = current_node;
return SUCCESS;
}
int edit(ListNode* head, int line_index, char* text){
if (head == NULL ||
head != head->head){
return ERROR;
}
int num_lines = length(head);
if (line_index > num_lines || line_index < 0){ // out of bounds check
return ERROR;
}
head = head->next;
while (line_index != 0){
head = head->next;
line_index--;
}
_change_node_text(head, text);
return SUCCESS;
}
char* get_line(ListNode* head, int line_index){
if (head == NULL ||
head != head->head){
return NULL;
}
int num_lines = length(head);
if (line_index > num_lines || line_index < 0){ // out of bounds check
return NULL;
}
head = head->next;
while (line_index != 0){
head = head->next;
line_index--;
}
return head->text;
}
char* get_range(ListNode* head, int start, int stop){
int bytes = 0;
char* string;
char* cursor;
char* current_line;
int line_len;
for (int index = start; index < stop; index++){
bytes += strlen(get_line(head, index));
}
string = (char*)malloc(bytes + 1);
cursor = string;
for (int index = start; index < stop; index++){
current_line = get_line(head, index);
line_len = strlen(current_line);
memcpy(cursor, current_line, line_len);
cursor += line_len;
}
string[bytes] = '\0';
return string;
}
int length(ListNode* head){
int size = 0;
while (head->next != head->tail){
size++;
head = head->next;
}
return size;
}
int print(ListNode* head){
if (head == NULL ||
head != head->head){
return ERROR;
}
int line_number = 0;
while (head->next != head->tail){
head = head->next;
if (head->text[strlen(head->text) - 1] != '\n'){
printf("#%d: %s\n", line_number, head->text);
}
else{
printf("#%d: %s", line_number, head->text);
}
line_number++;
}
return SUCCESS;
}
int print_range(ListNode* head, int start, int stop){
if (head == NULL ||
head != head->head){ // enforce correct use
return ERROR;
}
int num_lines = length(head);
if (start < 0 || stop > num_lines || stop < start){ // bound check
return ERROR;
}
int line_number = 0;
head = head->next;
for(int index = 0; index < start; index++){
head = head->next;
line_number++;
};
for (int index = start; index < stop; index++){
printf("#%d: %s", line_number, head->text);
line_number++;
head = head->next;
}
return SUCCESS;
}