diff --git a/LibAutomaton/list.c b/LibAutomaton/list.c index 02416f3..8b8a7e1 100644 --- a/LibAutomaton/list.c +++ b/LibAutomaton/list.c @@ -35,6 +35,7 @@ int unshift(List *list, cellElement *data){ ListElement *newElement = malloc(sizeof(*newElement)); if (list != NULL && newElement != NULL){ newElement->data = data; + newElement->pos = -1; /* Insert the element at the begining of the list */ @@ -65,6 +66,7 @@ int push(List *list, cellElement *data){ ListElement *newElement = malloc(sizeof(*newElement)); if(list != NULL && newElement != NULL){ newElement->data = data; + newElement->pos = -1; newElement->next = NULL; if (list->tail == NULL){ list->tail = newElement; @@ -204,3 +206,141 @@ int FreeList(List *list){ } return ok; } + +ListElement * GetElementPos(List *list, int pos){ + ListElement * el = list->head; + while (el != NULL && el->pos != pos){ + el = el->next; + } + return el; +} + +int RemoveElementPos(List *list, int pos){ + int ok = SUCCESS; + ListElement *toDelete = GetElementPos(list, pos); + + if (toDelete != NULL){ + ok = PopPtnList(list, toDelete); + } else { + ok = FAILURE; + } + return ok; +} + +int InsertBeforeElement(List *list, ListElement *eli, ListElement *elp){ + int ok = SUCCESS; + + if (list != NULL){ + if (elp->previous == NULL){ + eli->next = elp; + eli->previous = NULL; + elp->previous = eli; + } else { + eli->next = elp; + eli->previous = elp->previous; + elp->previous->next = eli; + elp->previous = eli; + } + list->size = list->size + 1; + } else { + ok = FAILURE; + } + return ok; +} + +int InsertAfterElement(List *list, ListElement *eli, ListElement *elb){ + int ok = SUCCESS; + + if (list != NULL){ + if (elb->next == NULL){ + eli->next = NULL; + eli->previous = elb; + elb->next = eli; + } else { + eli->previous = elb; + eli->next=elb->next; + elb->next->previous = eli; + elb->next = eli; + } + list->size = list->size + 1; + } else { + ok = FAILURE; + } + return ok; +} + +int InsertBefore(List *list, cellElement *data, int nb){ + int ok = SUCCESS; + ListElement *newElement = NULL; + ListElement *eli = GetElement(list, nb); + if (eli != NULL){ + newElement = malloc(sizeof(*newElement)); + if (newElement != NULL){ + newElement->pos = -1; + newElement->data = data; + ok = InsertBeforeElement(list, newElement, eli); + } else { + ok = FAILURE; + } + } else { + ok = FAILURE; + } + return ok; +} + +int InsertAfter(List *list, cellElement *data, int nb){ + int ok = SUCCESS; + ListElement *newElement = NULL; + ListElement *elb = GetElement(list, nb); + if (elb != NULL){ + newElement = malloc(sizeof(*newElement)); + if (newElement != NULL){ + newElement->pos = -1; + newElement->data = data; + ok = InsertAfterElement(list, newElement, elb); + } else { + ok = FAILURE; + } + } else { + ok = FAILURE; + } + return ok; +} + +int InsertBeforePos(List *list, cellElement *data, int pos){ + int ok = SUCCESS; + ListElement *newElement = NULL; + ListElement *eli = GetElementPos(list, pos); + if (eli != NULL){ + newElement = malloc(sizeof(*newElement)); + if (newElement != NULL){ + newElement->pos = -1; + newElement->data = data; + ok = InsertBeforeElement(list, newElement, eli); + } else { + ok = FAILURE; + } + } else { + ok = FAILURE; + } + return ok; +} + +int InsertAfterPos(List *list, cellElement *data, int pos){ + int ok = SUCCESS; + ListElement *newElement = NULL; + ListElement *elb = GetElementPos(list, pos); + if (elb != NULL){ + newElement = malloc(sizeof(*newElement)); + if (newElement != NULL){ + newElement->pos = -1; + newElement->data = data; + ok = InsertAfterElement(list, newElement, elb); + } else { + ok = FAILURE; + } + } else { + ok = FAILURE; + } + return ok; +} diff --git a/LibAutomaton/list.h b/LibAutomaton/list.h index b1d61ee..b0f3baf 100644 --- a/LibAutomaton/list.h +++ b/LibAutomaton/list.h @@ -11,6 +11,7 @@ struct ListElement { cellElement *data; ListElement *next; ListElement *previous; + int pos; }; typedef struct List { @@ -19,13 +20,13 @@ typedef struct List { int size; } List; -/* +/** * Create a new list * @return a pointer of list */ List * CreateList(); -/* +/** * Insert an element at the begining of a list * @param list pointer of a list * @param data any type of data @@ -34,7 +35,7 @@ List * CreateList(); */ int unshift(List* list, cellElement* data); -/* +/** * Insert an element at the end of a list * @param list pointer of a list * @param data any type of data @@ -43,7 +44,7 @@ int unshift(List* list, cellElement* data); */ int push(List* list, cellElement* data); -/* +/** * Get an element in a given list * @param list as a pointer * @param nb the number of the element (can be negative) @@ -51,7 +52,7 @@ int push(List* list, cellElement* data); */ ListElement * GetElement(List *list, int nb); -/* +/** * Delete an element with a pointer of element in the list * @param list as a pointer * @param element of the list as a pointer @@ -59,7 +60,7 @@ ListElement * GetElement(List *list, int nb); */ int PopPtnList(List *list, ListElement *element); -/* +/** * Delete an element with a position in the list * @param list as a pointer * @param position of the element @@ -67,33 +68,102 @@ int PopPtnList(List *list, ListElement *element); */ int RemoveElement(List *list, int nb); -/* +/** * Delete the first element of the list * @param list as a pointer * @return status of the operation */ int shift(List *list); -/* +/** * Delete the last element of the list * @param list as a pointer * @return status of the operation */ int pop(List *list); -/* +/** * Delete every elements in a list * @param list as a pointer * @return status of the operation */ int DeleteListContent(List *list); - -/* +/** * Free a list * @param list as a pointer * @return status of the operation */ int FreeList(List *list); +/** + * Find the first element with the given pos value + * @param list as a pointer + * @param pos the pos value to find + * @return ListElement the found element can return NULL + */ +ListElement * GetElementPos(List *List, int pos); + +/** + * Delete the first element of a list with the given pos + * @param list as a pointer + * @param pos pos value of the element + * @return status of the operation + */ +int RemoveElementPos(List *list, int pos); + +/** + * Insert an element in a list before the given element + * @param list as a pointer + * @param eli an element to insert + * @param elp the previous element in the list + * @return status of the operation + */ +int InsertBeforeElement(List *list, ListElement *eli, ListElement *elp); + +/** + * Insert an element in a list after the given element + * @param list as a pointer + * @param eli an element to insert + * @param elb the before element in the list + * @return status of the operation + */ +int InsertAfterElement(List *list, ListElement *eli, ListElement *elb); + +/** + * Insert an element in a list before the given position + * @param list as a pointer + * @param data cellElement to insert + * @param nb the position in list to find + * @return status of the operation + */ +int InsertBefore(List *list, cellElement *data, int nb); + +/** + * Insert an element in a list after the given position + * @param list as a pointer + * @param data cellElement to insert + * @param nb the position in list to find + * @return status of the operation + */ +int InsertAfter(List *list, cellElement *data, int nb); + +/** + * Insert an element in a list before the first element with the given pos + * @param list as a pointer + * @param data cellElement to insert + * @param pos the first pos in list to find + * @return status of the operation + */ +int InsertBeforePos(List *list, cellElement *data, int pos); + +/** + * Insert an element in a list after the first element with the given pos + * @param list as a pointer + * @param data cellElement to insert + * @param pos the first pos in list to find + * @return status of the operation + */ +int InsertAfterPos(List *list, cellElement *data, int pos); + #endif /* LIST_H */