class CustomDoubleLinkedList<T>
{
private CustomDoubleLinkedListItem<T> RootNode;
public int Count { set; get; }
public void AddValue(T value)
{
CustomDoubleLinkedListItem<T> CustomDoubleLinkedListItemReference = new CustomDoubleLinkedListItem<T>(value);
if (RootNode == null)
{
RootNode = CustomDoubleLinkedListItemReference;
RootNode.PreviousNode = null;
Count++;
return;
}
CustomDoubleLinkedListItem<T> nextNodeReference= this.RootNode;
while (nextNodeReference.NextNode != null)
{
nextNodeReference = nextNodeReference.NextNode;
}
nextNodeReference.NextNode = CustomDoubleLinkedListItemReference;
CustomDoubleLinkedListItemReference.PreviousNode = nextNodeReference;
}
public void InsertAt(T value, uint position)
{
if (position > Count)
{
throw new InvalidOperationException("Position cannot be greater than link nodes");
}
int tempCount = 0;
CustomDoubleLinkedListItem<T> CustomDoubleLinkedListItemReference = new CustomDoubleLinkedListItem<T>(value);
CustomDoubleLinkedListItem<T> node = this.RootNode;
while (tempCount != position && node.NextNode != null)
{
node = node.NextNode;
}
if (node == this.RootNode)
{
CustomDoubleLinkedListItemReference.PreviousNode = null;
}
else
{
CustomDoubleLinkedListItemReference.PreviousNode = node.PreviousNode;
}
CustomDoubleLinkedListItemReference.NextNode = node;
}
public void ReverseList()
{
CustomDoubleLinkedListItem<T> node = this.RootNode.NextNode;
CustomDoubleLinkedListItem<T> previousNode;
CustomDoubleLinkedListItem<T> nextNode;
while (node != null)
{
nextNode = node.PreviousNode;
previousNode = node.NextNode;
node.PreviousNode = previousNode;
node.NextNode = nextNode;
//Handle the old root
if (previousNode == null)
{
previousNode = this.RootNode.NextNode;
this.RootNode.NextNode = null;
//Set the previous node
this.RootNode.PreviousNode = previousNode;
this.RootNode = node;
previousNode = null;
}
if (previousNode == this.RootNode)
{
node.PreviousNode = null;
}
node = previousNode;
}
}
public string DrawNode()
{
string output = String.Empty;
if (RootNode.NextNode == null)
{
return RootNode.Value.ToString();
}
output = RootNode.Value.ToString();
CustomDoubleLinkedListItem<T> nextNodeReference = this.RootNode.NextNode;
while (nextNodeReference.NextNode != null)
{
output = String.Concat(output,",", nextNodeReference.PreviousNode.Value.ToString(), "->", nextNodeReference.Value);
nextNodeReference = nextNodeReference.NextNode;
}
return output;
}
}
class CustomDoubleLinkedListItem<T>
{
public CustomDoubleLinkedListItem<T> PreviousNode { set; get; }
public CustomDoubleLinkedListItem<T> NextNode { set; get; }
public T Value { set; get; }
public CustomDoubleLinkedListItem(T customDoubleLinkedListItemValue)
{
this.Value = customDoubleLinkedListItemValue;
}
}
{
private CustomDoubleLinkedListItem<T> RootNode;
public int Count { set; get; }
public void AddValue(T value)
{
CustomDoubleLinkedListItem<T> CustomDoubleLinkedListItemReference = new CustomDoubleLinkedListItem<T>(value);
if (RootNode == null)
{
RootNode = CustomDoubleLinkedListItemReference;
RootNode.PreviousNode = null;
Count++;
return;
}
CustomDoubleLinkedListItem<T> nextNodeReference= this.RootNode;
while (nextNodeReference.NextNode != null)
{
nextNodeReference = nextNodeReference.NextNode;
}
nextNodeReference.NextNode = CustomDoubleLinkedListItemReference;
CustomDoubleLinkedListItemReference.PreviousNode = nextNodeReference;
}
public void InsertAt(T value, uint position)
{
if (position > Count)
{
throw new InvalidOperationException("Position cannot be greater than link nodes");
}
int tempCount = 0;
CustomDoubleLinkedListItem<T> CustomDoubleLinkedListItemReference = new CustomDoubleLinkedListItem<T>(value);
CustomDoubleLinkedListItem<T> node = this.RootNode;
while (tempCount != position && node.NextNode != null)
{
node = node.NextNode;
}
if (node == this.RootNode)
{
CustomDoubleLinkedListItemReference.PreviousNode = null;
}
else
{
CustomDoubleLinkedListItemReference.PreviousNode = node.PreviousNode;
}
CustomDoubleLinkedListItemReference.NextNode = node;
}
public void ReverseList()
{
CustomDoubleLinkedListItem<T> node = this.RootNode.NextNode;
CustomDoubleLinkedListItem<T> previousNode;
CustomDoubleLinkedListItem<T> nextNode;
while (node != null)
{
nextNode = node.PreviousNode;
previousNode = node.NextNode;
node.PreviousNode = previousNode;
node.NextNode = nextNode;
//Handle the old root
if (previousNode == null)
{
previousNode = this.RootNode.NextNode;
this.RootNode.NextNode = null;
//Set the previous node
this.RootNode.PreviousNode = previousNode;
this.RootNode = node;
previousNode = null;
}
if (previousNode == this.RootNode)
{
node.PreviousNode = null;
}
node = previousNode;
}
}
public string DrawNode()
{
string output = String.Empty;
if (RootNode.NextNode == null)
{
return RootNode.Value.ToString();
}
output = RootNode.Value.ToString();
CustomDoubleLinkedListItem<T> nextNodeReference = this.RootNode.NextNode;
while (nextNodeReference.NextNode != null)
{
output = String.Concat(output,",", nextNodeReference.PreviousNode.Value.ToString(), "->", nextNodeReference.Value);
nextNodeReference = nextNodeReference.NextNode;
}
return output;
}
}
class CustomDoubleLinkedListItem<T>
{
public CustomDoubleLinkedListItem<T> PreviousNode { set; get; }
public CustomDoubleLinkedListItem<T> NextNode { set; get; }
public T Value { set; get; }
public CustomDoubleLinkedListItem(T customDoubleLinkedListItemValue)
{
this.Value = customDoubleLinkedListItemValue;
}
}
No comments:
Post a Comment