Wrote BST tests and list structure

This commit is contained in:
2025-05-04 21:54:22 +02:00
parent 1c8ab00c13
commit b14156589c
7 changed files with 151 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
#include "list.h"
List *insert(List *head, int data) {
if (head == nullptr) {
List *node = new List();
node->data = data;
node->next = nullptr;
return node;
}
if (head->data > data) {
List *newHead = new List();
newHead->data = data;
newHead->next = head;
head = newHead;
} else {
List *tmp = head;
while (tmp->next != nullptr && tmp->data < data) {
tmp = head->next;
}
List *tail = new List();
tail->data = data;
// Set the pointer to the next, we don't know if its at the end or not
tail->next = tmp->next;
tmp->next = tail;
}
return head;
}