广义表,依旧是泛型编程。可以编译通过。
///广义表
interface iGList<T>
{
void InitGList();
// void CreateGList(T[] S);
void DestroyGList();
//void CopyGList(iGList<T> Src);
int GListLength();
//int GListDepth();
bool GListEmpty();
T GetHead();
T GetTail();
void InsertFirst(T elem);
T DeleteFirst();
bool Traverse(visit<T> Visit);
}
///广义表
///广义表的实现
enum GListType
{
ATOM,
LIST
}
class GLNode<T>
{
public GListType type;
public GList<T> list;
public T data;
public GLNode<T> next;
}
class GList<T> : iGList<T>
{
GLNode<T> head;
int length;
public void InitGList()
{
length = 0;
head = new GLNode<T>();
}
public void DestroyGList()
{
head = null;
}
public GLNode<T> getData()
{
return head;
}
public void CopyGList(GList<T> Src)
{
head = Src.getData();
}
public int GListLength()
{
return length;
}
public int GListDepth(GLNode<T> forward)
{
if (head != null)
{
if (head.type == GListType.LIST)
{
if (head != null)
{
GListDepth(head);
}
}
else
{
if (head != null)
{
GListDepth(head.next);
}
}
return 1;
}
else
{
return 0;
}
}
public bool GListEmpty()
{
if (null == head)
{
return true;
}
else
{
return false;
}
}
public T GetHead()
{
return head.data;
}
public T GetTail()
{
GLNode<T> pH = head;
while (pH != null)
{
if (pH.next.next == null)
{
return pH.data;
}
pH = pH.next;
}
return default(T);
}
public void InsertFirst(T elem)
{
GLNode<T> pHead = new GLNode<T>();
pHead.type = GListType.ATOM;
pHead.data = elem;
pHead.list = null;
pHead.next = head;
head = pHead;
++length;
}
public void InsertFirst(GList<T> elem)
{
GLNode<T> pHead = new GLNode<T>();
pHead.type = GListType.LIST;
pHead.data = default(T);
pHead.list = elem;
pHead.next = head;
head = pHead;
++length;
}
public T DeleteFirst()
{
T res = head.data;
head = head.next;
--length;
return res;
}
public bool Traverse(visit<T> Visit)
{
GLNode<T> p = head;
while (p != null)
{
if (!Visit(p.data))
{
return false;
}
p = p.next;
}
return true;
}
}
///广义表的实现