-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArrayList.c
170 lines (153 loc) · 3.93 KB
/
ArrayList.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
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
/*
* @Name: ArrayList C
* @Author: Max Base
* @Date: 2022/12/02
* @Repository: https://github.com/basemax/ArrayListC
*/
#include "ArrayList.h"
/**
* @brief Create an array list with maximum `n` value capacity.
* @param n Maximum capacity of the array list.
* @return Pointer to the array list.
*/
ArrayList* ArrayCreate(int n)
{
ArrayList* list = malloc(sizeof(int) * n);
list->size = n;
list->count = 0;
return list;
}
/**
* @brief Append a value to the first of the array list.
* @param list Pointer to the array list.
* @param value Value to append.
*/
void ArrayAppendFirst(ArrayList* list, int value)
{
if(list->count == list->size) {
printf("Error: Array is full!\n");
return;
}
for (int i = list->count; i > 0; i--) {
list->data[i] = list->data[i - 1];
}
list->data[0] = value;
list->count++;
}
/**
* @brief Append a value to the last of the array list.
* @param list Pointer to the array list.
* @param value Value to append.
*/
void ArrayAppendLast(ArrayList* list, int value)
{
if (list->count == list->size) {
printf("Error: Array is full!\n");
return;
}
list->data[list->count] = value;
list->count++;
}
/**
* @brief Append a value to the array list at the specified index.
* @param list Pointer to the array list.
* @param value Value to append.
* @param index Index to append the value.
*/
void ArrayAppend(ArrayList* list, int value, int index)
{
if (list->count == list->size) {
printf("Error: Array is full!\n");
return;
}
if (index < 0 || index > list->count) {
printf("Error: Index out of range!\n");
return;
}
for (int i = list->count; i > index; i--) {
list->data[i] = list->data[i - 1];
}
list->data[index] = value;
list->count++;
}
/**
* @brief Delete a value from the array list at the specified index.
* @param list Pointer to the array list.
* @param index Index to delete the value.
*/
void ArrayDelete(ArrayList* list, int index)
{
if (list->count <= 0) {
printf("Error: Array is empty!\n");
return;
}
else if (index >= list->count || index < 0) {
printf("Error: Index out of range!\n");
return;
}
for (int i = index; i < list->count; i++) {
list->data[i] = list->data[i + 1];
}
list->count--;
}
/**
* @brief Delete a value from the first of the array list.
* @param list Pointer to the array list.
*/
void ArrayDeleteFirst(ArrayList* list)
{
ArrayDelete(list, 0);
}
/**
* @brief Delete a value from the last of the array list.
* @param list Pointer to the array list.
*/
void ArrayDeleteLast(ArrayList* list)
{
ArrayDelete(list, list->count - 1);
}
/**
* @brief Print the array list.
* @param list Pointer to the array list.
*/
void ArrayPrint(ArrayList* list)
{
if (list->count <= 0) {
printf("Error: Array is empty!\n");
return;
}
for (int i = 0; i < list->count; i++) {
printf("%d ", list->data[i]);
}
printf("\n");
}
/**
* @brief Free the array list.
* @param list Pointer to the array list.
*/
void ArrayFree(ArrayList* list)
{
free(list);
}
/**
* @brief Convert the array list to a string.
* @param list Pointer to the array list.
* @return String of the array list.
*/
char* ArrayToString(ArrayList* list)
{
if (list->count <= 0) {
printf("Error: Array is empty!\n");
return NULL;
}
// Consider the size of the string for each integer is maximum 10 characters.
char* str = malloc(sizeof(char) * list->count * 10);
char* temp = malloc(sizeof(char) * 10);
for (int i = 0; i < list->count; i++) {
sprintf(temp, "%d", list->data[i]);
strcat(str, temp);
strcat(str, " ");
}
free(temp);
return str;
}