beautypg.com

HP Integrity NonStop J-Series User Manual

Page 91

background image

//5

This is the constructor for an

RWFileManager

. It will create a new file, called linklist.dat.

//6

Allocate space on the file to store the offset to the first link. This first allocation is
considered special and will be saved by the

RWFileManager

. It can be retrieved at any

time by using the member function start().

//7

Allocate space to store the first link. The member function allocate() returns the offset to
this space. Since each DiskNode needs the offset to the next DiskNode, space for the next
link must be allocated before the current link is written.

//8

Seek to the position to write the offset to the first link. Note that the offset to this position
is returned by the member function start(). Note also that fm has access to public member
functions of class

RWFile

, since class

RWFileManager

is derived from class RWFile.

//9

Write the offset to the first link.

//10 A loop to read integers and store them in a linked-list.

//11 Allocate space for the next link, storing the offset to it in the nextNode field of this link.

//12 Seek to the proper offset to store this link

//13 Write this link.

//14 Since we allocate the next link before we write the current link, the final link in the list

will have an offset to an allocated block that is not used. It must be handled as a special
case.

//15 First, deallocate the final unused block.

//16 Next, reassign the offset of the final link to be RWNIL. When the list is read, this will

indicate the end of the linked list. Finally, rewrite the final link with the correct
information.

//17 The destructor for class

RWFileManager

, which closes the file, will be called here.

Having created the linked-list on disk, how might you read it? Here is a program that reads the
list and prints the stored integer field:

#include
#include

struct DiskNode {
int data;
Rwoffset nextNode;
};

main(){
RWFileManager fm("linklist.dat"); // 1

fm.SeekTo(fm.start()); // 2
RWoffset next;

This manual is related to the following products: