Delphi 单链表类 ,更新了一下类的定义
本帖最后由 daydream 于 2012-9-19 17:22 编辑type
PNode = ^TNode;
TNode = record
preNode : PNode;
nextNode: PNode;
ObjData : Tobject;
intIndex: Integer; //用作索引
strIndex: String; //用作索引
end;
TLinkList = class
private
HeadNode: PNode;
HMutex : THandle; //互斥锁,保证在多线程中,不重入
inumber : Integer;
protected
function GetTail : PNode;
public
property Head: PNode read HeadNode; //头结点
property Tail: PNode read GetTail;
property Count : Integer read inumber; //结点个数
//得到前后的结点
function GetNextNode(const node:PNode):PNode;
function GetPreNode(const node:PNode):PNode;
//插入删除结点
procedure AddItem(const Obj:TObject;
const integerIndex:Integer=0;
const stringIndex:string='';
const ToTail:Boolean=true);
function DelItem(node: PNode):Boolean;
//索引结点
function IndexOfInteger(const ivalue : Integer):PNode;
function IndexOfString(const strvalue : String):PNode;
function IndexOfObject(const objvalue : TObject):PNode;
function IsEmpty:Boolean;
procedure Clear;
constructor Create;
destructorFree;
end;测试代码var
i : integer;
P : PNode;
begin
list := TLinkList.Create;
i:=3;
list.AddItem(TObject(i));
i:=4;
list.AddItem(TObject(i));
P := list.Head;
list.DelItem(P^.nextNode);
P := list.Head^.nextNode;
while P<>nil do
begin
ShowMessage(IntToStr(Integer(P^.ObjData)));
P :=P^.nextNode;
end;
list.Free;
end; type
PDataRecore = ^TDataRecord;
TDataRecord = record
iInt : Integer;
sStr : String;
end;
var
i : integer;
P : PNode;
data : PDataRecore;
begin
list := TLinkList.Create;
New(data);
data^.iInt := 1;
data^.sStr := 'str';
list.AddItem(TObject(data));
New(data);
data^.iInt := 2;
data^.sStr := 'sstr';
list.AddItem(TObject(data));
P := list.Head^.nextNode;
while P<>nil do
begin
data:= Pointer(P^.ObjData);
ShowMessage(data^.sStr);
P :=P^.nextNode;
end;
list.Free;
页:
[1]