diff --git a/002LinkedList/.gitignore b/002LinkedList/.gitignore new file mode 100644 index 0000000..3c0160d --- /dev/null +++ b/002LinkedList/.gitignore @@ -0,0 +1,2 @@ +build/ +out/ diff --git a/002LinkedList/LICENSE b/002LinkedList/LICENSE new file mode 100644 index 0000000..e69de29 diff --git a/002LinkedList/README.md b/002LinkedList/README.md new file mode 100644 index 0000000..e69de29 diff --git a/002LinkedList/docs/.gitkeep b/002LinkedList/docs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/002LinkedList/lib/.gitkeep b/002LinkedList/lib/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/002LinkedList/project.json b/002LinkedList/project.json new file mode 100644 index 0000000..0856ba2 --- /dev/null +++ b/002LinkedList/project.json @@ -0,0 +1,45 @@ +{ + // Language version of C3. + "langrev": "1", + // Warnings used for all targets. + "warnings": [ "no-unused" ], + // Directories where C3 library files may be found. + "dependency-search-paths": [ "lib" ], + // Libraries to use for all targets. + "dependencies": [ ], + // Authors, optionally with email. + "authors": [ "John Doe " ], + // Version using semantic versioning. + "version": "0.1.0", + // Sources compiled for all targets. + "sources": [ "src/**" ], + // Test sources compiled for all targets. + "test-sources": [ "test/**" ], + // C sources if the project also compiles C sources + // relative to the project file. + // "c-sources": [ "csource/**" ], + // Include directories for C sources relative to the project file. + // "c-include-dirs": [ "csource/include" ], + // Build location, relative to project file. + "build-dir": "build", + // Output location, relative to project file. + "output": "build", + // Architecture and OS target. + // You can use 'c3c --list-targets' to list all valid targets. + // "target": "windows-x64", + // Targets. + "targets": { + "linkedlist": { + // Executable or library. + "type": "executable", + // Additional libraries, sources + // and overrides of global settings here. + }, + }, + // Global settings. + // CPU name, used for optimizations in the LLVM backend. + "cpu": "generic", + // Optimization: "O0", "O1", "O2", "O3", "O4", "O5", "Os", "Oz". + "opt": "O0" + // See resources/examples/project_all_settings.json and 'c3c --list-project-properties' to see more properties. +} \ No newline at end of file diff --git a/002LinkedList/resources/.gitkeep b/002LinkedList/resources/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/002LinkedList/scripts/.gitkeep b/002LinkedList/scripts/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/002LinkedList/src/.gitkeep b/002LinkedList/src/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/002LinkedList/src/main.c3 b/002LinkedList/src/main.c3 new file mode 100644 index 0000000..2f608fb --- /dev/null +++ b/002LinkedList/src/main.c3 @@ -0,0 +1,124 @@ +module linkedlist; +import std::io; + +struct Vector2 { + int x; + int y; +} + +struct Node { + // Node.x + //struct { + // int x; + //} + Vector2 *vec; + Node *next; +} + +fn Node* init(Vector2 *vec) { + + Node *node = alloc::alloc(mem, Node); + node.vec = vec; + node.next = null; + return node; +} + + +// &self == Node *self +fn void Node.append(&self, Vector2 *v2) { + + Node *newNode = init(v2); + + Node *prev; + Node *current = self; + while (current != null) { + prev = current; + current = current.next; + } + + prev.next = newNode; + newNode.next = null; + +} + +fn void Node.insertAfter(&self, Vector2 *key, Vector2 *v2) { + Node *newNode = init(v2); + newNode.vec = v2; + + Node *current = self; + + while (current != null) { + if (current.vec == key) { + break; + } + + current = current.next; + } + + newNode.next = current.next; + current.next = newNode; +} + + +fn void Node.deleteNode(&self, Vector2 *key) { + Node *current = self; + Node *prev; + + while (current != null) { + if (current.vec == key) { + break; + } + prev = current; + current = current.next; + + } + + prev.next = current.next; + alloc::free(mem, current); +} + + +fn void Node.printList(&self) { + Node *current = self; + + io::printf("head-> "); + while (current != null) { + io::printf("(%d, %d) -> ", current.vec.x, current.vec.y); + current = current.next; + } + + io::printfn("tail"); +} + +fn uint Node.count(&self) { + + uint count = 0; + Node *current = self; + + while (current != null) { + count++; + current = current.next; + } + return count; +} + +fn int main(String[] args) { + Vector2 v1 = { .x = 1, .y = 2 }; + Vector2 v2 = { .x = 3, .y = 4 }; + Vector2 v3 = { .x = 5, .y = 6 }; + Vector2 v4 = { .x = 7, .y = 8 }; + Node* list = init(&v1); + list.append(&v2); + list.append(&v3); + list.printList(); + io::printfn("count: %d", list.count()); + list.insertAfter(&v2, &v4); + list.printList(); + io::printfn("count: %d", list.count()); + + list.deleteNode(&v4); + list.printList(); + io::printfn("count: %d", list.count()); + + return 0; +} diff --git a/002LinkedList/test/.gitkeep b/002LinkedList/test/.gitkeep new file mode 100644 index 0000000..e69de29