Skip to content

Rudenko Oleksandr, IT-32 #3

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions CSharp/Double/DLL.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
public class DLL<T> : System.Collections.Generic.IEnumerable<T>
{
private ListNode<T> _head;
private ListNode<T> _tail;

public int Count { get; private set; }
/// <summary>
/// Adding new node to tail of list
/// </summary>
/// <param name="value">Value for new node</param>
public void Add(T value)
{
ListNode<T> _tmpNode = new ListNode<T>(value);
if (_head == null)
{
_head = _tmpNode;
_tail = _tmpNode;
}
else
{
_tail.Next = _tmpNode;
_tail.Next.Previous = _tail;
_tail = _tmpNode;
}
Count++;
}

/// <summary>
/// Make your list clear
/// </summary>
public void Clear()
{
_head = null;
_tail = null;
Count = 0;
}

/// <summary>
/// Removing node by value from list
/// </summary>
/// <param name="value">Value of node</param>
/// <returns></returns>
public bool Remove(T value)
{
ListNode<T> previous = null;
ListNode<T> 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<T>)this).GetEnumerator();
}

public IEnumerator<T> GetEnumerator()
{
ListNode<T> current = _head;

while (current != null)
{
yield return current.Value;
current = current.Next;
}
}
}
11 changes: 11 additions & 0 deletions CSharp/Double/ListNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class ListNode<T>
{
public T Value { get; set; }
public ListNode<T> Previous { get; internal set; }
public ListNode<T> Next { get; internal set; }

public ListNode(T value)
{
Value = value;
}
}
46 changes: 46 additions & 0 deletions CSharp/Double/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Program
{
static void Main(string[] args)
{
DLL<int> instance = new DLL<int> { };

#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<int> words, string test)
{
Console.WriteLine(test);
foreach (int word in words)
{
Console.Write(word + " ");
}
Console.WriteLine();
Console.WriteLine();
}
}
16 changes: 16 additions & 0 deletions CSharp/Single/ListNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class ListNode<T>
{
public T Value { get; set; }
public ListNode<T> Next { get; set; }

public ListNode(T value)
{
Value = value;
}

public ListNode(T value, ListNode<T> next)
{
Value = value;
Next = next;
}
}
17 changes: 17 additions & 0 deletions CSharp/Single/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Program
{
static void Main(string[] args)
{
ListNode<int> first = new ListNode<int>(1);
ListNode<int> second = new ListNode<int>(2);
first.Next = second;
ListNode<int> zero = new ListNode<int>(0, first);

Console.WriteLine(zero.Value);
Console.WriteLine(zero.Next.Value);
Console.WriteLine(zero.Next.Next.Value);

Console.ReadKey();
}
}