diff --git a/.gitignore b/.gitignore index ac1e8f7..0335b99 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -node_modules -*.log -.DS_Store +node_modules node_modules + *.log *.log + .DS_Store + \ No newline at end of file diff --git a/C#/DoubleLinked.cs b/C#/DoubleLinked.cs new file mode 100644 index 0000000..37e0b96 --- /dev/null +++ b/C#/DoubleLinked.cs @@ -0,0 +1,90 @@ +using System; + +namespace HowProgrammingWorks.CS.DoubleLinkedList +{ + class DoubleLinkedList + { + + public int Length { get; private set; } + + public Node First { get; private set; } + + public Node Last { get; private set; } + + public Node Push(T data) + { + var node = new Node(this, data); + node.Previous = this.Last; + + if (this.Length == 0) + this.First = node; + else + this.Last.Next = node; + + this.Last = node; + this.Length++; + + return node; + } + + public T Pop() + { + if (this.Length == 0) + return default(T); + + var node = this.Last; + + this.Length--; + this.Last = node.Previous; + + node.List = null; + node.Previous = null; + node.Next = null; + + return node.Data; + } + + } + + class Node + { + public DoubleLinkedList List { get; set; } + public Node Previous { get; set; } + public Node Next { get; set; } + public T Data { get; set; } + + public Node(DoubleLinkedList list, T data) + : this(list, null, null, data) + { + } + + protected Node(DoubleLinkedList list, Node previous, Node next, T data) + { + List = List; + Data = data; + Next = next; + Previous = previous; + } + } + + public class Example + { + public void Run() + { + var list = new DoubleLinkedList(); + + list.Push("road"); + list.Push("the"); + list.Push("across"); + list.Push("running"); + list.Push("was"); + list.Push("cat"); + list.Push("Black"); + + while (list.Length > 0) + Console.Write($"{list.Pop()} "); + + Console.WriteLine(); + } + } +} diff --git a/C#/HowProgrammingWorks.CS.csproj b/C#/HowProgrammingWorks.CS.csproj new file mode 100644 index 0000000..0c14923 --- /dev/null +++ b/C#/HowProgrammingWorks.CS.csproj @@ -0,0 +1,9 @@ + + + + Exe + netcoreapp2.1 + HowProgrammingWorks.CS + + + diff --git a/C#/Program.cs b/C#/Program.cs new file mode 100644 index 0000000..08b6fa5 --- /dev/null +++ b/C#/Program.cs @@ -0,0 +1,20 @@ +using System; + +namespace HowProgrammingWorks.CS +{ + class Program + { + static void Main(string[] args) + { + Console.Clear(); + + Console.WriteLine("Linked list"); + new SingleLinkedList.Example().Run(); + Console.WriteLine(); + + Console.WriteLine("Double linked list"); + new DoubleLinkedList.Example().Run(); + Console.WriteLine(); + } + } +} diff --git a/C#/SingleLinked.cs b/C#/SingleLinked.cs new file mode 100644 index 0000000..5b6b5c8 --- /dev/null +++ b/C#/SingleLinked.cs @@ -0,0 +1,50 @@ +using System; + +namespace HowProgrammingWorks.CS.SingleLinkedList +{ + class Node + { + public Node(T data) + : this(null, data) + { + } + + public Node(Node next, T data) + { + Next = next; + Data = data; + } + + public Node() + : this(null, default(T)) + { + } + + public Node Next { get; set; } + public T Data { get; set; } + } + + class Example + { + public void Run() + { + var n6 = new Node("road"); + var n5 = new Node(n6, "the"); + var n4 = new Node(n5, "across"); + var n3 = new Node(n4, "running"); + var n2 = new Node(n3, "was"); + var n1 = new Node(n2, "cat"); + var n0 = new Node(n1, "Black"); + + var currentNode = n0; + + while (currentNode != null) + { + Console.Write($"{currentNode.Data} "); + currentNode = currentNode.Next; + } + + Console.WriteLine(); + } + } +} diff --git a/F#/DoubleLinked.fs b/F#/DoubleLinked.fs new file mode 100644 index 0000000..5732bb7 --- /dev/null +++ b/F#/DoubleLinked.fs @@ -0,0 +1,84 @@ +module HowProgrammingWorks.FS.DoubleLinked + +open System + +type Node<'T> = { + mutable prev : Node<'T> option; + mutable next : Node<'T> option; + mutable data : 'T; +} + +type LinkedList<'T> = { + mutable first : Node<'T> option; + mutable last : Node<'T> option; + mutable count: int; +} + +let push (list : LinkedList<'T>, data: 'T) : Node<'T> = + match list.count with + | 0 -> + let node = {prev = None; next = None; data = data; } + list.first <- Some node; + list.last <- Some node; + list.count <- list.count + 1; + + node + | _ -> + let node = {prev = list.last; next = None; data = data; } + list.last.Value.next <- Some node + list.last <- Some node + list.count <- list.count + 1 + node + + +let pull (list : LinkedList<'T>) : 'T option = + match list.count with + | 0 -> + None; + | 1 -> + let node = list.last.Value; + list.last <- None; + list.first <- None; + list.count <- 0; + Some node.data; + |_-> + let node = list.last.Value; + list.last <- node.prev; + list.count <- list.count - 1; + Some node.data; + +let pullFromStart (list : LinkedList<'T>) : 'T option = + match list.count with + | 0 -> + None; + | 1 -> + let node = list.first.Value; + list.last <- None; + list.first <- None; + list.count <- 0; + Some node.data; + |_-> + let node = list.first.Value; + list.first <- node.next; + list.count <- list.count - 1; + Some node.data; + + +let Example = + + let rec write (list : LinkedList) = + match list.count with + | 0 -> + Console.WriteLine() + | _ -> + let value = pullFromStart list; //let value = pull list; + printf "%s " value.Value + write(list) + + let list : LinkedList = {first = None; last = None; count = 0;} + + let words = [ "Black"; "cat"; "was"; "running"; "across"; "the"; "road"; ] + words |> List.iter (fun w -> push(list, w) |> ignore ) + + write list + \ No newline at end of file diff --git a/F#/HowProgrammingWorks.FS.fsproj b/F#/HowProgrammingWorks.FS.fsproj new file mode 100644 index 0000000..c385491 --- /dev/null +++ b/F#/HowProgrammingWorks.FS.fsproj @@ -0,0 +1,15 @@ + + + + Exe + netcoreapp2.1 + HowProgrammingWorks + + + + + + + + + diff --git a/F#/Program.fs b/F#/Program.fs new file mode 100644 index 0000000..c4c0341 --- /dev/null +++ b/F#/Program.fs @@ -0,0 +1,18 @@ +// Learn more about F# at http://fsharp.org + +open System + +[] +let main argv = + Console.Clear() + + printf "Linked list: \n" + HowProgrammingWorks.FS.SingleLinked.Example + printf "\n" + + printf "Double linked list: \n" + HowProgrammingWorks.FS.DoubleLinked.Example + printf "\n" + + 0 // return an integer exit code + \ No newline at end of file diff --git a/F#/SingleLinked.fs b/F#/SingleLinked.fs new file mode 100644 index 0000000..f1095fb --- /dev/null +++ b/F#/SingleLinked.fs @@ -0,0 +1,35 @@ +module HowProgrammingWorks.FS.SingleLinked + +open System + +type Node<'T> = { + mutable next : Node<'T> option; + mutable data : 'T; +} + +let empty() = {next = None; data = None; } + +let pust node data = + Some { next = node; data = data } + + +let Example = + + let rec write (x : Node option) = + match x with + | None -> + Console.WriteLine() + | Some n -> + printf "%s " n.data + write n.next + + let n6 : Node = {next = None; data = "road";} + let n5 : Node = {next = Some n6; data = "the";} + let n4 : Node = {next = Some n5; data = "across";} + let n3 : Node = {next = Some n4; data = "running";} + let n2 : Node= {next = Some n3; data = "was";} + let n1 : Node= {next = Some n2; data = "cat";} + let n0 : Node= {next = Some n1; data = "Black";} + + write (Some n0) + \ No newline at end of file diff --git a/JavaScript/1-single-proto.js b/JavaScript/1-single-proto.js index b9c350c..e839f0d 100644 --- a/JavaScript/1-single-proto.js +++ b/JavaScript/1-single-proto.js @@ -1,15 +1,17 @@ 'use strict'; -function Node(prev, data) { - this.prev = prev; +function Node(next, data) { + this.next = next; this.data = data; } // Usage -const n1 = new Node(null, { name: 'first' }); -const n2 = new Node(n1, { name: 'second' }); -const n3 = new Node(n2, { name: 'third' }); +const n3 = new Node(null, { name: 'third' }); +const n2 = new Node(n3, { name: 'second' }); +const n1 = new Node(n2, { name: 'first' }); + + console.dir(n1); console.dir(n2); diff --git a/JavaScript/2-single-factory.js b/JavaScript/2-single-factory.js index ffc4d01..4ff2144 100644 --- a/JavaScript/2-single-factory.js +++ b/JavaScript/2-single-factory.js @@ -1,12 +1,14 @@ 'use strict'; -const node = (prev, data) => ({ prev, data }); +const node = (next, data) => ({ next, data }); // Usage -const n1 = node(null, { name: 'first' }); -const n2 = node(n1, { name: 'second' }); -const n3 = node(n2, { name: 'third' }); +const n3 = node(null, { name: 'third' }); +const n2 = node(n3, { name: 'second' }); +const n1 = node(n2, { name: 'first' }); + + console.dir(n1); console.dir(n2);