Hi all,
I am part way through implementing my own Linked List class (only stores Strings for now) to better understand how they work. I have completed the major portions of it; Add, Delete, Insert etc. and was just wondering if I could get some feedback on the code thus far.
private StringNode list;
public MyStringList()
{
list = null; // empty by default;
}
public void InsertNode(string stringIn, uint index)
{
// Check that user is not trying to insert outside the valid
// range of nodes.
if (IndexOutOfUpperBounds(index))
{
return;
}
// create a string node to insert into the list
StringNode newNode = new StringNode(stringIn);
StringNode current;
StringNode temp;
if (index == 0)
{
// store the list head.
temp = list;
// set the new node as the new list head
list = newNode;
// reconnect the old list head to the new list head.
list.next = temp;
return;
}
// temp node that is a reference to the beginning node
current = list;
// loop to the position of the node at index
// because of the way that current is initialized, we can
// skip index zero.
for (int i = 1; i < index; i++)
{
// check that there is another node to process.
if (current.next != null)
{
current = current.next;
}
}
// store a reference to the next node (the one at the index we desire) so as to preserve it
temp = current.next;
// set the current.next to point to the location of the new node
// and set the new nodes next to point to that of the old
current.next = newNode;
newNode.next = temp;
}
public bool DeleteNode(uint index)
{
if (IndexOutOfUpperBounds(index))
{
return false;
}
// temp node representing the current node in the list.
StringNode current = list;
// temp node representing the previous node in the list.
StringNode previous = null;
// if the user has searched for a node that is not the first in the list
if (index > 0)
{
// loop from 0 to the index position.
for (int i = 0; i < index; i++)
{
if (current != null)
{
previous = current;
current = current.next;
}
}
}
// need conditions to assure that the predecessor of a node,
// removed from the end will point to null
// a check to see if we are at the end of the list.
if ((current.next == null) && (current != list))
{
// make the very last node null, so it will be removed by
// garbage collection
previous.next = null;
current = null;
}
// condition that a node removed from the middle will link the two
// nodes that surround it, properly
else if ((current.next != null) && (current != list))
{
// change the previous node to link to the node up ahead.
previous.next = current.next;
current = null;
}
// condition that the successor of a node removed from the front
// will properly set the new list head.
else
{
// check that the list head is not the only node
if (current.next != null)
{
list = current.next;
}
else
{
list = null;
}
}
// reduce number of nodes by 1, if we have got to this point,
// there is no need to check if we are allowed to decrement.
this.Count--;
return true;
}
I have not included the entire implementation, for simplicity's sake. I would like to draw your attention to the Insert and Delete methods. They work in all the tests I have performed. I feel like they could be streamlined more. If you need anymore information, feel free to ask and I shall do my best to explain.
Many thanks,
Stitchs.