From 2377658d7bea2887d7da9b6f47ab2b8230226347 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Sat, 10 Dec 2016 05:04:13 +0100 Subject: [PATCH] Deleted files + fixed makefile --- .gitignore | 1 + LibList/Makefile | 1 + LibList/list.c | 217 +++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 6 +- 4 files changed, 222 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 LibList/list.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..586299e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Libs/ diff --git a/LibList/Makefile b/LibList/Makefile index 5f92df2..ac206d5 100644 --- a/LibList/Makefile +++ b/LibList/Makefile @@ -14,6 +14,7 @@ LIBSOURCEOFILE=$(LIBSOURCE:=.o) #Generating the library binary $(LIBTARGET): $(LIBSOURCEOFILE) @echo "\n Generating the library binary" + mkdir -p ../Libs $(CC) $(CFLAGS) -shared $(LIBSOURCEOFILE) -o ../Libs/$(LIBTARGET) #Generating object files diff --git a/LibList/list.c b/LibList/list.c new file mode 100644 index 0000000..f422a88 --- /dev/null +++ b/LibList/list.c @@ -0,0 +1,217 @@ +/********************************************************************************* +* File Name : list.c +* Created By : Bartuccio Antoine +* Creation Date : [2016-10-18 13:53] +* Last Modified : [2016-11-08 15:00] +* Description : +**********************************************************************************/ + +#include +#include +#include +#include +#include + +#define SUCCESS 0 +#define FAILURE 1 + +List * CreateList() { + List *list = malloc(sizeof(*list)); + + if(list != NULL){ + list->head = NULL; + list->tail = NULL; + list->size = 0; + } + + return list; +} + +int unshift(List *list, cellElement *data){ + int ok = SUCCESS; + + cellElement* newData = (cellElement*) malloc(sizeof(cellElement)); + + /* Create a new element */ + + ListElement *newElement = malloc(sizeof(*newElement)); + if (list != NULL && newElement != NULL && newData != NULL){ + memcpy(newData, data, sizeof(cellElement)); + newElement->data = newData; + + /* Insert the element at the begining of the list */ + + newElement->previous = NULL; + + if (list->head != NULL){ + list->head->previous = newElement; + } else { + list->tail = newElement; + } + newElement->next = list->head; + + list->head = newElement; + list->size = list->size + 1; + } else { + if (newElement != NULL){ + free(newElement); + } + if (newData != NULL){ + free(newData); + } + ok = FAILURE; + } + + return ok; +} + +int push(List *list, cellElement *data){ + int ok = SUCCESS; + cellElement *newData = (cellElement*) malloc(sizeof(cellElement)); + + ListElement *newElement = malloc(sizeof(*newElement)); + if(list != NULL && newElement != NULL && newData != NULL){ + memcpy(newData, data, sizeof(cellElement)); + newElement->data = newData; + newElement->next = NULL; + if (list->tail == NULL){ + list->tail = newElement; + list->head = newElement; + newElement->previous = NULL; + } else { + newElement->previous = list->tail; + list->tail->next = newElement; + list->tail = newElement; + } + list->size = list->size + 1; + } else { + if (newElement != NULL){ + free(newElement); + } + if (newData != NULL){ + free(newData); + } + ok = FAILURE; + } + return ok; +} + +ListElement * GetElement(List *list, int nb){ + ListElement *current = NULL; + int i; + + if (list != NULL && (nb < list->size || -nb < list->size)){ + if (nb == list->size -1 || nb == -1){ + current = list->tail; + } else if (nb == 0){ + current = list->head; + } else if (nb <= (list->size - 1)/2 && nb > 0){ + i = 0; + current = list->head; + while(inext; + i++; + } + } else { + i = list->size - 1; + if (nb < 0){ + nb = list->size + nb -1; + } + while(i>nb){ + current = current->previous; + i = i - 1; + } + } + } + return current; +} + +int PopPtnList(List *list, ListElement *element){ + int ok = SUCCESS; + + if (list != NULL && element != NULL){ + if (list->head == element && list->tail == element){ + list->head = NULL; + list->tail = NULL; + } else if (list->head == element){ + list->head = element->next; + element->previous = NULL; + } else if (list->tail == element){ + list->tail = element->previous; + element->previous->next = NULL; + } else { + element->next->previous = element->previous; + element->previous->next = element->next; + } + + if (element->data != NULL){ + FreeCellElement(element->data); + } + free(element); + list->size = list->size - 1; + } else { + ok = FAILURE; + } + return ok; +} + +int RemoveElement(List *list, int nb){ + int ok = SUCCESS; + ListElement *toDelete = GetElement(list, nb); + + if (toDelete != NULL){ + ok = PopPtnList(list, toDelete); + } else { + ok = FAILURE; + } + return ok; +} + +int shift(List *list){ + return RemoveElement(list, 0); +} + +int pop(List *list){ + return RemoveElement(list, -1); +} + +int DeleteListContent(List *list){ + int ok = SUCCESS; + ListElement * current = NULL; + ListElement * toDelete = NULL; + + if (list != NULL){ + current = list->head; + while (current != NULL){ + toDelete = current; + current = current->next; + + if (toDelete->data != NULL){ + FreeCellElement(toDelete->data); + } + + free(toDelete); + } + list->head = NULL; + list->tail = NULL; + list->size = 0; + } else { + ok = FAILURE; + } + + return ok; +} + +int FreeList(List *list){ + int ok = SUCCESS; + + if (list != NULL){ + ok = DeleteListContent(list); + if (ok == SUCCESS){ + free(list); + } + } else { + ok = FAILURE; + } + return ok; +} diff --git a/Makefile b/Makefile index d7e22c6..77e5206 100644 --- a/Makefile +++ b/Makefile @@ -17,8 +17,8 @@ SOURCECFILE=$(SOURCE:=.c) SOURCEOFILE=$(SOURCE:=.o) #Library variables -DEPENDENCE=libCellElement.so -DEPENDENCENAME=CellElement +# DEPENDENCE=libCellElement.so +# DEPENDENCENAME=CellElement DEPENDENCELIST=libList.so DEPENDENCENAMELIST=List @@ -35,7 +35,7 @@ $(TARGET): $(SOURCEOFILE) $(DEPENDENCELIST) lib lib: @echo "\n Generating the automaton library binary" mkdir -p Libs - $(MAKE) -C LibCell + # $(MAKE) -C LibCell $(DEPENDENCELIST): @echo "\n Generating the list library binary"