From 2cad573a2c68062b4498519e8b50cdf714445b15 Mon Sep 17 00:00:00 2001 From: Maks Melnik Date: Wed, 31 May 2017 22:29:59 +0300 Subject: [PATCH 1/5] Added CSharp examples --- CSharp/DoubleLinkedList.cs | 52 ++++++++++++++++++++++++++++++++++++++ CSharp/SingleLinkedList.cs | 45 +++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 CSharp/DoubleLinkedList.cs create mode 100644 CSharp/SingleLinkedList.cs diff --git a/CSharp/DoubleLinkedList.cs b/CSharp/DoubleLinkedList.cs new file mode 100644 index 0000000..3db4fc0 --- /dev/null +++ b/CSharp/DoubleLinkedList.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LinkedList +{ + class DoubleLinkedList + { + private Node last = null; + + private class Node + { + public T item; + public Node next; + public Node previous; + } + + public bool isEmpty() + { + return last == null; + } + + public void addNode(T item) + { + Node oldLast = last; + last = new Node(); + last.item = item; + last.previous = oldLast; + if (oldLast != null) + { + oldLast.next = last; + } + } + + public T printList() + { + T item = default(T); + if (!isEmpty()) + { + item = last.item; + last = last.previous; + if (last != null) + { + last.next = null; + } + } + return item; + } + } +} diff --git a/CSharp/SingleLinkedList.cs b/CSharp/SingleLinkedList.cs new file mode 100644 index 0000000..0fec0fc --- /dev/null +++ b/CSharp/SingleLinkedList.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LinkedList +{ + class SingleLinkedList + { + private Node last = null; + + private class Node + { + public T item; + public Node previous; + } + + public bool isEmpty() + { + return last == null; + } + + public void addNode(T item) + { + Node oldLast = last; + last = new Node(); + last.item = item; + last.previous = oldLast; + } + + public List printList() + { + List res = new List(); + T item = default(T); + while (!isEmpty()) + { + item = last.item; + last = last.previous; + res.Add(item); + } + return res; + } + } +} From d022b864ee4d752753e3af451c16b131a1bb8190 Mon Sep 17 00:00:00 2001 From: Maks Melnik Date: Wed, 31 May 2017 22:37:56 +0300 Subject: [PATCH 2/5] Add files via upload --- CSharp/DoubleLinkedList.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/CSharp/DoubleLinkedList.cs b/CSharp/DoubleLinkedList.cs index 3db4fc0..c2be0cb 100644 --- a/CSharp/DoubleLinkedList.cs +++ b/CSharp/DoubleLinkedList.cs @@ -34,19 +34,17 @@ public void addNode(T item) } } - public T printList() + public List printList() { + List res = new List(); T item = default(T); - if (!isEmpty()) + while (!isEmpty()) { item = last.item; last = last.previous; - if (last != null) - { - last.next = null; - } + res.Add(item); } - return item; + return res; } } } From ccabd7eecc84c8cae4f6432f2bd8e5d93a54defb Mon Sep 17 00:00:00 2001 From: Maks Melnik Date: Thu, 1 Jun 2017 20:40:08 +0300 Subject: [PATCH 3/5] Renamed files to keep consistency --- CSharp/{SingleLinkedList.cs => 1-single.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename CSharp/{SingleLinkedList.cs => 1-single.cs} (100%) diff --git a/CSharp/SingleLinkedList.cs b/CSharp/1-single.cs similarity index 100% rename from CSharp/SingleLinkedList.cs rename to CSharp/1-single.cs From de85639eeed0c5b7c8ed2f926000593d3cb65a10 Mon Sep 17 00:00:00 2001 From: Maks Melnik Date: Thu, 1 Jun 2017 20:40:19 +0300 Subject: [PATCH 4/5] Renamed files to keep consistency --- CSharp/{DoubleLinkedList.cs => 2-doubly.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename CSharp/{DoubleLinkedList.cs => 2-doubly.cs} (100%) diff --git a/CSharp/DoubleLinkedList.cs b/CSharp/2-doubly.cs similarity index 100% rename from CSharp/DoubleLinkedList.cs rename to CSharp/2-doubly.cs From 9322f12c69185a8372e9fd9772aee6049ad90140 Mon Sep 17 00:00:00 2001 From: Maks Melnik Date: Fri, 2 Jun 2017 13:19:40 +0300 Subject: [PATCH 5/5] Code update 1. Changed access modifier of Node fields 2. Updated return functions --- CSharp/1-single.cs | 8 +++++--- CSharp/2-doubly.cs | 10 ++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/CSharp/1-single.cs b/CSharp/1-single.cs index 0fec0fc..51f9292 100644 --- a/CSharp/1-single.cs +++ b/CSharp/1-single.cs @@ -12,8 +12,8 @@ class SingleLinkedList private class Node { - public T item; - public Node previous; + internal T item; + internal Node previous; } public bool isEmpty() @@ -29,9 +29,10 @@ public void addNode(T item) last.previous = oldLast; } - public List printList() + public List returnList() { List res = new List(); + Node temp = last; T item = default(T); while (!isEmpty()) { @@ -39,6 +40,7 @@ public List printList() last = last.previous; res.Add(item); } + last = temp; return res; } } diff --git a/CSharp/2-doubly.cs b/CSharp/2-doubly.cs index c2be0cb..52c4b41 100644 --- a/CSharp/2-doubly.cs +++ b/CSharp/2-doubly.cs @@ -12,9 +12,9 @@ class DoubleLinkedList private class Node { - public T item; - public Node next; - public Node previous; + internal T item; + internal Node next; + internal Node previous; } public bool isEmpty() @@ -34,9 +34,10 @@ public void addNode(T item) } } - public List printList() + public List returnList() { List res = new List(); + Node temp = last; T item = default(T); while (!isEmpty()) { @@ -44,6 +45,7 @@ public List printList() last = last.previous; res.Add(item); } + last = temp; return res; } }