using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LD_24.Code
{
///
/// Stores multiple products in a linked list
///
public class ProductList : IEnumerable
{
class ProductNode
{
public Product Data { get; set; }
public ProductNode Next { get; set; }
public ProductNode(Product data = null, ProductNode next = null)
{
Data = data;
Next = next;
}
}
private ProductNode head;
private ProductNode tail;
///
/// Append a value to the end of the linked list
///
///
public void AddToEnd(Product product)
{
ProductNode node = new ProductNode(product);
if (tail != null && head != null)
{
tail.Next = node;
tail = node;
}
else
{
tail = node;
head = node;
}
}
///
/// Inserts a value to the start of the linked list
///
///
public void AddToStart(Product product)
{
ProductNode node = new ProductNode(product);
if (tail != null && head != null)
{
node.Next = head;
head = node;
}
else
{
tail = node;
head = node;
}
}
///
/// Get the number of values stored in the linked list
///
/// A count
public int Count()
{
int count = 0;
ProductNode current = head;
while (current != null)
{
current = current.Next;
count++;
}
return count;
}
public override string ToString()
{
return String.Format("ProductList{ Count = '{0}' }", Count());
}
public IEnumerator GetEnumerator()
{
ProductNode current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}