Browse Source

added example, fixed logic, OT_erase, updated makefile, tests passed

Iver 5 hours ago
parent
commit
542569379e
7 changed files with 110 additions and 22 deletions
  1. 5 8
      Makefile
  2. BIN
      builds/liborangetree.so
  3. BIN
      builds/main.bin
  4. 7 5
      changelog.txt
  5. 14 5
      src/headers/OT_functions.h
  6. 56 0
      src/launch program/main.c
  7. 28 4
      src/main/main.c

+ 5 - 8
Makefile

@@ -1,16 +1,13 @@
 COMPILER=gcc
 COMPILER=gcc
 FLAGS_ALL=-g -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter
 FLAGS_ALL=-g -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter
-FLAGS_EXAMPLE=-Lbuilds/ -lNAMEHERE -lLIBNAMEHERE -Wl,-rpath=builds/ -lm
-FLAGS_LIB=-fPIC -shared -lc -lm 
+FLAGS_EXAMPLE=-Lbuilds/ -lorangetree -Wl,-rpath=builds/ -lm
+FLAGS_LIB=-fPIC -shared -lc
 
 
-main.bin: NAMEHERE.so LIBNAMEHERE.so
+main.bin: liborangetree.so
 	$(COMPILER) $(FLAGS_ALL) src/launch\ program/main.c -o builds/main.bin $(FLAGS_EXAMPLE) 
 	$(COMPILER) $(FLAGS_ALL) src/launch\ program/main.c -o builds/main.bin $(FLAGS_EXAMPLE) 
 
 
-NAMEHERE.so:
-	$(COMPILER) $(FLAGS_ALL) src/main/main.c -o builds/librasteriver.so $(FLAGS_LIB) 
-
-LIBNAMEHERE.so:
-	cp src/libraries/libLIBNAMEHERE.so builds/
+liborangetree.so:
+	$(COMPILER) $(FLAGS_ALL) src/main/main.c -o builds/liborangetree.so $(FLAGS_LIB) 
 
 
 clean:
 clean:
 	rm builds/*
 	rm builds/*

BIN
builds/liborangetree.so


BIN
builds/main.bin


+ 7 - 5
changelog.txt

@@ -1,5 +1,7 @@
--added all basic function of linked list (i didn't do any research so im assuming this is how it works)
--(also i havent tested this at all so idk if it works. lets push to prod!)
--wrote comments in OT_functions.h
--added etymology
--added how-to
+-added example program
+-added OT_iterate function
+-tested everything (it works on my machine, let's push to prod!)
+-added OT_erase
+-OT_drop now doesn't free operand
+-updated makefile
+-fixed logic error in OT_free

+ 14 - 5
src/headers/OT_functions.h

@@ -9,28 +9,37 @@ void* OT_get_last(void* operand);
 // - Takes in any element of the list
 // - Takes in any element of the list
 void* OT_get_first(void* operand);
 void* OT_get_first(void* operand);
 
 
-// - Removes an element from the linked list and frees it
+// - Removes an element from the linked list
 // - Takes in specific element of the list
 // - Takes in specific element of the list
 void OT_drop(void* operand);
 void OT_drop(void* operand);
 
 
+// - Removes an element from the linked list and frees it
+// - Takes in specific element of the list
+void OT_erase(void* operand);
+
 // - Adds an element to the end of a linked list
 // - Adds an element to the end of a linked list
-// - Takes in any element of the list
+// - Takes in any element of the list and an element to add to the list
 void OT_append(void* operand, void* appendee);
 void OT_append(void* operand, void* appendee);
 
 
 // - Adds and element to the beginning of a linked list
 // - Adds and element to the beginning of a linked list
-// - Takes in any element of the list
+// - Takes in any element of the list and an element to add to the list
 void OT_prepend(void* operand, void* prependee);
 void OT_prepend(void* operand, void* prependee);
 
 
 // - Adds an element to the left of an element in a linked list
 // - Adds an element to the left of an element in a linked list
-// - Takes in specific element of the list
+// - Takes in specific element of the list and an element to add to the list
 void OT_insert_left(void* operand, void* insertee);
 void OT_insert_left(void* operand, void* insertee);
 
 
 // - Adds an element to the right of an element in a linked list
 // - Adds an element to the right of an element in a linked list
-// - Takes in specific element of the list
+// - Takes in specific element of the list and an element to add to the list
 void OT_insert_right(void* operand, void* insertee);
 void OT_insert_right(void* operand, void* insertee);
 
 
 // - Frees entire list
 // - Frees entire list
 // - Takes in any element of the list
 // - Takes in any element of the list
 void OT_free(void* operand);
 void OT_free(void* operand);
 
 
+// - Runs `(*user_function)` for every element in the linked list
+// - Takes in any element of the list, a pointer that will be passed into the `user_function`, and `user_function`
+// - `user_function` must be in the form `void func(void* operand, void* user_pointer)`
+void OT_iterate(void* operand, void* user_pointer, void (*user_function)(void*, void*));
+
 #endif
 #endif

+ 56 - 0
src/launch program/main.c

@@ -1,3 +1,59 @@
+#include <stdio.h>
+#include "../headers/orangetree.h"
+
+typedef struct {
+    void* OT_next;
+    void* OT_prev;
+    char* string;
+} blahblahblah;
+
+void iteration_function(void* operand, void* user_pointer){
+    blahblahblah* oper = (blahblahblah*)(operand);
+    
+    printf(oper->string);
+}
+
 int main(){
 int main(){
+    blahblahblah a = {NULL, NULL, "Hello, "};
+    blahblahblah b = {NULL, NULL, "World!"};
+    blahblahblah c = {NULL, NULL, "Linked Lists "};
+    blahblahblah d = {NULL, NULL, "\b!"};
+    blahblahblah e = {NULL, NULL, "Laura the linked list lover says: "};
+
+    // see current state of linked list
+    OT_iterate(&a, NULL, iteration_function);
+    printf("\n");
+
+    // add B to end of linked list that contains A
+    OT_append(&a, &b);
+
+    // see current state of linked list
+    OT_iterate((void*)&a, NULL, iteration_function);
+    printf("\n");
+
+    // add C to the left of B in the linked list that contains B
+    OT_insert_left(&b, &c);
+
+    // see current state of linked list
+    OT_iterate((void*)&a, NULL, iteration_function);
+    printf("\n");
+
+    // remove B from the linked list that contains B
+    OT_drop(&b);
+
+    // add D to end of the linked list that contains A
+    OT_append(&a, &d);
+
+    // see current state of linked list
+    OT_iterate((void*)&a, NULL, iteration_function);
+    printf("\n");
+
+    // add E to the beginning of the linked list that contains A
+    OT_prepend(&a, &e);
+
+    // see current state of linked list
+    OT_iterate((void*)&a, NULL, iteration_function);
+    printf("\n");
+
     return 0;
     return 0;
 }
 }

+ 28 - 4
src/main/main.c

@@ -26,6 +26,16 @@ void OT_drop(void* operand){
 
 
     if (prev) prev->OT_next = (void*)next;
     if (prev) prev->OT_next = (void*)next;
     if (next) next->OT_prev = (void*)prev;
     if (next) next->OT_prev = (void*)prev;
+}
+
+void OT_erase(void* operand){
+    struct OT_operand* oper = (struct OT_operand*)operand;
+    
+    struct OT_operand* prev = (struct OT_operand*)oper->OT_prev;
+    struct OT_operand* next = (struct OT_operand*)oper->OT_next;
+
+    if (prev) prev->OT_next = (void*)next;
+    if (next) next->OT_prev = (void*)prev;
 
 
     free(operand);
     free(operand);
 }
 }
@@ -77,9 +87,23 @@ void OT_insert_right(void* operand, void* insertee){
 void OT_free(void* operand){
 void OT_free(void* operand){
     struct OT_operand* cur_oper = (struct OT_operand*)OT_get_first(operand);
     struct OT_operand* cur_oper = (struct OT_operand*)OT_get_first(operand);
 
 
-    while (cur_oper->OT_next){
-        cur_oper = (struct OT_operand*)(cur_oper->OT_next);
+    while (cur_oper){
+        struct OT_operand* next = (struct OT_operand*)(cur_oper->OT_next);
         
         
-        free(cur_oper->OT_prev);
+        free(cur_oper);
+        
+        cur_oper = next;
     }
     }
-}
+}
+
+void OT_iterate(void* operand, void* user_pointer, void (*user_function)(void*, void*)){
+    struct OT_operand* cur_oper = (struct OT_operand*)OT_get_first(operand);
+
+     while (cur_oper){
+        struct OT_operand* next = (struct OT_operand*)(cur_oper->OT_next);
+        
+        (*user_function)(cur_oper, user_pointer);
+
+        cur_oper = next;
+    }
+}