请问谁能用类pascal编一个链表,能不能给我举个例子,多谢了(50分)

  • 主题发起人 主题发起人 zhaohg
  • 开始时间 开始时间
Z

zhaohg

Unregistered / Unconfirmed
GUEST, unregistred user!
请问谁能用类pascal编一个链表,能不能给我举个例子,多谢了
 
教科书上多的是这种例子。
 
Delphi的源码里多的是
TList
TStrings
TStringList
 
type
pnode=^tnode;
tnode=record
data:datatype;//数据
next:pnode;
end;
var head,tale:pnode;//头尾指针
tmp:pnode;
i:integer;

new(head);head^.next:=nil;tale:=head;

for i:=1 to 10 do //建立一个长为10(不包括头)的链表
begin
new(tmp);tmp^.next:=nil;tmp^.data:=datas;
tale^.next:=tmp;
tale:=tale^.next;
end;

满意吗

DELPHI里也可以这样:
type
tnode=class;
tnode=class
data:datatype;
next:tnode;
end;
... ^o^
 
我有个双连表例子
Here's the code for a simple generic unordered linked-list class. OK, so there's not much you can do with it as it stands but it is easily extended to real working code. My Maps library consists of five generic associative containers based on a doubly-linked list, a hashed array tree, a hash table, a binary search tree, and a treap. They offer a range of performance tradeoffs depending upon whether you project needs addition or searching or deletion, etc., to be fastest. The simple code below, though, should give you the idea. Even this list is truly generic. A very simple test program is available to exercise the list. Click here to download the sample code.
type
PNode = ^TNode

TNode = record
Item : TVarRec

Left : PNode

Right : PNode

end

type
TGenericList = class
private
Head : PNode

Tail : PNode

SP: Pnode

protected
public
constructor Create

destructor Destroy
override

function AddItem(const Item : TVarRec) : PNode

procedure AddItems(Items : array of const)

procedure Clear

function First : PNode

function GetItem(const Index : PNode) : TVarRec

function Last : PNode

function Next : PNode

function Prev : PNode

end


{ TGenericList }


function TGenericList.AddItem(const Item : TVarRec) : PNode

var
P : PNode

begin
GetMem(P, SizeOf(Tnode))

CopyVarRec(Item, P.Item)

P.Left := nil

P.Right := Head

if (Tail = nil) then
begin
{ previously empty }
Tail := P

Head := Tail

end
else
begin
Head.Left := P

Head := P

end

Result := P

end


procedure TGenericList.AddItems(Items: array of const)

var
n: integer

begin
for n:= 0 to High(Items) do
AddItem(Items[n])

end


procedure TGenericList.Clear

var
P : PNode

begin
while Head <> nil do
begin
P := Head

Head := Head.Right

ClearVarRec(P.Item)

FreeMem(P)

end

Tail := nil

end


constructor TGenericList.Create

begin
inherited Create

Head := nil

Tail := nil

end


destructor TGenericList.Destroy

begin
Clear

inherited Destroy

end


function TGenericList.First : PNode

begin
SP := Head

Result := SP

end


function TGenericList.GetItem(const Index : PNode) : TVarRec

begin
Result := Index.Item

end


function TGenericList.Last : PNode

begin
SP := Tail

Result := SP

end


function TGenericList.Next : PNode

begin
if SP <> nil then
begin
SP := SP.Right

end

Result := SP

end


function TGenericList.Prev : PNode

begin
if SP <> nil then
begin
SP := SP.Left

end

Result := SP

end;
 
其实用tstringlist来做链表即方便又快捷;)
type
p=record
str:string[10];
number:integer;
end;
var
t:tstringlist;
pp:^p;
begin
new(pp);
pp.str:='xxx';
pp.number:=99;
t.addobject('',pointer(pp));//存入链表
pp:=pointer(t.objects[0]);//从链表取出
t.delete(0);//释放链表
dispose(pp);//释放链表
end;
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=724012
 
我最近也编了一个要使用链表的程序,本来也想用手工作个链表的,后来研究了一下Tlist(它是用来存放指针的),发现它真的很简单,最后就用它了:
Type
P=^pRecord
pRecord=record
data:integer;
s:string;
end;
Var
pp:p
myList:TList;
i:integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
myList:=Tlist.Create ;
new(pp);
pp^.data:=5;
pp^.s:='ok';
myList.Add(pp)
//添加数据
myList.Remove(pp)
//删除数据
pp:=myList.Items
//读取数据
myList.Insert(i,pp)
//插如数据
//遍历链表
For I:=0 To myList.count-1 do pp:=myList.Items(i);
//或
i:=0
Repeat
Begin
pp:=myList.Items (i);
inc(i);
end
until pp=myList.last;

myList.Free //释放list
end;
 
请问firemeteor:

主程序里的begin和end哪里去了?
 
还有,既然链表的长度都由固定的数字来决定,那么它和数组有何区别?我不如用动态数组了
 
后退
顶部