From: Jérôme Benoit Date: Tue, 28 Feb 2017 17:48:17 +0000 (+0100) Subject: TP_9 exo2: preliminary work on linked list helpers. X-Git-Url: https://git.piment-noir.org/?p=TD_C.git;a=commitdiff_plain;h=f8051b356ef079c703c1dc0369a135b5032f4027 TP_9 exo2: preliminary work on linked list helpers. Signed-off-by: Jérôme Benoit --- diff --git a/TP_9/exo2/Makefile b/TP_9/exo2/Makefile new file mode 100644 index 0000000..20b3d81 --- /dev/null +++ b/TP_9/exo2/Makefile @@ -0,0 +1,31 @@ +TARGET = exo2 +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/exo2/clist.c b/TP_9/exo2/clist.c new file mode 100644 index 0000000..db08117 --- /dev/null +++ b/TP_9/exo2/clist.c @@ -0,0 +1,73 @@ +#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; +} + +link_t* list_append(link_t* head, int value) { + + if (head == NULL) { + return head = list_new(value); + } else { + link_t* head_first = head; + while (head->next != NULL) { + head = head->next; + } + head->next = list_new(value); + return head_first; + } +} + +link_t* list_prepend(link_t* head, int value) { + link_t* first_link = list_new(value); + + first_link->next = head; + return first_link; +} + +unsigned list_count(link_t* head) { + int count = 1; + + if (head == NULL) { return 0; } + while (head->next != NULL) { + ++count; + head = head->next; + } + return count; +} + +void list_set(link_t* head, unsigned index, int value) { + + // FIXME: check for the index value validity + for (unsigned count = 0; count < index; count++) { + head = head->next; + } + head->value = value; +} + +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->next; + free(link); + link = next_link; + } +} diff --git a/TP_9/exo2/clist.h b/TP_9/exo2/clist.h new file mode 100644 index 0000000..ec1c13a --- /dev/null +++ b/TP_9/exo2/clist.h @@ -0,0 +1,18 @@ +#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); +link_t* list_append(link_t* head, int value); +link_t* list_prepend(link_t* head, int value); +unsigned list_count(link_t* head); +void list_set(link_t* head, unsigned index, int value); +int list_get(link_t* head, unsigned index); +void list_clear(link_t* link); + +#endif /* CLIST_H */ diff --git a/TP_9/exo2/exo2.c b/TP_9/exo2/exo2.c new file mode 100644 index 0000000..15172ce --- /dev/null +++ b/TP_9/exo2/exo2.c @@ -0,0 +1,26 @@ +#include + +#include "clist.h" + +int main() { + link_t* head = NULL; + printf("Longueur de la liste: %d\n", list_count(head)); + head = list_append(head, 1); + head = list_append(head, 2); + head = list_append(head, 3); + head = list_append(head, 4); + printf("Longueur de la liste: %d\n", list_count(head)); + printf("Valeur a index %d: %d\n", 0, list_get(head, 0)); + printf("Valeur a index %d: %d\n", 1, list_get(head, 1)); + printf("Valeur a index %d: %d\n", 2, list_get(head, 2)); + printf("Valeur a index %d: %d\n", 3, list_get(head, 3)); + head = list_prepend(head, 5); + printf("Longueur de la liste: %d\n", list_count(head)); + printf("Valeur a index %d: %d\n", 0, list_get(head, 0)); + printf("Valeur a index %d: %d\n", 4, list_get(head, 4)); + list_set(head, 0, 78); + printf("Valeur a index %d: %d\n", 0, list_get(head, 0)); + list_clear(head); + + return 0; +}