From 2cc8748af10522d9629cf2159c74faff866b630c Mon Sep 17 00:00:00 2001 From: Alexandre P Francisco Date: Tue, 6 May 2014 10:17:16 +0100 Subject: [PATCH] Updating aula09 --- aula09/Makefile | 15 +++++++++++---- aula09/item7.h | 13 +++++++++++++ aula09/list.c | 2 +- aula09/list7.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ aula09/list7.h | 18 ++++++++++++++++++ aula09/main7.c | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 aula09/item7.h create mode 100644 aula09/list7.c create mode 100644 aula09/list7.h create mode 100644 aula09/main7.c diff --git a/aula09/Makefile b/aula09/Makefile index a6d3048..f2143ea 100644 --- a/aula09/Makefile +++ b/aula09/Makefile @@ -1,14 +1,21 @@ CC=gcc CFLAGS=-Wall -EXECS=charlst +EXECS=charlst intlst -OBJS=main.o list.o +CHAROBJS=main.o list.o +CHARHDRS=list.h item.h + +INTOBJS=main7.o list7.o +INTHDRS=list7.h item7.h all: ${EXECS} -charlst: ${OBJS} - ${CC} ${CFLAGS} -o $@ ${OBJS} +charlst: ${CHAROBJS} ${CHARHDRS} + ${CC} ${CFLAGS} -o $@ ${CHAROBJS} + +intlst: ${INTOBJS} ${INTHDRS} + ${CC} ${CFLAGS} -o $@ ${INTOBJS} clean: rm -f *.o ${EXECS} diff --git a/aula09/item7.h b/aula09/item7.h new file mode 100644 index 0000000..d7f91e4 --- /dev/null +++ b/aula09/item7.h @@ -0,0 +1,13 @@ +#ifndef ITEM_H +#define ITEM_H + +#include + +typedef int Item; + +#define newItem(A) (A) +#define itemCompare(A,B) ((A)-(B)) +#define showItem(A) printf("%d\n", A) +#define deleteItem(A) + +#endif /* ITEM_H */ diff --git a/aula09/list.c b/aula09/list.c index 2cb1633..03d35fc 100644 --- a/aula09/list.c +++ b/aula09/list.c @@ -37,7 +37,7 @@ link lremove(link head, Item i) { if (iter != NULL && !itemCompare(iter->item, i)) { if (aux != NULL) aux->next = iter->next; else head = iter->next; - free(iter->item); + deleteItem(iter->item); free(iter); } diff --git a/aula09/list7.c b/aula09/list7.c new file mode 100644 index 0000000..7bd77c0 --- /dev/null +++ b/aula09/list7.c @@ -0,0 +1,46 @@ +#include + +#include "list7.h" +#include "item7.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; + deleteItem(iter->item); + free(iter); + } + + return head; +} + diff --git a/aula09/list7.h b/aula09/list7.h new file mode 100644 index 0000000..ee43740 --- /dev/null +++ b/aula09/list7.h @@ -0,0 +1,18 @@ +#ifndef LIST_H +#define LIST_H + +#include "item7.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/main7.c b/aula09/main7.c new file mode 100644 index 0000000..e099644 --- /dev/null +++ b/aula09/main7.c @@ -0,0 +1,33 @@ +#include +#include + +#include "item7.h" +#include "list.h" + +int main(int argc, char * argv[]) { + link lst = NULL, aux; + + while (argc-- > 1) + lst = insertSorted(lst, atoi(argv[argc])); + show(lst); + + printf("----\n"); + printf("xpto? %d\n", search(lst, 10)); + printf("----\n"); + + lst = lremove(lst, 10); + show(lst); + + printf("----\n"); + printf("xpto? %d\n", search(lst, 10)); + + while (lst) { + aux = lst; + lst = lst->next; + deleteItem(aux->item); + free(aux); + } + + return EXIT_SUCCESS; +} + -- 2.30.2