home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.barnyard.co.uk
/
2015.02.ftp.barnyard.co.uk.tar
/
ftp.barnyard.co.uk
/
cpm
/
walnut-creek-CDROM
/
ENTERPRS
/
CPM
/
UTILS
/
A
/
BYTETURB.ARC
/
LINKED.LIB
< prev
next >
Wrap
Text File
|
1989-09-27
|
4KB
|
160 lines
{
procedures and function for linked lists
NOTE: the following routines assume that you have declared the following
data types (they won't compile, otherwise):
type
NodePtr = ^Node;
Node =
record
Next,Last : NodePtr;
... : (* data fields *)
end;
Using such a structure, these routines will implement a circular, doubly-
linked list. They assume that you have use a header node for each list,
that is, a variable of type NodePtr which is used only to point to the
list and which (usually) doesn't hold any other information. To create
the list, just call GetNode, passing it that header variable.
InsertNode inserts one node after another in a linked list
RemoveNode removes a node from a linked list
GetNode creates new node if space in available
CreateList creates a new linked list
RemoveList completely removes linked list (incl. header)
Push pushes node onto stack
Pop pops node off of stack
Add adds node to one end of list (queue or deque)
Take pulls node off of one end of list (queue or deque)
}
const
Front = True; { for use with Put and Get }
Rear = False; { ditto }
procedure InsertNode(var NPtr,TPtr : NodePtr);
{
purpose inserts NPtr after TPtr
last update 03 Jul 85
}
begin
NPtr^.Last := TPtr;
NPtr^.Next := TPtr^.Next;
TPtr^.Next := NPtr;
NPtr^.Next^.Last := NPtr
end; { of proc InsertNode }
procedure RemoveNode(var NPtr,TPtr : NodePtr);
{
purpose assigns NPtr = TPtr and removes NPtr from linked list
last update 08 Jul 85
}
begin
NPtr := TPtr;
with NPtr^ do begin
Last^.Next := Next;
Next^.Last := Last;
Last := nil; Next := nil
end
end; { of proc RemoveNode }
function GetNode(var NPtr : NodePtr) : Boolean;
{
purpose allocate node if space is available
last update 08 Jul 85
}
begin
if MemAvail > 4*SizeOf(Node) then begin { leave a little cushion }
New(NPtr);
with NPtr^ do begin
FillChar(NPtr^,SizeOf(NPtr^),0);
Next := nil;
Last := nil
end;
GetNode := True
end
else begin
GetNode := False;
NPtr := nil
end
end; { of func GetNode }
procedure CreateList(var Header : NodePtr);
{
purpose creates a new linked list
last update 10 Jul 85
}
begin
if GetNode(Header) then with Header^ do begin
Next := Header;
Last := Header
end
end; { of proc CreateList }
procedure RemoveList(var Header : NodePtr);
{
purpose completely removes linked list
last update 03 Jul 85
}
var
TPtr : NodePtr;
begin
while Header^.Next <> Header do begin
RemoveNode(TPtr,Header^.Next);
Dispose(TPtr)
end;
Dispose(Header)
end; { of proc RemoveList }
{ routines for stack handling }
procedure Push(var NPtr,Header : NodePtr);
{
purpose pushes NPtr onto stack
last update 09 Jul 85
}
begin
InsertNode(NPtr,Header)
end; { of proc Push }
procedure Pop(var NPtr,Header : NodePtr);
{
purpose pops NPtr off of stack
last update 09 Jul 85
}
begin
if Header^.Next <> Header
then RemoveNode(NPtr,Header^.Next)
else NPtr := nil
end; { of proc Pop }
{ routines for queues and deques }
procedure Add(var NPtr,Header : NodePtr; onFront : Boolean);
{
purpose adds NPtr on front or rear of list
last update 09 Jul 85
}
begin
if onFront
then InsertNode(NPtr,Header)
else InsertNode(NPtr,Header^.Last)
end; { of proc Add }
procedure Take(var NPtr,Header : NodePtr; offFront : Boolean);
{
purpose takes NPtr off of front or rear of list
last update 09 Jul 85
}
begin
if Header^.Next = Header
then NPtr := nil
else if offFront
then RemoveNode(NPtr,Header^.Next)
else RemoveNode(NPtr,Header^.Last)
end; { of proc Take }