From d610ee3a18a662485d6a00b68e854ee97a278527 Mon Sep 17 00:00:00 2001 From: Jonathan Hager Date: Mon, 21 Nov 2022 23:50:32 +0100 Subject: [PATCH] Basic double linked list added --- .vscode/settings.json | 3 ++- List/list.c | 58 +++++++++++++++++++++++++++++++++++++++++++ List/list.h | 29 ++++++++++++++++++++++ List/nodeData.h | 12 +++++++++ Makefile | 7 ++++-- main.c | 12 +++++++++ 6 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 List/list.c create mode 100644 List/list.h create mode 100644 List/nodeData.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 23106ef..694ddf3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "files.associations": { "inputhandling.h": "c" - } + }, + "C_Cpp.errorSquiggles": "Disabled" } \ No newline at end of file diff --git a/List/list.c b/List/list.c new file mode 100644 index 0000000..5a5b7bc --- /dev/null +++ b/List/list.c @@ -0,0 +1,58 @@ +#include "list.h" +#include +#include + +Node * ListCreateNode(int *data){ + Node *new = (Node *) malloc(sizeof(Node)); + new->data = *data; + return new; +} + +void ListPrintForward(List *list){ + Node *current = list->head; + + printf("\n["); + while(current != 0){ + printf("%d,", current->data); + current = current->next; + } + printf("]\n"); +} + +void ListInsertFront(List *list, int data){ + Node *new = ListCreateNode(&data); + + if(list->head == 0){ + list->head = new; + list->tail = new; + } + else if(list->head == list->tail){ + list->head = new; + list->head->next = list->tail; + list->tail->prev = list->head; + } + else{ + list->head->prev = new; + new->next = list->head; + list->head = new; + } +} + +void ListInsertBack(List *list, int data){ + Node *new = ListCreateNode(&data); + + if(list->head == 0){ + list->head = new; + list->tail = new; + } + else if(list->head == list->tail){ + list->tail = new; + list->head->next = list->tail; + list->tail->prev = list->head; + } + else{ + list->tail->next = new; + new->prev = list->tail; + list->tail = new; + } +} \ No newline at end of file diff --git a/List/list.h b/List/list.h new file mode 100644 index 0000000..ea3d2e8 --- /dev/null +++ b/List/list.h @@ -0,0 +1,29 @@ +#ifndef LIST_H_ +#define LIST_H_ +#include "nodeData.h" + +typedef struct Node Node; +typedef struct List List; + +typedef struct List { + Node *head; + Node *tail; +} List; + +typedef struct Node { + int data; + + Node *next; + Node *prev; +} Node; + +//Only for internal purpose +Node * ListCreateNode(int *data); + +//Print the list in order +void ListPrintForward(List *list); + +void ListInsertFront(List *list, int data); +void ListInsertBack(List *list, int data); + +#endif \ No newline at end of file diff --git a/List/nodeData.h b/List/nodeData.h new file mode 100644 index 0000000..d6ac082 --- /dev/null +++ b/List/nodeData.h @@ -0,0 +1,12 @@ +#ifndef NODEDATA_H_ +#define NODEDATA_H_ + +#include "../sprite.h" + +typedef union NodeData { + int dataInt; + float dataFloat; + Sprite dataSprite; +} NodeData; + +#endif \ No newline at end of file diff --git a/Makefile b/Makefile index 044400c..6caa4e4 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -spiel: main.o sprite.o inputHandler.o - gcc -o spiel main.o sprite.o inputHandler.o -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 +spiel: main.o sprite.o inputHandler.o list.o + gcc -o spiel main.o list.o sprite.o inputHandler.o -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 main.o: main.c gcc -c main.c -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 @@ -10,5 +10,8 @@ sprite.o: sprite.c inputHandler.o: inputHandler.c gcc -c inputHandler.c -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 +list.o: List/list.c + gcc -c List/list.c + clean: rm *.o spiel diff --git a/main.c b/main.c index f076b8b..12ad32e 100644 --- a/main.c +++ b/main.c @@ -3,8 +3,20 @@ #include "sprite.h" #include "inputHandler.h" #include "raymath.h" +#include "List/list.h" int main(){ + + List testListe; + testListe.head = 0; + testListe.tail = 0; + ListInsertBack(&testListe, 1); + ListInsertBack(&testListe, 2); + ListInsertBack(&testListe, 3); + ListInsertFront(&testListe, 6); + ListInsertBack(&testListe, 4); + ListInsertBack(&testListe, 5); + ListPrintForward(&testListe); InitWindow(800, 450, "basic window");