Changed the signatures of some of the List functions to have the 'const' contract. I feel this makes more sense to have since these functions are to not modify all or some of their parameters. Plus it just feels right to have them defined this way.
This commit is contained in:
+2
-2
@@ -14,8 +14,8 @@ typedef struct {
|
|||||||
} List;
|
} List;
|
||||||
|
|
||||||
List* CreateList(void);
|
List* CreateList(void);
|
||||||
int AddListItem(char *, List *);
|
int AddListItem(const char *, List *);
|
||||||
void PrintList(List*);
|
void PrintList(const List*);
|
||||||
void DestroyList(List*);
|
void DestroyList(List*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+10
-4
@@ -9,11 +9,17 @@ List* CreateList() {
|
|||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
int AddListItem(char *value, List* list) {
|
int AddListItem(const char *value, List* list) {
|
||||||
if (list == NULL) return -1;
|
if (list == NULL) return -1;
|
||||||
|
|
||||||
if (list->capacity < list->size + 1) {
|
if (list->capacity < list->size + 1) {
|
||||||
list->root = realloc(list->root, sizeof(char*) * list->capacity * 2);
|
void* ptr = realloc(list->root, sizeof(char*) * list->capacity * 2);
|
||||||
|
//Note: realloc will free list->root if it succeeds.
|
||||||
|
if (!ptr) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
list->root = ptr;
|
||||||
list->capacity = list->capacity * 2;
|
list->capacity = list->capacity * 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,10 +34,10 @@ int AddListItem(char *value, List* list) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintList(List* list) {
|
void PrintList(const List* list) {
|
||||||
if (list == NULL) return;
|
if (list == NULL) return;
|
||||||
|
|
||||||
List *current = list;
|
const List *current = list;
|
||||||
|
|
||||||
printf("Size: %d; Capacity: %d\n", list->size, list->capacity);
|
printf("Size: %d; Capacity: %d\n", list->size, list->capacity);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user