feat: extend kmain with listInit and listEmpty
This commit is contained in:
1 parent
21f2719413
commit
8101704cc3
2 files changed
+167
-1
No files matched your search
+61
@@ -0,0 +1,61 @@
|
||||
#include "list.h"
|
||||
|
||||
void listInit(ListNode* head)
|
||||
{
|
||||
head->next = head;
|
||||
head->prev = head;
|
||||
}
|
||||
|
||||
bool listEmpty(const ListNode* head)
|
||||
{
|
||||
return head->next == head;
|
||||
}
|
||||
|
||||
void listInsertAfter(ListNode* pos, ListNode* newNode)
|
||||
{
|
||||
newNode->prev = pos;
|
||||
newNode->next = pos->next;
|
||||
pos->next->prev = newNode;
|
||||
pos->next = newNode;
|
||||
}
|
||||
|
||||
void listInsertBefore(ListNode* pos, ListNode* newNode)
|
||||
{
|
||||
listInsertAfter(pos->prev, newNode);
|
||||
}
|
||||
|
||||
void listPushBack(ListNode* head, ListNode* newNode)
|
||||
{
|
||||
listInsertBefore(head, newNode);
|
||||
}
|
||||
|
||||
void listPushFront(ListNode* head, ListNode* newNode)
|
||||
{
|
||||
listInsertAfter(head, newNode);
|
||||
}
|
||||
|
||||
void listRemove(ListNode* node)
|
||||
{
|
||||
node->prev->next = node->next;
|
||||
node->next->prev = node->prev;
|
||||
node->next = node;
|
||||
node->prev = node;
|
||||
}
|
||||
|
||||
usize listLen(const ListNode* head)
|
||||
{
|
||||
usize count = 0;
|
||||
for (const ListNode* cursor = head->next; cursor != head; cursor = cursor->next)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
ListNode* listFront(const ListNode* head)
|
||||
{
|
||||
return listEmpty(head) ? nullptr : head->next;
|
||||
}
|
||||
|
||||
ListNode* listBack(const ListNode* head)
|
||||
{
|
||||
return listEmpty(head) ? nullptr : head->prev;
|
||||
}
|
||||
Reference in new issue
Block a user