From deb20c5230c724d6394f011ed83e73874a4ce6ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Cruz=20Garc=C3=ADa?= Date: Fri, 23 Oct 2020 22:05:36 +0200 Subject: [PATCH] Added List with Linked List Dummy Node implementation (TAD) --- .../LinkedList Imp/Dummy Node Imp/LLDList.c | 172 ++++++++++++++++++ .../LinkedList Imp/Dummy Node Imp/LLDList.h | 39 ++++ .../LinkedList Imp/Dummy Node Imp/main.c | 49 +++++ .../LinkedList Imp/Dummy Node Imp/makefile | 21 +++ 4 files changed, 281 insertions(+) create mode 100644 TADS/Lists/LinkedList Imp/Dummy Node Imp/LLDList.c create mode 100644 TADS/Lists/LinkedList Imp/Dummy Node Imp/LLDList.h create mode 100644 TADS/Lists/LinkedList Imp/Dummy Node Imp/main.c create mode 100644 TADS/Lists/LinkedList Imp/Dummy Node Imp/makefile diff --git a/TADS/Lists/LinkedList Imp/Dummy Node Imp/LLDList.c b/TADS/Lists/LinkedList Imp/Dummy Node Imp/LLDList.c new file mode 100644 index 0000000..8b509d1 --- /dev/null +++ b/TADS/Lists/LinkedList Imp/Dummy Node Imp/LLDList.c @@ -0,0 +1,172 @@ +#include +#include +#include + +#include "LLDList.h" + +int createEmptyList(LLDList *list){ + if (list == NULL) return -1; + + //Create a dummy node + if (NULL == (list->first = list->last = (listNode *)malloc(sizeof(listNode)))) return -2; + + list->first->next = NULL; + return 0; + +} + +int isEmpty(LLDList *list){ + if (list == NULL || list->first == NULL) return -1; + + return (list->first->next == NULL); +} + +listPosition first(LLDList *list){ + if(list==NULL || list->first==NULL) return NULL; + + return list->first; +} + +listPosition next(listPosition p, LLDList *list){ + if(list==NULL || list->first==NULL || p==NULL) return NULL; + + return p->next; +} + + +listPosition prev(listPosition p, LLDList *list){ + if(list==NULL || list->first==NULL || p==NULL) return NULL; + + if(p == list->first) + return list->first; + else{ + listPosition previous = list->first; + while(previous->next != p){ + previous = previous->next; + } + return previous; + } +} + +listPosition end(LLDList *list){ + if(list==NULL || list->first==NULL) return NULL; + + return list->last; +} + +int listAdd(listElement x, listPosition p, LLDList *list){ + if(p==NULL || list==NULL || list->first==NULL || list->last==NULL) return -1; + + listNode *new;; + if(NULL == (new = malloc(sizeof(listNode)))) return -2; + + new->element = x; + new->next = p->next; + p->next = new; + + if(p == end(list)) list->last = new; + return 0; +} + + +int listRemove(listPosition p, LLDList *list){ + if(p==NULL || list==NULL || list->first==NULL || list->last==NULL) return -1; + + listPosition toRemove; + + if(p == end(list)){ + list->last = prev(p,list); + list->last->next = NULL; + toRemove = p; + }else{ + toRemove = p->next; + p->next = toRemove->next; + } + + free(toRemove); + return 0; + +} + + +listPosition find(listElement x, LLDList *list, int *position){ + if(list == NULL || list->first==NULL || list->last==NULL){ + *position = -1; + return NULL; + } + + listPosition aux = list->first; + *position = 0; + while(aux != end(list)){ + if(aux->next->element == x) + return aux; + else{ + (*position)++; + aux = aux->next; + } + } + + *position = -2; + return NULL; +} + +listElement get(listPosition p, LLDList *list){ + if(list == NULL || list->first==NULL || list->last==NULL || p==end(list)) return -1; + + return p->next->element; +} + +int clear(LLDList *list){ + if(list == NULL || list->first==NULL) return -1; + + listPosition aux; + + while(list->first->next != NULL){ + aux = list->first->next; + list->first->next = list->first->next->next; + free(aux); + } + + list->first->next = list->last = NULL; + return 0; +} + +int destroy(LLDList *list){ + if(list == NULL || list->first==NULL) return -1; + + clear(list); + free(list->first); + list->first = list->last = NULL; + return 0; +} + +void printList(LLDList *list){ + if(list != NULL && list->first !=NULL){ + printf("\nLIST\n"); + if(!isEmpty(list)){ + listPosition p = list->first->next; + while(p != NULL){ + printf("%d ",p->element); + p = p->next; + } + }else{ + printf("The list is empty"); + } + puts(""); + } + +} + + + + + + + + + + + + + + diff --git a/TADS/Lists/LinkedList Imp/Dummy Node Imp/LLDList.h b/TADS/Lists/LinkedList Imp/Dummy Node Imp/LLDList.h new file mode 100644 index 0000000..334093d --- /dev/null +++ b/TADS/Lists/LinkedList Imp/Dummy Node Imp/LLDList.h @@ -0,0 +1,39 @@ +#ifndef __LLD_LIST_H__ +#define __LLD_LIST_H__ + +//List element +typedef int listElement; + +//List structure with array implementation +typedef struct listNode{ + listElement element; + struct listNode *next; +} listNode; + +typedef struct LLDList { + listNode *first, *last; +} LLDList; + +//List position +typedef listNode* listPosition; + +//Stack methods +int createEmptyList(LLDList *list); +int isEmpty(LLDList *list); + +listPosition first(LLDList *list); +listPosition next(listPosition p, LLDList *list); +listPosition prev(listPosition p, LLDList *list); +listPosition end(LLDList *list); + +int listAdd(listElement x, listPosition p, LLDList *list); +int listRemove(listPosition p, LLDList *list); + +listPosition find(listElement x, LLDList *list, int *position); +listElement get(listPosition p, LLDList *list); + +int clear(LLDList *list); +int destroy(LLDList *list); +void printList(LLDList *list); + +#endif diff --git a/TADS/Lists/LinkedList Imp/Dummy Node Imp/main.c b/TADS/Lists/LinkedList Imp/Dummy Node Imp/main.c new file mode 100644 index 0000000..f8efb9c --- /dev/null +++ b/TADS/Lists/LinkedList Imp/Dummy Node Imp/main.c @@ -0,0 +1,49 @@ +#include +#include + +#include "LLDList.h" + + +int main(int argc, char **argv){ + LLDList *list; + + if(NULL == (list = malloc(sizeof(LLDList)))) return -1; + + + createEmptyList(list); + printList(list); + + listAdd(5,end(list),list); + listAdd(8,end(list),list); + listAdd(3,end(list),list); + + listAdd(4,first(list),list); + printList(list); + + listAdd(7, prev(prev(end(list),list),list), list); + printList(list); + + listRemove(first(list),list); + printList(list); + + listElement element = 7; + int position; + find(element, list, &position); + printf("\n\nElement %d finded at position %d\n",element, position); + + listPosition posToGet = prev(end(list), list); + printf("\nElement %d goted\n",get(posToGet, list)); + + + clear(list); + printList(list); + + destroy(list); + + puts("\n"); + return 0; +} + + + + diff --git a/TADS/Lists/LinkedList Imp/Dummy Node Imp/makefile b/TADS/Lists/LinkedList Imp/Dummy Node Imp/makefile new file mode 100644 index 0000000..d5bb777 --- /dev/null +++ b/TADS/Lists/LinkedList Imp/Dummy Node Imp/makefile @@ -0,0 +1,21 @@ +CC=gcc +#CFLAGS=-c +CFLAGS=-c -g +EXECUTABLE_NAME=LLDList + +all: $(EXECUTABLE_NAME) + +$(EXECUTABLE_NAME): main.o LLDList.o + $(CC) $^ -o $@ -lm + +main.o: main.c + $(CC) $(CFLAGS) main.c + +LLDList.o: LLDList.c LLDList.h + $(CC) $(CFLAGS) LLDList.c + + +clean: + rm *.o + rm $(EXECUTABLE_NAME) +