diff --git a/CSharp/Double/DLL.cs b/CSharp/Double/DLL.cs new file mode 100644 index 0000000..9947f5a --- /dev/null +++ b/CSharp/Double/DLL.cs @@ -0,0 +1,97 @@ +public class DLL : System.Collections.Generic.IEnumerable + { + private ListNode _head; + private ListNode _tail; + + public int Count { get; private set; } + /// + /// Adding new node to tail of list + /// + /// Value for new node + public void Add(T value) + { + ListNode _tmpNode = new ListNode(value); + if (_head == null) + { + _head = _tmpNode; + _tail = _tmpNode; + } + else + { + _tail.Next = _tmpNode; + _tail.Next.Previous = _tail; + _tail = _tmpNode; + } + Count++; + } + + /// + /// Make your list clear + /// + public void Clear() + { + _head = null; + _tail = null; + Count = 0; + } + + /// + /// Removing node by value from list + /// + /// Value of node + /// + public bool Remove(T value) + { + ListNode previous = null; + ListNode current = _head; + + while (current != null) + { + if (current.Value.Equals(value)) + { + + if (previous != null) + { + previous.Next = current.Next; + current.Next.Previous = current.Previous; + + if (current.Next == null) + { + _tail = previous; + } + } + else + { + _head = _head.Next; + + if (_head == null) + { + _tail = null; + } + } + + Count--; + return true; + } + previous = current; + current = current.Next; + } + return false; + } + + IEnumerator IEnumerable.GetEnumerator() + { + return ((IEnumerable)this).GetEnumerator(); + } + + public IEnumerator GetEnumerator() + { + ListNode current = _head; + + while (current != null) + { + yield return current.Value; + current = current.Next; + } + } + } diff --git a/CSharp/Double/ListNode.cs b/CSharp/Double/ListNode.cs new file mode 100644 index 0000000..53215db --- /dev/null +++ b/CSharp/Double/ListNode.cs @@ -0,0 +1,11 @@ +public class ListNode + { + public T Value { get; set; } + public ListNode Previous { get; internal set; } + public ListNode Next { get; internal set; } + + public ListNode(T value) + { + Value = value; + } + } diff --git a/CSharp/Double/Program.cs b/CSharp/Double/Program.cs new file mode 100644 index 0000000..04a5ceb --- /dev/null +++ b/CSharp/Double/Program.cs @@ -0,0 +1,46 @@ +class Program + { + static void Main(string[] args) + { + DLL instance = new DLL { }; + + #region Adding items + + instance.Add(12); + instance.Add(15); + instance.Add(20); + instance.Add(25); + + Display(instance, "List"); + + #endregion + + #region Removing item + + instance.Remove(20); + + Display(instance, "20 was removed"); + + #endregion + + #region Clearing list + + instance.Clear(); + Display(instance, "List is cleared"); + + #endregion + + Console.ReadKey(); + } + + public static void Display(DLL words, string test) + { + Console.WriteLine(test); + foreach (int word in words) + { + Console.Write(word + " "); + } + Console.WriteLine(); + Console.WriteLine(); + } + } diff --git a/CSharp/Single/ListNode.cs b/CSharp/Single/ListNode.cs new file mode 100644 index 0000000..6146300 --- /dev/null +++ b/CSharp/Single/ListNode.cs @@ -0,0 +1,16 @@ +public class ListNode + { + public T Value { get; set; } + public ListNode Next { get; set; } + + public ListNode(T value) + { + Value = value; + } + + public ListNode(T value, ListNode next) + { + Value = value; + Next = next; + } + } diff --git a/CSharp/Single/Program.cs b/CSharp/Single/Program.cs new file mode 100644 index 0000000..ba55abc --- /dev/null +++ b/CSharp/Single/Program.cs @@ -0,0 +1,17 @@ +class Program + { + static void Main(string[] args) + { + ListNode first = new ListNode(1); + ListNode second = new ListNode(2); + first.Next = second; + ListNode zero = new ListNode(0, first); + + Console.WriteLine(zero.Value); + Console.WriteLine(zero.Next.Value); + Console.WriteLine(zero.Next.Next.Value); + + Console.ReadKey(); + } + } +