From 1c7a8fe964e67bfcdc3e6550d0a090e8686e8387 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 28 Feb 2017 15:46:15 +0100 Subject: [PATCH] TP_9 exo1: split the linked list implementation into reusable code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_9/exo1/Makefile | 31 ++++++++++++++++++++++++++++ TP_9/exo1/clist.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++ TP_9/exo1/clist.h | 16 +++++++++++++++ TP_9/exo1/exo1.c | 18 ++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 TP_9/exo1/Makefile create mode 100644 TP_9/exo1/clist.c create mode 100644 TP_9/exo1/clist.h create mode 100644 TP_9/exo1/exo1.c diff --git a/TP_9/exo1/Makefile b/TP_9/exo1/Makefile new file mode 100644 index 0000000..4db03fb --- /dev/null +++ b/TP_9/exo1/Makefile @@ -0,0 +1,31 @@ +TARGET = exo1 +LIBS = +CC = gcc +# Enforce C11 ISO standard for now +CFLAGS = -std=c11 -g -Wall -Wextra +LDFLAGS = -g -Wall -Wextra + +.PHONY: default all clean + +default: $(TARGET) +all: default + +OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c)) +HEADERS = $(wildcard *.h) + +%.o: %.c $(HEADERS) + $(CC) $(CFLAGS) -c $< -o $@ + +.PRECIOUS: $(TARGET) $(OBJECTS) + +$(TARGET): $(OBJECTS) + $(CC) $(OBJECTS) $(LDFLAGS) $(LIBS) -o $@ + +clean: + -rm -f $(TARGET) $(OBJECTS) + +disassemble: $(TARGET) + objdump -d $< | less + +symbols: $(TARGET) + objdump -t $< | sort | less diff --git a/TP_9/exo1/clist.c b/TP_9/exo1/clist.c new file mode 100644 index 0000000..69fd8cc --- /dev/null +++ b/TP_9/exo1/clist.c @@ -0,0 +1,51 @@ +#include + +#include "clist.h" + +link_t* list_new(int value) { + link_t* link_t_new; + link_t_new = malloc(sizeof(link_t)); + link_t_new->value = value; + link_t_new->next = NULL; + return link_t_new; +} + +void list_append(link_t* head, int value) { + + while (head->next != NULL) { + head = head->next; + } + head->next = list_new(value); +} + +unsigned list_count(link_t* head) { + // And if head is not defined? + int count = 1; + + while (head->next != NULL) { + ++count; + head = head->next; + } + return count; +} + +int list_get(link_t* head, unsigned index) { + + if (index < list_count(head)) { + for (unsigned i = 0; i < index; i++) { + head = head->next; + } + return head->value; + } else { + return -1; + } +} + +void list_clear(link_t* link) { + + while (link != NULL) { + link_t* next_link = link; + free(link); + link = next_link; + } +} diff --git a/TP_9/exo1/clist.h b/TP_9/exo1/clist.h new file mode 100644 index 0000000..02a59be --- /dev/null +++ b/TP_9/exo1/clist.h @@ -0,0 +1,16 @@ +#ifndef CLIST_H +#define CLIST_H + +/** Linked list of int */ +typedef struct link_s { + int value; + struct link_s* next; +} link_t; + +link_t* list_new(int value); +void list_append(link_t* head, int value); +unsigned list_count(link_t* head); +int list_get(link_t* head, unsigned index); +void list_clear(link_t* link); + +#endif /* CLIST_H */ diff --git a/TP_9/exo1/exo1.c b/TP_9/exo1/exo1.c new file mode 100644 index 0000000..e70d29b --- /dev/null +++ b/TP_9/exo1/exo1.c @@ -0,0 +1,18 @@ +#include + +#include "clist.h" + +int main() { + link_t* head = list_new(1); + printf("Longueur de la liste: %d\n", list_count(head)); + list_append(head, 2); + list_append(head, 3); + list_append(head, 4); + printf("Longueur de la liste: %d\n", list_count(head)); + printf("Valeur a index %d: %d\n", 2, list_get(head, 2)); + printf("Valeur a index %d: %d\n", 3, list_get(head, 3)); + printf("Valeur a index %d: %d\n", 4, list_get(head, 4)); + list_clear(head); + + return 0; +} -- 2.34.1