-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_list.c
More file actions
157 lines (122 loc) · 4.5 KB
/
array_list.c
File metadata and controls
157 lines (122 loc) · 4.5 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
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
typedef struct {
size_t len;
size_t capacity;
int *elems;
} ArrayList;
ArrayList* arraylist_init(size_t initial_capacity) {
ArrayList *arr = malloc(sizeof(ArrayList));
if (!arr) {
fprintf(stderr, "error: memory allocation failed!\n");
exit(-1);
}
arr->elems = (int*)malloc(initial_capacity * sizeof(int));
if (!(arr->elems)) {
fprintf(stderr, "error: memory allocation failed!\n");
free(arr);
exit(-1);
}
arr->capacity = initial_capacity;
arr->len = 0;
return arr;
}
void arraylist_deinit(ArrayList **self) {
free((*self)->elems); // Deallocate array elements
free(*self); // Deallocate ArrayList struct
*self = NULL; // Remove the pointer reference to the array list
}
int arraylist_resize(ArrayList *self) {
self->capacity *= 2;
self->elems = (int*)realloc(self->elems, sizeof(int) * self->capacity);
if (!(self->elems)) {
fprintf(stderr, "error: memory allocation failed!\n");
return 0;
}
return 1;
}
void arraylist_append(ArrayList *self, int val) {
if (self->len == self->capacity) {
// don't add the new element if resize fails
if (!arraylist_resize(self)) return;
}
self->elems[self->len++] = val;
}
int arraylist_pop(ArrayList *self) {
if (self->len <= 0) {
fprintf(stderr, "error: fail to remove, list is empty!\n");
return -1;
}
return self->elems[--self->len];
}
int arraylist_get(ArrayList *self, size_t pos) {
if (pos >= self->len) {
fprintf(stderr, "error: index out of bounds!\n");
return -1;
}
return self->elems[pos];
}
void arraylist_insert(ArrayList *self, int val, size_t pos) {
if (pos > self->len) {
fprintf(stderr, "error: index out of bounds!\n");
return;
} else if (self->len == self->capacity) {
// don't add the new element if resize fails
if (!arraylist_resize(self)) return;
}
// shift elems
for (size_t i = self->len; i > pos; i--) {
self->elems[i] = self->elems[i - 1];
}
self->elems[pos] = val;
self->len++;
}
int main() {
ArrayList *arrlist = arraylist_init(1);
{
int val_to_insert = 20;
size_t pos_to_insert = 0;
printf("Inserting value %d at position %zu\n", val_to_insert, pos_to_insert);
arraylist_insert(arrlist, val_to_insert, pos_to_insert);
printf("Elements in the array list after insertion: ");
for (int i = 0; i < arrlist->len; i++)
printf("%d ", arraylist_get(arrlist, i));
printf("\n");
}
arraylist_append(arrlist, 1);
arraylist_append(arrlist, 2);
arraylist_append(arrlist, 3);
printf("Elements in the array list: ");
for (int i = 0; i < arrlist->len; i++)
printf("%d ", arraylist_get(arrlist, i));
printf("\n");
{
// Test arraylist_insert
int val_to_insert = 100;
size_t pos_to_insert = 1;
printf("Inserting value %d at position %zu\n", val_to_insert, pos_to_insert);
arraylist_insert(arrlist, val_to_insert, pos_to_insert);
printf("Elements in the array list after insertion: ");
for (int i = 0; i < arrlist->len; i++)
printf("%d ", arraylist_get(arrlist, i));
printf("\n");
}
// Test arraylist_pop
int removedElement = arraylist_pop(arrlist);
printf("Removed element: %d\n", removedElement);
printf("Elements in the array list after removal: ");
for (int i = 0; i < arrlist->len; i++)
printf("%d ", arraylist_get(arrlist, i));
printf("\n");
// Test resizing
arraylist_append(arrlist, 4);
arraylist_append(arrlist, 5);
arraylist_append(arrlist, 6);
printf("Elements in the array list after resizing: ");
for (int i = 0; i < arrlist->len; i++)
printf("%d ", arraylist_get(arrlist, i));
printf("\n");
arraylist_deinit(&arrlist);
return 0;
}