Created
July 29, 2022 20:01
-
-
Save yoloroy/b31b265720696dbf2ef06da2128348fb to your computer and use it in GitHub Desktop.
OopInCu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "ArrayList.h" | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #define INITIAL_SIZE 10 | |
| struct ArrayListImpl { | |
| struct List list; | |
| void **values; | |
| unsigned long size; | |
| unsigned long length; | |
| }; | |
| void *arrayListGetByIndex(struct ArrayListImpl *self, unsigned long index); | |
| void arrayListAppend(struct ArrayListImpl *self, void *value); | |
| void arrayListFree(struct ArrayListImpl *self); | |
| struct Node* createNode(void *value); | |
| struct ArrayList* createArrayList() { | |
| struct ArrayListImpl* list = (struct ArrayListImpl*) malloc(sizeof(struct ArrayListImpl)); | |
| list->list.getByIndex = (void *(*)(struct List*, unsigned long)) arrayListGetByIndex; | |
| list->list.append = (void (*)(struct List*, void *)) arrayListAppend; | |
| list->list.free = (void (*)(struct List *)) arrayListFree; | |
| list->values = NULL; | |
| list->size = 0; | |
| list->length = 0; | |
| return (struct ArrayList *) list; | |
| } | |
| void* arrayListGetByIndex(struct ArrayListImpl *self, unsigned long index) { | |
| if (index > self->length) return NULL; | |
| return self->values[index]; | |
| } | |
| void arrayListAppend(struct ArrayListImpl *self, void *value) { | |
| if (self->length == 0 && self->size == 0 && self->values == NULL) { // initial | |
| self->values = calloc(INITIAL_SIZE, sizeof(void *)); | |
| self->size = INITIAL_SIZE; | |
| } else if (self->length + 1 > self->size) { | |
| void **tempValues = self->values; | |
| self->values = calloc(self->size * 2 + 1, sizeof(void *)); | |
| memcpy(self->values, tempValues, self->size); | |
| self->size = self->size * 2 + 1; | |
| } | |
| self->values[self->length++] = value; | |
| } | |
| void arrayListFree(struct ArrayListImpl *self) { | |
| free(self->values); | |
| free(self); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef POLYLIST_ARRAY_LIST_H | |
| #define POLYLIST_ARRAY_LIST_H | |
| #include "List.h" | |
| struct ArrayList { | |
| struct List list; | |
| }; | |
| struct ArrayList* createArrayList(); | |
| #endif //POLYLIST_ARRAY_LIST_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "LinkedList.h" | |
| #include <stdlib.h> | |
| struct LinkedListImpl { | |
| struct List list; | |
| struct Node *head; | |
| }; | |
| struct Node { | |
| struct Node *next; | |
| void *value; | |
| }; | |
| void *linkedListGetByIndex(struct LinkedListImpl *self, unsigned long index); | |
| void linkedListAppend(struct LinkedListImpl *self, void *value); | |
| void linkedListFree(struct LinkedListImpl *self); | |
| struct Node* createNode(void *value); | |
| void freeNode(struct Node *node); | |
| struct LinkedList* createLinkedList() { | |
| struct LinkedListImpl* list = (struct LinkedListImpl*) malloc(sizeof(struct LinkedListImpl)); | |
| list->list.getByIndex = (void *(*)(struct List*, unsigned long)) linkedListGetByIndex; | |
| list->list.append = (void (*)(struct List*, void *)) linkedListAppend; | |
| list->list.free = (void (*)(struct List *)) linkedListFree; | |
| list->head = NULL; | |
| return (struct LinkedList *) list; | |
| } | |
| void* linkedListGetByIndex(struct LinkedListImpl *self, unsigned long index) { | |
| struct Node *currentNode = self->head; | |
| for (unsigned long i = 0; i < index; i++) { | |
| currentNode = currentNode->next; | |
| } | |
| return currentNode->value; | |
| } | |
| void linkedListAppend(struct LinkedListImpl *self, void *value) { | |
| if (self->head == NULL) { | |
| self->head = createNode(value); | |
| return; | |
| } | |
| struct Node *currentNode = self->head; | |
| while (currentNode->next != NULL) { | |
| currentNode = currentNode->next; | |
| } | |
| currentNode->next = createNode(value); | |
| } | |
| void linkedListFree(struct LinkedListImpl *self) { | |
| freeNode(self->head); | |
| free(self); | |
| } | |
| struct Node* createNode(void *value) { | |
| struct Node* node = malloc(sizeof(struct Node)); | |
| node->next = NULL; | |
| node->value = value; | |
| return node; | |
| } | |
| void freeNode(struct Node *node) { | |
| if (node->next != NULL) { | |
| freeNode(node->next); | |
| } | |
| free(node); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef POLYLIST_LINKEDLIST_H | |
| #define POLYLIST_LINKEDLIST_H | |
| #include "List.h" | |
| struct LinkedList { | |
| struct List list; | |
| }; | |
| struct LinkedList* createLinkedList(); | |
| #endif //POLYLIST_LINKEDLIST_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef POLYLIST_LIST_H | |
| #define POLYLIST_LIST_H | |
| struct List { | |
| void *(* getByIndex)(struct List *self, unsigned long index); | |
| void (* append)(struct List *self, void *); | |
| void (* free)(struct List *self); | |
| }; | |
| #endif //POLYLIST_LIST_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include "List.h" | |
| #include "ArrayList.h" | |
| #include "inkedList.h" | |
| #define SUCCESS_RESULT 1 | |
| #define ERROR_RESULT 0 | |
| void testArrayList(); | |
| void testLinkedList(); | |
| int main() { | |
| testArrayList(); | |
| testLinkedList(); | |
| return 0; | |
| } | |
| int testList(struct List *list) { | |
| int value1 = 1; | |
| int value2 = 2; | |
| list->append(list, &value1); | |
| list->append(list, &value2); | |
| if (*((int *) list->getByIndex(list, 0)) != value1) { | |
| return ERROR_RESULT; | |
| } | |
| if (*((int *) list->getByIndex(list, 1)) != value2) { | |
| return ERROR_RESULT; | |
| } | |
| return SUCCESS_RESULT; | |
| } | |
| void testArrayList() { | |
| struct List* list = (struct List *) createArrayList(); | |
| if (testList((struct List *) list) == SUCCESS_RESULT) { | |
| printf("ArrayList is good\n"); | |
| } else { | |
| printf("ArrayList is bad\n"); | |
| } | |
| list->free(list); | |
| } | |
| void testLinkedList() { | |
| struct List* list = (struct List *) createLinkedList(); | |
| if (testList((struct List *) list) == SUCCESS_RESULT) { | |
| printf("LinkedList is good\n"); | |
| } else { | |
| printf("LinkedList is bad\n"); | |
| } | |
| list->free(list); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment