From 2305571445ae35a70695e3653ae84e12c0e30f49 Mon Sep 17 00:00:00 2001 From: Alexandre P Francisco Date: Tue, 6 May 2014 00:51:04 +0100 Subject: [PATCH] Adding aula09 --- aula09/Makefile | 14 ++++++++++++++ aula09/item.h | 15 +++++++++++++++ aula09/list.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ aula09/list.h | 18 ++++++++++++++++++ aula09/main.c | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+) create mode 100644 aula09/Makefile create mode 100644 aula09/item.h create mode 100644 aula09/list.c create mode 100644 aula09/list.h create mode 100644 aula09/main.c diff --git a/aula09/Makefile b/aula09/Makefile new file mode 100644 index 0000000..a6d3048 --- /dev/null +++ b/aula09/Makefile @@ -0,0 +1,14 @@ +CC=gcc +CFLAGS=-Wall + +EXECS=charlst + +OBJS=main.o list.o + +all: ${EXECS} + +charlst: ${OBJS} + ${CC} ${CFLAGS} -o $@ ${OBJS} + +clean: + rm -f *.o ${EXECS} diff --git a/aula09/item.h b/aula09/item.h new file mode 100644 index 0000000..5697036 --- /dev/null +++ b/aula09/item.h @@ -0,0 +1,15 @@ +#ifndef ITEM_H +#define ITEM_H + +#include +#include +#include + +typedef char* Item; + +#define newItem strdup +#define itemCompare strcmp +#define showItem(A) printf("%s\n", A) +#define deleteItem free + +#endif /* ITEM_H */ diff --git a/aula09/list.c b/aula09/list.c new file mode 100644 index 0000000..2cb1633 --- /dev/null +++ b/aula09/list.c @@ -0,0 +1,46 @@ +#include + +#include "list.h" +#include "item.h" + +link insertSorted(link head, Item i) { + link iter = head, aux = NULL, n; + + for (; iter != NULL && itemCompare(iter->item, i) < 0; + aux = iter, iter = iter->next); + + n = malloc(sizeof(struct node)); + n->item = newItem(i); + n->next = iter; + if (aux != NULL) aux->next = n; + else head = n; + + return head; +} + +void show(link head) { + for (; head; head = head->next) + showItem(head->item); +} + +int search(link head, Item i) { + for (; head && itemCompare(head->item, i) < 0; head = head->next); + return head != NULL && !itemCompare(head->item, i); +} + +link lremove(link head, Item i) { + link iter = head, aux = NULL; + + for (; iter != NULL && itemCompare(iter->item, i) < 0; + aux = iter, iter = iter->next); + + if (iter != NULL && !itemCompare(iter->item, i)) { + if (aux != NULL) aux->next = iter->next; + else head = iter->next; + free(iter->item); + free(iter); + } + + return head; +} + diff --git a/aula09/list.h b/aula09/list.h new file mode 100644 index 0000000..a5f731e --- /dev/null +++ b/aula09/list.h @@ -0,0 +1,18 @@ +#ifndef LIST_H +#define LIST_H + +#include "item.h" + +typedef struct node *link; + +struct node { + Item item; + link next; +}; + +link insertSorted(link head, Item i); +void show(link head); +int search(link head, Item i); +link lremove(link head, Item i); + +#endif /* LIST_H */ diff --git a/aula09/main.c b/aula09/main.c new file mode 100644 index 0000000..1ba7423 --- /dev/null +++ b/aula09/main.c @@ -0,0 +1,33 @@ +#include +#include + +#include "item.h" +#include "list.h" + +int main(int argc, char * argv[]) { + link lst = NULL, aux; + + while (argc--) + lst = insertSorted(lst, argv[argc]); + show(lst); + + printf("----\n"); + printf("xpto? %d\n", search(lst, "xpto")); + printf("----\n"); + + lst = lremove(lst, argv[0]); + show(lst); + + printf("----\n"); + printf("xpto? %d\n", search(lst, "xpto")); + + while (lst) { + aux = lst; + lst = lst->next; + free(aux->item); + free(aux); + } + + return EXIT_SUCCESS; +} + -- 2.30.2