summaryrefslogtreecommitdiff
path: root/comp/cw/code/tokenizer/ads/ll
diff options
context:
space:
mode:
Diffstat (limited to 'comp/cw/code/tokenizer/ads/ll')
-rw-r--r--comp/cw/code/tokenizer/ads/ll/Makefile5
-rw-r--r--comp/cw/code/tokenizer/ads/ll/ll.c50
-rw-r--r--comp/cw/code/tokenizer/ads/ll/ll.h15
-rw-r--r--comp/cw/code/tokenizer/ads/ll/ll.obin0 -> 2184 bytes
-rwxr-xr-xcomp/cw/code/tokenizer/ads/ll/lltestbin0 -> 20592 bytes
-rw-r--r--comp/cw/code/tokenizer/ads/ll/lltest.c18
6 files changed, 88 insertions, 0 deletions
diff --git a/comp/cw/code/tokenizer/ads/ll/Makefile b/comp/cw/code/tokenizer/ads/ll/Makefile
new file mode 100644
index 0000000..aaf0241
--- /dev/null
+++ b/comp/cw/code/tokenizer/ads/ll/Makefile
@@ -0,0 +1,5 @@
+all: ll.c
+ cc ll.c -c -o ll.o
+
+test: all
+ cc ll.o lltest.c -o lltest
diff --git a/comp/cw/code/tokenizer/ads/ll/ll.c b/comp/cw/code/tokenizer/ads/ll/ll.c
new file mode 100644
index 0000000..c45ab0c
--- /dev/null
+++ b/comp/cw/code/tokenizer/ads/ll/ll.c
@@ -0,0 +1,50 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+typedef struct ll_t ll_t;
+
+typedef struct ll_t {
+ void *data;
+ ll_t *next;
+} ll_t;
+
+ll_t *llalloc(){
+ ll_t *output = malloc(sizeof(ll_t));
+ if (output == NULL)
+ return NULL;
+ output->next = NULL;
+ return output;
+}
+
+void llsetdata(ll_t *node, void *data){
+ node->data = malloc(sizeof(data));
+ memcpy(node->data, data, sizeof(data));
+}
+
+void llsetnext(ll_t *node, ll_t *next){
+ if (node->next == NULL)
+ node->next = next;
+ else
+ llsetnext(node->next, next);
+}
+
+void *llgetat(ll_t *head, int index){
+ if (index == 0)
+ return head->data;
+ else {
+ if (head->next != NULL) {
+ return llgetat(head->next, index - 1);
+ }else {
+ return NULL;
+ }
+ }
+}
+
+void llfreeall(ll_t *head){
+ if (head->next != NULL)
+ llfreeall(head->next);
+ free(head->data);
+ free(head);
+}
+
diff --git a/comp/cw/code/tokenizer/ads/ll/ll.h b/comp/cw/code/tokenizer/ads/ll/ll.h
new file mode 100644
index 0000000..30830b1
--- /dev/null
+++ b/comp/cw/code/tokenizer/ads/ll/ll.h
@@ -0,0 +1,15 @@
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct ll_t ll_t;
+
+typedef struct ll_t {
+ void *data;
+ ll_t *next;
+} ll_t;
+
+ll_t *llalloc();
+void llsetdata(ll_t *node, void *data);
+void llsetnext(ll_t *node, ll_t *next);
+void *llgetat(ll_t *head, int index);
+void llfreeall(ll_t *head);
diff --git a/comp/cw/code/tokenizer/ads/ll/ll.o b/comp/cw/code/tokenizer/ads/ll/ll.o
new file mode 100644
index 0000000..b033278
--- /dev/null
+++ b/comp/cw/code/tokenizer/ads/ll/ll.o
Binary files differ
diff --git a/comp/cw/code/tokenizer/ads/ll/lltest b/comp/cw/code/tokenizer/ads/ll/lltest
new file mode 100755
index 0000000..b98c5de
--- /dev/null
+++ b/comp/cw/code/tokenizer/ads/ll/lltest
Binary files differ
diff --git a/comp/cw/code/tokenizer/ads/ll/lltest.c b/comp/cw/code/tokenizer/ads/ll/lltest.c
new file mode 100644
index 0000000..8aa6514
--- /dev/null
+++ b/comp/cw/code/tokenizer/ads/ll/lltest.c
@@ -0,0 +1,18 @@
+#include <stdio.h>
+
+#include "ll.h"
+
+int main(){
+ ll_t *head = llalloc();
+ llsetdata(head, "hello");
+
+ for (int i = 0; i < 3; i++){
+ ll_t *node = llalloc();
+ llsetdata(node, "hi");
+ llsetnext(head, node);
+ }
+ for (int i = 0; i < 4; i++)
+ printf("%s\n", (char *)llgetat(head, i));
+
+ llfreeall(head);
+}