public Node(object element, Node prevNode)
public Node(object element)
public void Add(object item)
Node newNode = new Node(item, tail);
public object Remove(int index)
if (index >= count || index < 0)
throw new ArgumentOutOfRangeException("Invalid index: " + index);
while (currentIndex < index)
currentNode = currentNode.Next;
else if (prevNode == null)
prevNode.Next = currentNode.Next;
while (lastElement.Next != null)
lastElement = lastElement.Next;
return currentNode.Element;
public int Remove(object item)
while (currentNode != null)
if ((currentNode.Element != null &&
currentNode.Element.Equals(item)) ||
(currentNode.Element == null) && (item == null))
currentNode = currentNode.Next;
else if (prevNode == null)
prevNode.Next = currentNode.Next;
while (lastElement.Next != null)
lastElement = lastElement.Next;
public int IndexOf(object item)
if ((current.Element != null &&
current.Element == item) ||
(current.Element == null) && (item == null))
public bool Contains(object item)
int index = IndexOf(item);
bool found = (index != -1);
public object this[int index]
if (index >= count || index < 0)
throw new ArgumentOutOfRangeException("Invalid index: " + index);
Node currentNode = this.head;
for (int i = 0; i < index; i++)
currentNode = currentNode.Next;
return currentNode.Element;
if (index >= count || index < 0)
throw new ArgumentOutOfRangeException("Invalid index: " + index);
Node currentNode = this.head;
for (int i = 0; i < index; i++)
currentNode = currentNode.Next;
currentNode.Element = value;
public static void Main()
DynamicList shoppingList = new DynamicList();
shoppingList.Add("Milk");
shoppingList.Add("Honey");
shoppingList.Add("Olives");
shoppingList.Add("Beer");
shoppingList.Remove("Olives");
Console.WriteLine("We need to buy:");
for (int i = 0; i < shoppingList.Count; i++)
Console.WriteLine(shoppingList[i]);
Console.WriteLine("Do we have to buy Bread? " +
shoppingList.Contains("Bread"));