|
我写了一个链表的代码,自己不知道怎样测试它会不会发生内存异常(例如没有释放掉什么东西之类的),大家帮忙看一下,最好帮我上几调试一下,谢了。(我的调试功底不是很好)。
[em15]:空格都被滤掉了,不是我没写空格。
unit untLinkTable;
interface
Type
LinkGetDataToFree=Procedure(Dataointer); //回调函数指针
LinkPointer=^LinkItem; //链表项指针
LinkItem=Record //链表项
Data:Pointer; //数据指针
PrevinkPointer; //前一项链表项指针
Next:LinkPointer; //后一项链表项指针
End;
LinkHead=Record //链表头
Node:LinkPointer; //链表连接节点,不包括数据
Count:Integer; //链表项数量
End;
Procedure LinkInitial(var Head:LinkHead;var Tail:LinkHead);
//初始化链表
Procedure LinkInsert(var Head:LinkHead;Node:LinkPointer;Data:Pointer);
//增加节点
Function LinkDelete(var Head:LinkHead;Node:LinkPointer):Pointer;
//删除节点,返回数据地址
Procedure LinkEmpty(var Head:LinkHead;var Tail:LinkHead;LinkProc:LinkGetDataToFree);
//清除节点数据
Procedure LinkFree(var Head:LinkHead;var Tail:LinkHead;LinkProc:LinkGetDataToFree);
//清除节点
Function LinkGetAddress(Head:LinkHead;Index:Integer):LinkPointer;
//获得节点地址
Function LinkGetData(Head:LinkHead;Index:Integer):Pointer;
//获得节点数据
implementation
Procedure LinkInitial(var Head:LinkHead;var Tail:LinkHead);
Begin
New(Head.Node);
New(Tail.Node);
Head.Node^.Data:=nil;
Head.Node^.Next:=Tail.Node;
Head.Node^.Prev:=nil;
Head.Count:=0;
Tail.Node.Data:=nil;
Tail.Node.Next:=nil;
Tail.Node^.Prev:=Head.Node;
Tail.Count:=0;
End;
Procedure LinkInsert(var Head:LinkHead;Node:LinkPointer;Data:Pointer);
var P:LinkPointer;
Begin
New(P);
P^.Prev:=Node;
P^.Next:=Node^.Next;
P^.Data:=Data;
Node^.Next:=P;
P^.Next^.Prev:=P;
Head.Count:=Head.Count+1;
End;
Function LinkDelete(var Head:LinkHead;Node:LinkPointer):Pointer;
Begin
Node^.Prev^.Next:=Node^.Next;
Node^.Next^.Prev:=Node^.Prev;
LinkDelete:=Node^.Data;
Dispose(Node);
Head.Count:=Head.Count-1;
End;
Procedure LinkEmpty(var Head:LinkHead;var Tail:LinkHead;LinkProc:LinkGetDataToFree);
var I,C:Integer;
Begin
C:=Head.Count;
For I:=1 To C Do
LinkProc(LinkDelete(Head,Head.Node^.Next));
Head.Count:=0;
Tail.Count:=0;
End;
Procedure LinkFree(var Head:LinkHead;var Tail:LinkHead;LinkProc:LinkGetDataToFree);
Begin
LinkEmpty(Head,Tail,LinkProc);
Dispose(Head.Node);
Dispose(Tail.Node);
End;
Function LinkGetAddress(Head:LinkHead;Index:Integer):LinkPointer;
var I:Integer;
P:LinkPointer;
Begin
P:=Head.Node;
For I:=0 To Index Do
P:=P^.Next;
LinkGetAddress:=P;
End;
Function LinkGetData(Head:LinkHead;Index:Integer):Pointer;
var P:LinkPointer;
Begin
P:=LinkGetAddress(Head,Index);
LinkGetData:=P^.Data;
End;
end. |
|