I have this code to remove nodes in a linked list which have odd values. This works well but I want to fine tune it for better reading. Can you please suggest improvements?
public class MyLinkedList
{
public int data;
public MyLinkedList next;
public MyLinkedList(int v)
{
this.data = v;
}
//public void add(int i)
//{
// this.next = new MyLinkedList(i);
// this.next.next = null;
//}
public void printLL()
{
MyLinkedList mmm = this;
while (mmm != null)
{
Console.Write(mmm.data);
Console.Write("->");
mmm = mmm.next;
}
}
}
public static void deleteOdd(ref MyLinkedList mll)
{
MyLinkedList head, previous, curr;
head = mll;
while (head.data % 2 != 0)
{
head = head.next;
}
mll = head;
previous = head;
curr = previous.next;
while (curr != null)
{
if (curr.data % 2 != 0)
{
previous.next = curr.next;
curr = previous.next;
}
else
{
previous = previous.next;
curr = curr.next;
}
}
}
static void Main(string[] args)
{
MyLinkedList myll1 = new MyLinkedList(1); MyLinkedList myll2 = new MyLinkedList(3);
MyLinkedList myll3 = new MyLinkedList(4); MyLinkedList myll4 = new MyLinkedList(4);
MyLinkedList myll5 = new MyLinkedList(5); MyLinkedList myll6 = new MyLinkedList(6);
MyLinkedList myll7 = new MyLinkedList(5); MyLinkedList myll8 = new MyLinkedList(6);
MyLinkedList myll9 = new MyLinkedList(6); MyLinkedList myll10 = new MyLinkedList(9);
MyLinkedList myll11 = new MyLinkedList(11); MyLinkedList myll12 = new MyLinkedList(12);
myll1.next = myll2; myll2.next = myll3; myll3.next = myll4; myll4.next = myll5;
myll5.next = myll6; myll6.next = myll7; myll7.next = myll8; myll8.next = myll9;
myll9.next = myll10; myll10.next = myll11; myll11.next = myll12; myll12.next = myll12; myll12.next = null;
myll1.printLL();
Console.WriteLine();
deleteOdd(ref myll1);
Console.WriteLine();
myll1.printLL();
}
MyLinkedList mmm = this
Using nonsensical variable names complicates things. Calling itcurrentNode
or something similar would've helped tremendously. \$\endgroup\$