Wrote BST tests and list structure
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user