105 lines
2.7 KiB
C#
105 lines
2.7 KiB
C#
using StgSharp.Graphics;
|
|
using StgSharp.Logic;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace StgSharp
|
|
{
|
|
public class LinkedList<T>:INode<LinkedList<T>>,IEnumerable where T: INode<T>
|
|
{
|
|
internal readonly ContainerNode<T> _heading;
|
|
private ContainerNode<T> _cache;
|
|
private bool isEmpty;
|
|
|
|
public LinkedList()
|
|
{
|
|
_heading = new ContainerNode<T>(null);
|
|
isEmpty = true;
|
|
|
|
#if DEBUG
|
|
Console.WriteLine("Linked list init ok");
|
|
#endif
|
|
}
|
|
|
|
public T First
|
|
{
|
|
get { return _heading._next._value; }
|
|
private set { _heading._next = value.ID; }
|
|
}
|
|
|
|
public T Tail
|
|
{
|
|
get { return _heading._previous._value; }
|
|
private set { _heading._previous = value.ID; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add an item to the tail of linked list.
|
|
/// </summary>
|
|
/// <param name="t">The item ready to add to the list</param>
|
|
public void Add(T t)
|
|
{
|
|
//checkout if the linkedlist is null
|
|
if (isEmpty)
|
|
{
|
|
_heading._next = t.ID;
|
|
_heading._previous = t.ID;
|
|
t.Next = _heading;
|
|
t.Previous = _heading;
|
|
isEmpty = false;
|
|
Console.WriteLine("1st item ok");
|
|
Console.WriteLine(_heading._next);
|
|
Console.WriteLine(_heading._previous);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("next begin");
|
|
t.Next = _heading;
|
|
Console.WriteLine("step 01 ok");
|
|
t.Previous = _heading.Previous;
|
|
Console.WriteLine("step 02 ok");
|
|
_heading._previous._next = t.ID;
|
|
Console.WriteLine("step 03 ok");
|
|
_heading._previous = t.ID;
|
|
|
|
}
|
|
}
|
|
|
|
public void Remove(T node)
|
|
{
|
|
foreach (INode<T> item in this)
|
|
{
|
|
if (item == node)
|
|
{
|
|
node.SelfDestrcut();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void RemoveTail()
|
|
{
|
|
//check out if the list has only one member
|
|
if (Tail == First)
|
|
{
|
|
First = default;
|
|
|
|
|
|
isEmpty = true;
|
|
}
|
|
else
|
|
{
|
|
Tail.Previous.Next = _heading;
|
|
Tail.Next = null;
|
|
}
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return new LinkedListEnumrator<T>(this);
|
|
}
|
|
}
|
|
}
|