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

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();
}