]> web.ist.utl.pt Git - iaed.git/commitdiff
Updating aula09
authorAlexandre P Francisco <aplf@ist.utl.pt>
Tue, 6 May 2014 09:17:16 +0000 (10:17 +0100)
committerAlexandre P Francisco <aplf@ist.utl.pt>
Tue, 6 May 2014 09:17:16 +0000 (10:17 +0100)
aula09/Makefile
aula09/item7.h [new file with mode: 0644]
aula09/list.c
aula09/list7.c [new file with mode: 0644]
aula09/list7.h [new file with mode: 0644]
aula09/main7.c [new file with mode: 0644]

index a6d3048ecb938ae4f1870bc5ffb4cb07c42ef429..f2143ea4365b0f33b7406ac010a874f3dfb65bc4 100644 (file)
@@ -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 (file)
index 0000000..d7f91e4
--- /dev/null
@@ -0,0 +1,13 @@
+#ifndef ITEM_H
+#define ITEM_H
+
+#include <stdio.h>
+
+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 */
index 2cb16333e886437602bada6a30fd98a615410281..03d35fcdd1593a97caeb822efd826200fb567592 100644 (file)
@@ -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 (file)
index 0000000..7bd77c0
--- /dev/null
@@ -0,0 +1,46 @@
+#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;
+}
+
diff --git a/aula09/list7.h b/aula09/list7.h
new file mode 100644 (file)
index 0000000..ee43740
--- /dev/null
@@ -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 (file)
index 0000000..e099644
--- /dev/null
@@ -0,0 +1,33 @@
+#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;
+}
+