-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
117 lines (106 loc) · 2.73 KB
/
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
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
// Implements linked list containing pairs of ints
#include <stdlib.h>
#include <string.h>
typedef struct Element Element;
struct Element {
int begin, end;
Element* next;
};
typedef struct List List;
struct List {
Element* head;
Element* tail;
int length;
};
// Allocate memory and initialize empty list element
Element* list_element_construct(int begin, int end) {
Element* element = (Element*)malloc(sizeof(Element));
element->begin = begin;
element->end = end;
element->next = NULL;
return element;
}
// Allocate memory and initialize empty list
List* list_construct() {
List* list = (List*)malloc(sizeof(List));
list->length = 0;
list->head = NULL;
list->tail = NULL;
return list;
}
// Returin boolean telling if given list is empty
int list_empty(List* list) {
return ! list->length;
}
// Push given pair of ints into given list
void list_push(List* list, int begin, int end) {
Element* element = list_element_construct(begin, end);
if (! list_empty(list)) {
list->tail->next = element;
} else {
list->head = element;
}
list->tail = element;
list->length++;
}
// Generate string representation of given number
char* list_int_tostring(int number) {
int length, temp = number;
if (! number) {
return "0";
}
for (length = 0; temp; temp /= 10) {
length++;
}
char* result = (char*)malloc((length + 1) * sizeof(char));
int i;
for (i = length; number; number /= 10) {
result[--i] = (char)('0' + number % 10);
}
result[length] = 0;
return result;
}
// Generate string representation of given list element
char* list_element_tostring(Element* element) {
char* begin = list_int_tostring(element->begin);
char* end = list_int_tostring(element->end);
int length = strlen(begin) + strlen(end) + 5;
char* result = (char*)malloc(length * sizeof(char));
strcpy(result, "<");
strcat(result, begin);
strcat(result, ", ");
strcat(result, end);
strcat(result, ">");
free(begin);
free(end);
return result;
}
// Generate string representation of given list
char* list_tostring(List* list) {
int len = 7;
if (list_empty(list)) {
return "Empty list";
}
char* result = (char*)malloc(len * sizeof(char));
strcpy(result, "List: ");
Element* element;
for (element = list->head; element; element = element->next) {
char* element_str = list_element_tostring(element);
len += strlen(element_str) + 2;
result = (char*)realloc(result, len * sizeof(char));
strcat(result, element_str);
free(element_str);
if (element->next) {
strcat(result, ", ");
}
}
return result;
}
// Destroy given list
void list_destroy(List* list) {
Element* element;
for (element = list->head; element; element = element->next) {
free(element);
}
free(list);
}