{
///
///
///
public class DBNode
{
public Object Element;
public DBNode nextLink;
public DBNode prevLink;
public DBNode()
{
Element = null;
nextLink = null;
prevLink = null;
}
public DBNode(Object theElement)
{
Element = theElement;
nextLink = null;
prevLink = null;
}
}
///
///
///
public class DoublyLinkedList
{
protected DBNode header;
public DoublyLinkedList()
{
header = new DBNode("header");
}
public DBNode Find(Object Item)
{
DBNode current = new DBNode();
current = header;
while (current.Element != Item)
{
current = current.nextLink;
}
return current;
}
public void Insert(Object newItem,Object after)
{
DBNode current = new DBNode();
DBNode newNode = new DBNode(newItem);
current = Find(after);
newNode.nextLink = current.nextLink;
newNode.prevLink = current;
current.nextLink = newNode;
}
public void Remove(Object n)
{
DBNode p = Find(n);
p.prevLink.nextLink = p.nextLink;
p.nextLink.prevLink = p.prevLink;
p.prevLink = null;
p.nextLink = null;
}
public DBNode FindLast()
{
DBNode current = new DBNode();
current = header;
while (current.nextLink != null)
{
current = current.nextLink;
}
return current;
}
public void PrintReverse()
{
DBNode current = new DBNode();
current = FindLast();
while (current.prevLink != null)
{
Console.WriteLine(current.Element);
current = current.prevLink;
}
}
public void PrintObverse()
{
DBNode current = new DBNode();
current = header;
while (current.nextLink != null)
{
Console.WriteLine(current.nextLink.Element);
current = current.nextLink;
}
}
}
class Program
{
static void Main(string[] args)
{
DoublyLinkedList myList = new DoublyLinkedList();
myList.Insert("kevin1", "header");
myList.Insert("kevin2","kevin1");
myList.Insert("kevin3", "kevin2");
myList.Insert("kevin4", "kevin3");
myList.Insert("kevin5", "kevin4");
myList.Remove("kevin3");
myList.PrintReverse();
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~");
myList.PrintObverse();
}
}
}


No comments:
Post a Comment