using System.Collections.Generic;
public class DynamicStack<T>
this.PreviousNode = null;
public Node(T element, Node previousNode)
this.PreviousNode = previousNode;
this.tail = new Node(item);
Node newNode = new Node(item, this.tail);
Node removedNode = this.tail;
throw new ArgumentNullException("The node is null");
this.tail = this.tail.PreviousNode;
return removedNode.Element;
Node highestNode = this.tail;
throw new ArgumentNullException("The node is null");
return highestNode.Element;
this.tail = this.tail.PreviousNode;
public class DynamicStackTest
public static void Main()
DynamicStack<string> pile = new DynamicStack<string>();
pile.Push("mineral water");
Console.WriteLine(pile.Pop());
Console.WriteLine(pile.Peek());
Console.WriteLine(pile.Count());
Console.WriteLine(pile.Count());
Console.WriteLine(pile.Count());
Console.WriteLine(pile.Peek());
Console.WriteLine(pile.Pop());
Console.WriteLine(pile.Pop());
Console.WriteLine(pile.Count());
Use singly linked list (similar to the list from the previous task, but only
with a field Previous, without a field Next).
DoublyLinkedList<string> shoppingList = new DoublyLinkedList<string>();
shoppingList.Add("Milk");
shoppingList.Remove("Milk");
shoppingList.Add("Honey");
shoppingList.Add("Olives");
shoppingList.Add("Water");
shoppingList[2] = "A lot of " + shoppingList[2];
shoppingList.Add("Fruits");
shoppingList.RemoveAt(0);
shoppingList.RemoveAt(2);
shoppingList.Add("Beer");
shoppingList.Remove(null);
Console.WriteLine("We need to buy:");
for (int i = 0; i < shoppingList.Count; i++)
Console.WriteLine(" - " + shoppingList[i]);
Console.WriteLine("Position of 'Beer' = {0}",
shoppingList.IndexOf("Beer"));
Console.WriteLine("Position of 'Water' = {0}",
shoppingList.IndexOf("Water"));
Console.WriteLine("Do we have to buy Bread? " +
shoppingList.Contains("Bread"));