class CustomSingleLinkedList<T>
{
private CustomLinkedListItem<T> rootNode;
public int Count = 0;
public void Add(T value)
{
CustomLinkedListItem<T> linkedListItem = new CustomLinkedListItem<T>(value);
if (rootNode == null)
{
rootNode = linkedListItem;
++Count;
return;
}
CustomLinkedListItem<T> previousNode = rootNode;
while (previousNode.Reference != null)
{
previousNode = previousNode.Reference;
}
previousNode.Reference = linkedListItem;
++Count;
}
public void InsertAt(T value, int position)
{
if (position > Count)
{
throw new IndexOutOfRangeException();
}
int tempCount = 0;
CustomLinkedListItem<T> linkedListItem = new CustomLinkedListItem<T>(value);
CustomLinkedListItem<T> node = rootNode;
CustomLinkedListItem<T> tempnode = null;
while (tempCount != position && node.Reference != null)
{
tempnode = node;
node = node.Reference;
tempCount++;
}
if (tempnode != null)
{
tempnode.Reference = linkedListItem;
}
else
{
this.rootNode = linkedListItem;
}
linkedListItem.Reference = node;
}
public void ChangeValueAt(int position, T value)
{
int tempCount = 1;
CustomLinkedListItem<T> node = rootNode;
while (tempCount != position && node.Reference != null)
{
node = node.Reference;
tempCount++;
}
node.Item = value;
}
public T ValueAt(int position)
{
T value = default(T);
int tempCount = 1;
CustomLinkedListItem<T> node = rootNode;
while (tempCount != position && node.Reference != null)
{
node = node.Reference;
tempCount++;
}
value = node.Item;
return value;
}
public void ReverseLinkedList()
{
int tempCount = Count;
for (int i = 1, j = tempCount; i < j; i++, j--)
{
T Temp = ValueAt(i);
ChangeValueAt(i, ValueAt(j));
ChangeValueAt(j, Temp);
}
}
public T ReturnLastItem()
{
if (rootNode == null)
{
throw new InvalidOperationException("Root node is null");
}
if (rootNode.Reference == null)
{
return rootNode.Item;
}
CustomLinkedListItem<T> node = rootNode;
while (node.Reference != null)
{
node = node.Reference;
}
return node.Item;
}
public void RemoveLastItem()
{
if (rootNode == null)
{
throw new InvalidOperationException("Root node is null");
}
if (rootNode.Reference == null)
{
rootNode = null;
return;
}
CustomLinkedListItem<T> node = this.rootNode;
CustomLinkedListItem<T> tempnode = null;
while (node.Reference != null)
{
tempnode = node;
node = node.Reference;
}
tempnode.Reference = null;
}
public void RemoveFirstItem()
{
if (rootNode == null)
{
throw new InvalidOperationException("Root node is null");
}
if (rootNode.Reference != null)
{
rootNode = rootNode.Reference;
}
else
{
rootNode = null;
}
}
}
class CustomLinkedListItem<T>
{
public T Item { set; get; }
public CustomLinkedListItem<T> Reference { set; get; }
public CustomLinkedListItem(T item)
{
this.Item = item;
}
}
{
private CustomLinkedListItem<T> rootNode;
public int Count = 0;
public void Add(T value)
{
CustomLinkedListItem<T> linkedListItem = new CustomLinkedListItem<T>(value);
if (rootNode == null)
{
rootNode = linkedListItem;
++Count;
return;
}
CustomLinkedListItem<T> previousNode = rootNode;
while (previousNode.Reference != null)
{
previousNode = previousNode.Reference;
}
previousNode.Reference = linkedListItem;
++Count;
}
public void InsertAt(T value, int position)
{
if (position > Count)
{
throw new IndexOutOfRangeException();
}
int tempCount = 0;
CustomLinkedListItem<T> linkedListItem = new CustomLinkedListItem<T>(value);
CustomLinkedListItem<T> node = rootNode;
CustomLinkedListItem<T> tempnode = null;
while (tempCount != position && node.Reference != null)
{
tempnode = node;
node = node.Reference;
tempCount++;
}
if (tempnode != null)
{
tempnode.Reference = linkedListItem;
}
else
{
this.rootNode = linkedListItem;
}
linkedListItem.Reference = node;
}
public void ChangeValueAt(int position, T value)
{
int tempCount = 1;
CustomLinkedListItem<T> node = rootNode;
while (tempCount != position && node.Reference != null)
{
node = node.Reference;
tempCount++;
}
node.Item = value;
}
public T ValueAt(int position)
{
T value = default(T);
int tempCount = 1;
CustomLinkedListItem<T> node = rootNode;
while (tempCount != position && node.Reference != null)
{
node = node.Reference;
tempCount++;
}
value = node.Item;
return value;
}
public void ReverseLinkedList()
{
int tempCount = Count;
for (int i = 1, j = tempCount; i < j; i++, j--)
{
T Temp = ValueAt(i);
ChangeValueAt(i, ValueAt(j));
ChangeValueAt(j, Temp);
}
}
public T ReturnLastItem()
{
if (rootNode == null)
{
throw new InvalidOperationException("Root node is null");
}
if (rootNode.Reference == null)
{
return rootNode.Item;
}
CustomLinkedListItem<T> node = rootNode;
while (node.Reference != null)
{
node = node.Reference;
}
return node.Item;
}
public void RemoveLastItem()
{
if (rootNode == null)
{
throw new InvalidOperationException("Root node is null");
}
if (rootNode.Reference == null)
{
rootNode = null;
return;
}
CustomLinkedListItem<T> node = this.rootNode;
CustomLinkedListItem<T> tempnode = null;
while (node.Reference != null)
{
tempnode = node;
node = node.Reference;
}
tempnode.Reference = null;
}
public void RemoveFirstItem()
{
if (rootNode == null)
{
throw new InvalidOperationException("Root node is null");
}
if (rootNode.Reference != null)
{
rootNode = rootNode.Reference;
}
else
{
rootNode = null;
}
}
}
class CustomLinkedListItem<T>
{
public T Item { set; get; }
public CustomLinkedListItem<T> Reference { set; get; }
public CustomLinkedListItem(T item)
{
this.Item = item;
}
}
No comments:
Post a Comment