first commit, initial layout and some ideas I want to implement, included my linked list impl
This commit is contained in:
67
src/nickel/linkedlist.c
Normal file
67
src/nickel/linkedlist.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "nickel/linkedlist.h"
|
||||
|
||||
void ni_list__init(ni_list* l)
|
||||
{
|
||||
l->head.next = &l->head;
|
||||
l->head.prev = &l->head;
|
||||
l->count = 0;
|
||||
}
|
||||
|
||||
bool ni_list__is_empty(ni_list* l) {
|
||||
// only accept if head.next is head (circular head)
|
||||
// as the indicator of empty-ness.
|
||||
return l->head.next == &l->head;
|
||||
}
|
||||
|
||||
void ni_list__insert_after(ni_list* l, ni_list_node* entry, ni_list_node* n) {
|
||||
// setup our incomming node as the next node after the insert point
|
||||
n->next = entry->next;
|
||||
n->prev = entry;
|
||||
|
||||
// update the following node to point at our new next node
|
||||
entry->next->prev = n;
|
||||
|
||||
// update the insert point to point at the new node
|
||||
entry->next = n;
|
||||
l->count++;
|
||||
}
|
||||
|
||||
void ni_list__insert_before(ni_list* l, ni_list_node* entry, ni_list_node* n) {
|
||||
// setup our incomming node as the next node before the insert point
|
||||
n->prev = entry->prev;
|
||||
n->next = entry;
|
||||
|
||||
// update the previous node to point at our new next node
|
||||
entry->prev->next = n;
|
||||
|
||||
// update our insert point with the new prev
|
||||
entry->prev = n;
|
||||
l->count++;
|
||||
}
|
||||
|
||||
void ni_list__push_front(ni_list* l, ni_list_node* n) {
|
||||
ni_list__insert_after(l, &l->head, n);
|
||||
}
|
||||
|
||||
void ni_list__push_back(ni_list* l, ni_list_node* n) {
|
||||
ni_list__insert_before(l, &l->head, n);
|
||||
}
|
||||
|
||||
void ni_list__remove(ni_list* l, ni_list_node* n) {
|
||||
// set node before our node to point back to the node after n.
|
||||
n->prev->next = n->next;
|
||||
|
||||
// set node after our node to point back to the node before n
|
||||
n->next->prev = n->prev;
|
||||
|
||||
// poison our removed node
|
||||
n->next = NULL;
|
||||
n->prev = NULL;
|
||||
|
||||
l->count--;
|
||||
}
|
||||
Reference in New Issue
Block a user