I have a small Linked list program which I created for just brushing up the concepts.
Following is my Node class
public class Node
{
private int element;
public int Element
{
get
{
return element;
}
set
{
element = value;
}
}
private Node next;
public Node Next
{
get
{
return next;
}
set
{
next = value;
}
}
public Node(int e, Node n)
{
Element = e;
Next = n;
}
}
Here is my linked list Class Logic.
public class CustomLinkedList
{
private int size;
private Node head;
private Node tail;
public int Size
{
get
{
return size;
}
private set
{
size = value;
}
}
public Node Head
{
get
{
return head;
}
private set
{
head = value;
}
}
public Node Tail
{
get
{
return tail;
}
private set
{
tail = value;
}
}
public CustomLinkedList()
{
this.size = 0;
this.head = null;
this.tail = null;
}
private bool isEmpty()
{
return Head == null;
}
public virtual void AddLast(int e)
{
Node newnode = new Node(e, null);
if(isEmpty())
this.head = newnode;
else
this.tail.Next = newnode;
this.tail = newnode;
this.size = this.size + 1;
}
public Node FindLast(int e)
{
Node node = this.head;
while(node!=null)
{
if(node.Element==e)
return node;
else
node = node.Next;
}
return null;
}
public virtual void AddFirst(int e)
{
Node newnode = new Node(e, null);
if(isEmpty())
{
this.head = newnode;
this.tail = newnode;
}
else
{
newnode.Next = this.head;
this.head = newnode;
}
this.size = this.size + 1;
}
public virtual void AddAfter(Node n,int e)
{
Node newnode = new Node(e, null);
newnode.Next = n.Next;
n.Next = newnode;
this.size = this.size + 1;
}
public virtual void RemoveFirst()
{
this.head = this.head.Next;
this.size = this.size - 1;
}
public virtual void RemoveLast()
{
Node n = this.head;
while(n!=null)
{
if(n.Next == this.tail)
{
n.Next = null;
this.tail = n;
this.size = this.size - 1;
}
n = n.Next;
}
}
public virtual void Remove(int e)
{
Node n = this.head;
while (n != null)
{
if(n.Next.Element == e)
{
n.Next = n.Next.Next;
this.size = this.size - 1;
break;
}
n = n.Next;
}
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
Node n = this.head;
while(n!=null)
{
sb.Append(n.Element.ToString());
n = n.Next;
if(n!=null)
sb.Append(" -> ");
}
return sb.ToString();
}
}
After creating this class I decided to apply colors to the list output after each and every operation.
I didn't wanted to implement the coloriing logic inside the class which process the linked list. So implemented the logic like this in a derived class using virtual and override methods.
public sealed class ColoredCustomLinkedList : CustomLinkedList
{
public override void AddLast(int e)
{
Console.ForegroundColor = ConsoleColor.Green;
base.AddLast(e);
}
public override void AddFirst(int e)
{
Console.ForegroundColor = ConsoleColor.Blue;
base.AddFirst(e);
}
public override void AddAfter(Node n, int e)
{
Console.ForegroundColor = ConsoleColor.Yellow;
base.AddAfter(n, e);
}
public override void RemoveFirst()
{
Console.ForegroundColor = ConsoleColor.Red;
base.RemoveFirst();
}
public override void RemoveLast()
{
Console.ForegroundColor = ConsoleColor.DarkRed;
base.RemoveLast();
}
public override void Remove(int e)
{
Console.ForegroundColor = ConsoleColor.DarkMagenta;
base.Remove(e);
}
}
I am getting the desired output as follows.
Is this the right way to do this? is there any better methods to suggest?
Node
class can be simplified like this in newer C# versions:record class Node(int Element, Node Next);
\$\endgroup\$