adding linked list source code
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
build/
|
||||||
|
out/
|
||||||
@@ -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 <john.doe@example.com>" ],
|
||||||
|
// 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.
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user