list: Permet d'insérer et de récupérer la taille
This commit is contained in:
@@ -14,3 +14,8 @@ build/testLinkedList.o: test/testLinkedList.c src/linkedList.h | build
|
||||
|
||||
build:
|
||||
mkdir build
|
||||
|
||||
clean:
|
||||
rm -rf testLinkedList build
|
||||
|
||||
.PHONY: all test clean
|
||||
|
@@ -1,11 +1,32 @@
|
||||
#include "linkedList.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
LinkedList createLinkedList(void) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void insertAtListHead(LinkedList *list, int value) {
|
||||
struct list_node *node = malloc(sizeof(struct list_node));
|
||||
if (node == NULL) {
|
||||
fprintf(stderr, "Memory exhausted!\n");
|
||||
abort();
|
||||
}
|
||||
node->value = value;
|
||||
node->next = *list;
|
||||
*list = node;
|
||||
}
|
||||
|
||||
int linkedListLength(LinkedList list) {
|
||||
int n = 0;
|
||||
while (list != NULL) {
|
||||
n += 1;
|
||||
list = list->next;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
void freeLinkedList(LinkedList *list) {
|
||||
struct list_node *next;
|
||||
struct list_node *node = *list;
|
||||
|
@@ -8,6 +8,10 @@ typedef struct list_node {
|
||||
|
||||
LinkedList createLinkedList(void);
|
||||
|
||||
void insertAtListHead(LinkedList *list, int value);
|
||||
|
||||
int linkedListLength(LinkedList list);
|
||||
|
||||
void freeLinkedList(LinkedList *list);
|
||||
|
||||
#endif // MY_LINKED_LIST_H
|
||||
|
@@ -5,6 +5,9 @@
|
||||
|
||||
int main(void) {
|
||||
LinkedList list = createLinkedList();
|
||||
insertAtListHead(&list, 5);
|
||||
insertAtListHead(&list, 9);
|
||||
assert(linkedListLength(list) == 2);
|
||||
freeLinkedList(&list);
|
||||
printf("Success!\n");
|
||||
return 0;
|
||||
|
Reference in New Issue
Block a user