not sus at all
This commit is contained in:
1 parent
321323191a
commit
8202b00522
12 files changed
+224
-110
No files matched your search
+119
@@ -0,0 +1,119 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
/*
|
||||
Basic operations are often defined
|
||||
1 init(): create an empty list
|
||||
2 isEmpty(): check if the list is empty
|
||||
3 insert(): add new item in a list
|
||||
4 remove(): remove an item from a list
|
||||
Some other possible operations can be used:
|
||||
1 length(): return the length of a list
|
||||
2 search(): search a specific element in a list
|
||||
3 display(): display a list
|
||||
4 sort(): sort all items in a list
|
||||
*/
|
||||
|
||||
typedef struct Node
|
||||
{
|
||||
uintptr_t ptr;
|
||||
node *next;
|
||||
node *prev;
|
||||
} node;
|
||||
|
||||
void pair2(node *a, node *b)
|
||||
{
|
||||
if (a)
|
||||
a->next = b;
|
||||
if (b)
|
||||
b->prev = a;
|
||||
}
|
||||
|
||||
void pair3(node *a, node *b, node *c)
|
||||
{
|
||||
if (b)
|
||||
{
|
||||
pair2(a, b);
|
||||
pair2(b, c);
|
||||
}
|
||||
}
|
||||
|
||||
uintptr_t node_into_inner(node *n)
|
||||
{
|
||||
uintptr_t prt = n->ptr;
|
||||
free(n);
|
||||
return prt;
|
||||
};
|
||||
|
||||
typedef struct DoublyLinkedList
|
||||
{
|
||||
int size;
|
||||
node *head;
|
||||
node *tail;
|
||||
} dll;
|
||||
|
||||
dll *init()
|
||||
{
|
||||
dll *l = (dll *)malloc(sizeof(dll));
|
||||
if (!l)
|
||||
return NULL;
|
||||
l->size = 0;
|
||||
l->head = NULL;
|
||||
l->tail = NULL;
|
||||
return l;
|
||||
}
|
||||
node *new(uintptr_t ptr)
|
||||
{
|
||||
node *n = (node *)malloc(sizeof(node));
|
||||
if (!n)
|
||||
return NULL;
|
||||
|
||||
n->ptr = ptr;
|
||||
n->prev = NULL;
|
||||
n->next = NULL;
|
||||
return n;
|
||||
}
|
||||
|
||||
void push_front(dll *list, node *node_)
|
||||
{
|
||||
// border case
|
||||
if (list->head == NULL)
|
||||
{
|
||||
list->head = node_;
|
||||
list->tail = node_;
|
||||
// teach never tells you what to do when this happens;
|
||||
}
|
||||
else
|
||||
{
|
||||
node *second = list->head;
|
||||
pair2(node_, second);
|
||||
list->head = node_;
|
||||
};
|
||||
list->size++;
|
||||
}
|
||||
void push_back(dll *list, node *node_)
|
||||
{
|
||||
// border case
|
||||
if (list->tail == NULL)
|
||||
{
|
||||
list->head = node_;
|
||||
list->tail = node_;
|
||||
// teach never tells you what to do when this happens;
|
||||
}
|
||||
else
|
||||
{
|
||||
node *secondlast = list->head;
|
||||
pair2(secondlast, node_);
|
||||
list->tail = node_;
|
||||
};
|
||||
list->size++;
|
||||
}
|
||||
|
||||
// void remove(dll* l, ){
|
||||
|
||||
// }
|
||||
|
||||
int main(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user