<font color=red>用DELPHI做<B>链表</B></font>(50分)

  • 主题发起人 主题发起人 cat.yy
  • 开始时间 开始时间
C

cat.yy

Unregistered / Unconfirmed
GUEST, unregistred user!
DELPHI有动态数组,很好用 但在无法预知其尺寸的情况下 必须使用"链表"

我想做一个"双向链表" 怎么创建呢, 讲一个"单链表"的例子就行了
(节点怎么定义 才使他能创建新的节点呢??)

<FONT COLOR=RED>急! 急!! 急!!!</FONT>
 
http://www.turbopower.com/products/systools/Containers/

The SysTools list container is implemented as a doubly-linked list, a data
container where data objects are chained together like links on a chain:
from any data object you can find the next one (or the previous one) easily.
However, unlike an array (for example) you cannot 'jump' to a particular data
object, you have to search for it sequentially by following the links. Adding
and removing a data object is simple and fast: you are only affecting its immediate neighbors.
Contrast this with an array: insertion and deletion of a data object is
inefficient (you have to make room in the array for insertion, or close up
the hole for deletion), but accessing of random elements is easy. For large
numbers of elements (with corresponding insertions and deletions) an array
can be very inefficient compared with a linked list
 
看看书好不好!
type
p=Record
value : integer;
Pnext : PP;//后一个
Ppre : pp;//前一个
end;

用的时候new(pp),ok!
 
TO foxs,
你可能还得详细点

>> value : integer;
存放数据?

>>Pnext : PP;//后一个
>>p=Record <- 是"pp=Record" 吗??

举个例子吧
 
type
p=^pnode;
~~~~~~~~~~~
pnode=Record
value : integer;
Pnext : PP;//后一个
Ppre : pp;//前一个
end;
 
用完后怎么释放呢 free(pp)??
 
看看这本书《数据结构〉,中央广播电视大学出的。。。。
它帮到你
 
写了一个简单的:

unit BiLkList;

interface

uses
SysUtils;

type
PBiLkLstNode = ^TBiLkLstNode;
TBiLkLstNode = record
Data: Integer;
Prev, Next: PBiLkLstNode;
end;

TJBinaryLinkList = class
private
FHead, FTail: PBiLkLstNode;
function GetDataString: String;
public
constructor Create;
destructor Destroy
override;
procedure Clear;
function Add(Dat: Integer): TBiLkLstNode;
property DataString: String read GetDataString;
end;

implementation

constructor TJBinaryLinkList.Create;
begin
FHead := nil;
FTail := nil;
end;

destructor TJBinaryLinkList.Destroy;
begin
Clear;
inherited;
end;

procedure TJBinaryLinkList.Clear;
var
Ptr: PBiLkLstNode;
begin
Ptr := FHead;
while Assigned(Ptr) do begin
FHead := FHead^.Next;
Dispose(Ptr);
Ptr := FHead;
end;
FHead := nil;
FTail := nil;
end;

function TJBinaryLinkList.Add(Dat: Integer): TBiLkLstNode;
var
Ptr: PBiLkLstNode;
begin
Ptr := FTail;
if Assigned(FTail) then begin
New(FTail^.Next);
FTail := FTail^.Next;
end else begin
New(FTail);
FHead := FTail;
end;
with FTail^ do begin
Data := Dat;
Prev := Ptr;
Next := nil;
end;
Result := FTail^;
end;

function TJBinaryLinkList.GetDataString: String;
var
Ptr: PBiLkLstNode;
begin
Result := '';
Ptr := FHead;
while Assigned(Ptr) do begin
AppendStr(Result, IntToStr(Ptr^.Data) + ' ');
Ptr := Ptr^.Next;
end;
end;

end.
 
看看数据结构的书吧
 
用TList不就行了,什么都已经做好了,何必要自己去重新做一个。
 
在foxs和cocia的提示下 我已经做好了
感谢JohnsonGuo
 
后退
顶部