add my unfinished cbase

This commit is contained in:
joachimschmidt557 2020-06-09 17:51:23 +02:00
parent 501065651d
commit bb63a9dd3c
16 changed files with 277 additions and 0 deletions

0
cbase/data/Makefile Normal file
View file

26
cbase/data/lists/Makefile Normal file
View file

@ -0,0 +1,26 @@
OBJ=linkedlist.o
SRC=$(OBJ:.o=.c)
HDR=$(SRC:.c=.h)
TST=$(SRC:.c=.test.c)
TSTBIN=$(TST:.test.c=.test)
CFLAGS+=-Wall
CFLAGS+=-Wextra
all: $(OBJ)
.PHONY: all
test: $(TSTBIN)
.PHONY: test
clean:
$(RM) $(OBJ) $(TSTBIN)
%.o: %.c %.h
$(CC) -o $@ -c $< $(CFLAGS)
$(TSTBIN): $(TST) $(OBJ)
$(CC) -o $@ $(@:=.c) $(@:.test=.c) $(CFLAGS)
./$@

View file

@ -0,0 +1,116 @@
#include <stdlib.h>
#include "linkedlist.h"
LinkedList*
new_linkedlist() {
// TODO: handle malloc failure
LinkedListNode* nil = malloc(sizeof(LinkedListNode));
nil->next = nil;
nil->prev = nil;
LinkedList* l = malloc(sizeof(LinkedList));
l->nil = nil;
return l;
}
LinkedListNode*
first(LinkedList* l) {
return l->nil->next;
}
LinkedListNode*
last(LinkedList* l) {
return l->nil->prev;
}
void
append(LinkedList* l, void* x) {
LinkedListNode* nil = l->nil;
// TODO: handle malloc failure
LinkedListNode* y = malloc(sizeof(LinkedListNode));
y->next = nil;
y->prev = nil->prev->prev;
y->data = x;
nil->prev->prev->next = y;
nil->prev = y;
}
void
insert(LinkedList* l, void* x) {
LinkedListNode* nil = l->nil;
// TODO: handle malloc failure
LinkedListNode* y = malloc(sizeof(LinkedListNode));
y->prev = nil;
y->next = nil->next;
y->data = x;
nil->next->prev = x;
nil->next = x;
}
void
insert_at(LinkedList* l, int at, void* x) {
LinkedListNode* nil = l->nil;
LinkedListNode* y = nil;
for (int i = 0; i < at; i++) {
y = y->next;
if (y == nil) {
// Index out of range
}
}
// TODO: handle malloc failure
LinkedListNode* new = malloc(sizeof(LinkedListNode));
new->prev = y;
new->next = y->next;
new->data = x;
y->next->prev = new;
y->next = new;
}
LinkedListNode*
search(LinkedList* l, void* x) {
LinkedListNode* y = l->nil->next;
while (y != l->nil) {
if (y->data == x)
return y;
y = y->next;
}
return NULL;
}
void
delete_item(LinkedListNode* x) {
x->prev->next = x->next;
x->next->prev = x->prev;
free(x);
}
void
delete_linkedlist(LinkedList* l) {
free(l->nil);
free(l);
}
int
count_linkedlist(LinkedList* l) {
int n = 0;
LinkedListNode* x = l->nil;
while (x->next != l->nil) {
x = x->next;
n++;
}
return n;
}

View file

@ -0,0 +1,44 @@
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
typedef struct LinkedListNode {
void* data;
struct LinkedListNode* prev;
struct LinkedListNode* next;
} LinkedListNode;
typedef struct LinkedList {
LinkedListNode* nil;
} LinkedList;
LinkedList*
new_linkedlist();
LinkedListNode*
first(LinkedList* l);
LinkedListNode*
last(LinkedList* l);
void
append(LinkedList* l, void* x);
void
insert(LinkedList* l, void* x);
void
insert_at(LinkedList* l, int i, void* x);
LinkedListNode*
search(LinkedList* l, void* x);
void
delete_item(LinkedListNode* x);
void
delete_linkedlist(LinkedList* l);
int
count_linkedlist(LinkedList* l);
#endif

BIN
cbase/data/lists/linkedlist.test Executable file

Binary file not shown.

View file

@ -0,0 +1,35 @@
#include<stdio.h>
#include<assert.h>
#include "linkedlist.h"
void
test_empty() {
LinkedList* l = new_linkedlist();
assert(count_linkedlist(l) == 0);
delete_linkedlist(l);
}
void
test_single() {
LinkedList* l = new_linkedlist();
int x = 42;
int* p = &x;
append(l, p);
assert(count_linkedlist(l) == 1);
assert(first(l)->data == p);
assert(last(l)->data == p);
delete_item(first(l));
assert(count_linkedlist(l) == 0);
delete_linkedlist(l);
}
int
main() {
test_empty();
test_single();
}

0
cbase/data/stack.c Normal file
View file

0
cbase/data/stack.h Normal file
View file

14
cbase/data/trees/Makefile Normal file
View file

@ -0,0 +1,14 @@
OBJ=binarytree.o bst.o rbtree.o
SRC=$(OBJ:.o=.c)
HDR=$(SRC:.c=.h)
CFLAGS+=-Wall
CFLAGS+=-Wextra
all: $(OBJ)
clean:
$(RM) $(OBJ)
%.o: %.c %.h
$(CC) -o $@ -c $< $(CFLAGS)

View file

@ -0,0 +1 @@
#include "binarytree.h"

View file

@ -0,0 +1,13 @@
#ifndef BINARYTREE_H
#define BINARYTREE_H
typedef struct BinaryTreeNode {
void* data;
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
} BinaryTreeNode;
BinaryTreeNode*
new_binarytree();
#endif

1
cbase/data/trees/bst.c Normal file
View file

@ -0,0 +1 @@
#include "bst.h"

5
cbase/data/trees/bst.h Normal file
View file

@ -0,0 +1,5 @@
#ifndef BST_H
#define BST_H
#endif

View file

@ -0,0 +1 @@
#include "rbtree.h"

16
cbase/data/trees/rbtree.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef RBTREE_H
#define RBTREE_H
typedef enum {
Red,
Black,
} RBColor;
typedef struct RBTreeNode {
void* data;
struct RBTreeNode* left;
struct RBTreeNode* right;
RBColor color;
} RBTreeNode;
#endif