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}
--- /dev/null
+#include <stdlib.h>
+
+#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;
+}
+
--- /dev/null
+#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 */
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+
+#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;
+}
+