Files
aisd/Lab2/list/list.cpp
T
SnailMan 899c844c78 Refactor benchmark to use randomized vectors instead of sets
- Replaced std::set with std::vector for sequence handling.
- Added randomization of sequences to avoid ordered input bias.
- Removed unused balance function and related code in bst.cpp and bst.h.
- Fixed bugs in list insertion and search logic.
- Updated plot.py to allow custom y-axis labels and enable log scale for build plots.
2025-05-14 19:44:07 +02:00

52 lines
1.0 KiB
C++

#include "list.h"
#include <sys/types.h>
List *insert(List *head, int value) {
if (head == nullptr) {
List *node = new List();
node->data = value;
node->next = nullptr;
return node;
}
if (head->data > value) {
List *newHead = new List();
newHead->data = value;
newHead->next = head;
head = newHead;
} else {
List *tmp = head;
while (tmp->next != nullptr && tmp->next->data < value) {
tmp = tmp->next;
}
List *tail = new List();
tail->data = value;
// 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;
}
List *search(List *list, int value) {
List *ptr = list;
while (ptr != nullptr && ptr->data != value) {
if (ptr->data > value) return nullptr;
ptr = ptr->next;
}
return ptr;
}
// Remove the first element
List *remove(List *head) {
if (head == nullptr) {
return nullptr;
}
List *newHead = head->next;
delete head;
return newHead;
}