Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added C# and F# examples #22

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
*.log
.DS_Store
node_modules node_modules
*.log *.log
.DS_Store

90 changes: 90 additions & 0 deletions C#/DoubleLinked.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;

namespace HowProgrammingWorks.CS.DoubleLinkedList
{
class DoubleLinkedList<T>
{

public int Length { get; private set; }

public Node<T> First { get; private set; }

public Node<T> Last { get; private set; }

public Node<T> Push(T data)
{
var node = new Node<T>(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<T>
{
public DoubleLinkedList<T> List { get; set; }
public Node<T> Previous { get; set; }
public Node<T> Next { get; set; }
public T Data { get; set; }

public Node(DoubleLinkedList<T> list, T data)
: this(list, null, null, data)
{
}

protected Node(DoubleLinkedList<T> list, Node<T> previous, Node<T> next, T data)
{
List = List;
Data = data;
Next = next;
Previous = previous;
}
}

public class Example
{
public void Run()
{
var list = new DoubleLinkedList<string>();

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();
}
}
}
9 changes: 9 additions & 0 deletions C#/HowProgrammingWorks.CS.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace>HowProgrammingWorks.CS</RootNamespace>
</PropertyGroup>

</Project>
20 changes: 20 additions & 0 deletions C#/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
50 changes: 50 additions & 0 deletions C#/SingleLinked.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;

namespace HowProgrammingWorks.CS.SingleLinkedList
{
class Node<T>
{
public Node(T data)
: this(null, data)
{
}

public Node(Node<T> next, T data)
{
Next = next;
Data = data;
}

public Node()
: this(null, default(T))
{
}

public Node<T> Next { get; set; }
public T Data { get; set; }
}

class Example
{
public void Run()
{
var n6 = new Node<string>("road");
var n5 = new Node<string>(n6, "the");
var n4 = new Node<string>(n5, "across");
var n3 = new Node<string>(n4, "running");
var n2 = new Node<string>(n3, "was");
var n1 = new Node<string>(n2, "cat");
var n0 = new Node<string>(n1, "Black");

var currentNode = n0;

while (currentNode != null)
{
Console.Write($"{currentNode.Data} ");
currentNode = currentNode.Next;
}

Console.WriteLine();
}
}
}
84 changes: 84 additions & 0 deletions F#/DoubleLinked.fs
Original file line number Diff line number Diff line change
@@ -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<string>) =
match list.count with
| 0 ->
Console.WriteLine()
| _ ->
let value = pullFromStart list; //let value = pull list;
printf "%s " value.Value
write(list)

let list : LinkedList<string> = {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

15 changes: 15 additions & 0 deletions F#/HowProgrammingWorks.FS.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace>HowProgrammingWorks</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Compile Include="SingleLinked.fs" />
<Compile Include="DoubleLinked.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

</Project>
18 changes: 18 additions & 0 deletions F#/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Learn more about F# at http://fsharp.org

open System

[<EntryPoint>]
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

35 changes: 35 additions & 0 deletions F#/SingleLinked.fs
Original file line number Diff line number Diff line change
@@ -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<string> option) =
match x with
| None ->
Console.WriteLine()
| Some n ->
printf "%s " n.data
write n.next

let n6 : Node<string> = {next = None; data = "road";}
let n5 : Node<string> = {next = Some n6; data = "the";}
let n4 : Node<string> = {next = Some n5; data = "across";}
let n3 : Node<string> = {next = Some n4; data = "running";}
let n2 : Node<string>= {next = Some n3; data = "was";}
let n1 : Node<string>= {next = Some n2; data = "cat";}
let n0 : Node<string>= {next = Some n1; data = "Black";}

write (Some n0)

12 changes: 7 additions & 5 deletions JavaScript/1-single-proto.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
10 changes: 6 additions & 4 deletions JavaScript/2-single-factory.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down