]> web.ist.utl.pt Git - iaed.git/commitdiff
Adding aula09
authorAlexandre P Francisco <aplf@ist.utl.pt>
Mon, 5 May 2014 23:51:04 +0000 (00:51 +0100)
committerAlexandre P Francisco <aplf@ist.utl.pt>
Mon, 5 May 2014 23:51:04 +0000 (00:51 +0100)
aula09/Makefile [new file with mode: 0644]
aula09/item.h [new file with mode: 0644]
aula09/list.c [new file with mode: 0644]
aula09/list.h [new file with mode: 0644]
aula09/main.c [new file with mode: 0644]

diff --git a/aula09/Makefile b/aula09/Makefile
new file mode 100644 (file)
index 0000000..a6d3048
--- /dev/null
@@ -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 (file)
index 0000000..5697036
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef ITEM_H
+#define ITEM_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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 (file)
index 0000000..2cb1633
--- /dev/null
@@ -0,0 +1,46 @@
+#include <stdlib.h>
+
+#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 (file)
index 0000000..a5f731e
--- /dev/null
@@ -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 (file)
index 0000000..1ba7423
--- /dev/null
@@ -0,0 +1,33 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#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;
+}
+