473,659 Members | 3,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Persistent linked list (node with fixed size)

Hi everyone,

Is there anybody who can suggest me a link where I can find information
about 'Persistent linked list' ?
I need to implement a linked list where every node is a structure like the
following:

struct Node
{
int integer_value;
char test_value[125];
}

The list should be saved on a flat file so that the list can be re-load at
every new start.

Thanks in advance
Fabio
Jul 19 '05 #1
10 5486

"Fabio" <fa************ *@libero.it> wrote in message
news:bo******** **@newsreader.m ailgate.org...
Hi everyone,

Is there anybody who can suggest me a link where I can find information
about 'Persistent linked list' ?
I need to implement a linked list where every node is a structure like the
following:

struct Node
{
int integer_value;
char test_value[125];
}


http://www.winghands.it/download/download.html?id=2

Antonio
Jul 19 '05 #2
Are you wanting to actually use this in memory as a linked list? Or an
array? Your struct doesn't have any pointer variables defined in it.

A linked list using your structure might be setup as

struct llNode {
Node myNode;
llNode *prevNode; // points to previous entry in list; NULL if
first entry
llNode *nextNode; // points to next entry in list; NULL if last
entry
};

You would need an additional pointer outside of the structure to point to
the first node in the list, a head pointer;

llNode *headNode;

When loading the file into memory, you would read each record, and execute
your function to insert a new node into the list, passing the headNode and
the Node data into the function.

When saving the linked list to disk, you would traverse the linked list and
write the data in myNode to the file.

Take a look at www.programmersheaven.com . They've got some examples there
that might help.

Paul
When
"Fabio" <fa************ *@libero.it> wrote in message
news:bo******** **@newsreader.m ailgate.org...
Hi everyone,

Is there anybody who can suggest me a link where I can find information
about 'Persistent linked list' ?
I need to implement a linked list where every node is a structure like the
following:

struct Node
{
int integer_value;
char test_value[125];
}

The list should be saved on a flat file so that the list can be re-load at
every new start.

Thanks in advance
Fabio

Jul 19 '05 #3


Fabio wrote:

Hi everyone,

Is there anybody who can suggest me a link where I can find information
about 'Persistent linked list' ?
I need to implement a linked list where every node is a structure like the
following:

struct Node
{
int integer_value;
char test_value[125];
}

The list should be saved on a flat file so that the list can be re-load at
every new start.


What's your specific problem?

In principle this is easy:

write list:
open file
for all nodes
write node
close file
read list:
empty list
open file
as long as there is something to read
read node
insert node into list
close file

Which step(s) give you troubles?`

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #4

"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:3F******** *******@gascad. at...

What's your specific problem?

In principle this is easy:


I suppose I should have worded my request slightly better.
I'm trying to implement a 'full persistent list'. It means that I cannot
have all nodes in memory but the list must be *only* on a flat file.
So when I have to add a node to the list, I allocate (or re-use) a node in
the flat file. The idea is to have an header with some information such has:
tail, head, free_block and manage a sort of lite persistent heap (witrh
transaction as option)

Fabio
I
Jul 19 '05 #5
Fabio wrote:
"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:3F******** *******@gascad. at...
What's your specific problem?

In principle this is easy:

I suppose I should have worded my request slightly better.
I'm trying to implement a 'full persistent list'. It means that I cannot
have all nodes in memory but the list must be *only* on a flat file.
So when I have to add a node to the list, I allocate (or re-use) a node in
the flat file. The idea is to have an header with some information such has:
tail, head, free_block and manage a sort of lite persistent heap (witrh
transaction as option)

Fabio
I


One consideration (probably not portable), is to use
the file position instead of pointers.

As far as re-using nodes, that depends on how you
define a node and whether you are using variable
sized records of data.

I decided to implement a persistant linked-list
using the following node:
struct Node
{
streampos data_posn;
streampos next_posn;
streampos prev_posn;
};
The above structure allowed each node to be
a fixed size and the data to be allocated in
the file elsewhere. Since the nodes are fixed
size, they could be re-used.

Note that one platforms value for streampos
may not be the same on another platform.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #6


Fabio wrote:

"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:3F******** *******@gascad. at...

What's your specific problem?

In principle this is easy:


I suppose I should have worded my request slightly better.
I'm trying to implement a 'full persistent list'. It means that I cannot
have all nodes in memory but the list must be *only* on a flat file.
So when I have to add a node to the list, I allocate (or re-use) a node in
the flat file. The idea is to have an header with some information such has:
tail, head, free_block and manage a sort of lite persistent heap (witrh
transaction as option)


OK.
Since your nodes have a fixed size, that shouldn't be a problem (add an
entry 'next' to your node, which is used for linking the nodes)

What you suggest above sounds good and would be the way I would do it:
Have 2 chains of nodes: one for used nodes, one for unused nodes, just
like an in-memory list with cached nodes, except that the memory address
is replaced by a "file address" (the things seekg() and tellg() use).

Something like this:
(Note: changing C-based file functions to C++ file functions is left
as an exercise to the read, as well as adding some error checks as well
as adding missing functions)

#include <iostream>
#include <cstring>
using namespace std;

struct MyNode
{
MyNode( int Value = 0, const char* Text = "" ) : m_Value( Value ),
m_Next( 0 )
{ strcpy( m_Text, Text ); }

int m_Value;
char m_Text[128];
long m_Next;
};

struct ListHeader
{
long m_UsedHead;
long m_UsedTail;
long m_UnusedHead;
};

class FileList
{
public:
FileList( const char* szFileName );
~FileList();

long GetHeadPos();
MyNode GetNext( long& Pos );
void Add( const MyNode& Node );
void DelFirst();

protected:
void Enlarge();

ListHeader m_Header;
FILE* m_File;
};

FileList::FileL ist( const char* szFileName )
{
if( ( m_File = fopen( szFileName, "r+" ) ) == NULL ) {
m_File = fopen( szFileName, "w+" );
m_Header.m_Used Head = 0;
m_Header.m_Used Tail = 0;
m_Header.m_Unus edHead = 0;
fwrite( &m_Header, sizeof( m_Header ), 1, m_File );
}

else
fread( &m_Header, sizeof( m_Header ), 1, m_File );
}

FileList::~File List()
{
fseek( m_File, 0, SEEK_SET );
fwrite( &m_Header, sizeof( m_Header ), 1, m_File );
fclose( m_File );
}

long FileList::GetHe adPos()
{
return m_Header.m_Used Head;
}

MyNode FileList::GetNe xt( long& Pos )
{
MyNode Temp;

fseek( m_File, Pos, SEEK_SET );
fread( &Temp, sizeof( MyNode ), 1, m_File );

Pos = Temp.m_Next;
return Temp;
}

void FileList::Enlar ge()
{
MyNode NewNode;
fseek( m_File, 0, SEEK_END );
m_Header.m_Unus edHead = ftell( m_File );
fwrite( &NewNode, sizeof( NewNode ), 1, m_File );
}

void FileList::Add( const MyNode& Node )
{
MyNode Temp;

if( m_Header.m_Unus edHead == 0 )
Enlarge();

// grab the first unused node and figure out its next field

fseek( m_File, m_Header.m_Unus edHead, SEEK_SET );
fread( &Temp, sizeof( Temp ), 1, m_File );
long NextUnused = Temp.m_Next;

// fill in the data and write the node back

Temp = Node;
Temp.m_Next = 0;
fseek( m_File, m_Header.m_Unus edHead, SEEK_SET );
fwrite( &Temp, sizeof( Temp ), 1, m_File );

// now do the bookkeeping
// the previously unused node gets used and thus
// needs to be chained in the link of used nodes.

if( m_Header.m_Used Head == 0 ) {
m_Header.m_Used Head = m_Header.m_Unus edHead;
}
else {
fseek( m_File, m_Header.m_Used Tail, SEEK_SET );
fread( &Temp, sizeof( Temp ), 1, m_File );
Temp.m_Next = m_Header.m_Unus edHead;
fseek( m_File, m_Header.m_Used Tail, SEEK_SET );
fwrite( &Temp, sizeof( Temp ), 1, m_File );
}
m_Header.m_Used Tail = m_Header.m_Unus edHead;

// shorten the list of unused nodes

m_Header.m_Unus edHead = NextUnused;
}

void FileList::DelFi rst()
{
if( m_Header.m_Used Head == 0 )
return;

MyNode Temp;
long FirstPos = m_Header.m_Used Head;
fseek( m_File, m_Header.m_Used Head, SEEK_SET );
fread( &Temp, sizeof( Temp ), 1, m_File );
m_Header.m_Used Head = Temp.m_Next;
Temp.m_Next = m_Header.m_Unus edHead;
fseek( m_File, FirstPos, SEEK_SET );
fwrite( &Temp, sizeof( Temp ), 1, m_File );

m_Header.m_Unus edHead = FirstPos;
}

void PrintList( FileList& List )
{
cout << "************** ******\n";

long Pos = List.GetHeadPos ();
while( Pos ) {
cout << "*** FilePos : " << Pos << "\n";
MyNode Node = List.GetNext( Pos );
cout << Node.m_Value << "\n";
cout << Node.m_Text << endl;
}
}

int main()
{
FileList MyList( "C:\\MyList.dat " );

PrintList( MyList );

MyList.Add( MyNode( 100, "Test1" ) );
MyList.Add( MyNode( 101, "Test2" ) );
MyList.Add( MyNode( 102, "Test3" ) );

PrintList( MyList );

MyList.DelFirst ();
MyList.DelFirst ();

PrintList( MyList );

MyList.Add( MyNode( 103, "Test4" ) );
MyList.Add( MyNode( 104, "Test5" ) );
MyList.Add( MyNode( 105, "Test6" ) );

PrintList( MyList );

return 0;
}

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #7

"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:3F******** *******@gascad. at...

Ok...your implementation is similar to what I had in mind.
But whatever structure I use to implement the list on a flat file I probably
need to execute two or more write operations because I update at
least two file pointers. What happen if one write operation fails or the
system crashs ?
Fabio
Jul 19 '05 #8


Fabio wrote:

"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:3F******** *******@gascad. at...

Ok...your implementation is similar to what I had in mind.
But whatever structure I use to implement the list on a flat file I probably
need to execute two or more write operations because I update at
least two file pointers. What happen if one write operation fails or the
system crashs ?


You end up with an inconsistent file :-)
Ever wondered why database systems cost that much of money?

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #9

"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:3F******** *******@gascad. at...

You end up with an inconsistent file :-)
Ever wondered why database systems cost that much of money?

You are right Karl...probably I was asking too much...or trying to do
something too complex for my requirements.
Thanks anyway for your help

Fabio
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3266
by: John | last post by:
Hi all, Can a linked list be a member of a structure? If so, when I add or remove an element from the linked list, the size of the structure will change. Will it cause any problem? Thanks a lot. John
19
13566
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e., head and tail are not connected. Of course, recursive function aproach to traverse the list is one way. But, depending upon the list size, it could overrun the stack pretty fast.
1
2039
by: Tim | last post by:
I can't seem to figure out why this very simple linked list wont build.. I mean, there is no intelligence, just add to end. Anyway, please let me know if something i can do will make head (the root pointer) not be null. /* LINKED LIST DEFINITIONS */ typedef struct a_fnode {
6
10137
by: Jim Showalter | last post by:
I'm trying to write code that gets fixed-length strings from the user and then stores them in a linked list. Here's the definition of my list node: struct node {char str; struct node* next; }; And here is my attempt at Push(), the function that adds new nodes to the list:
9
4899
by: Sheldon | last post by:
Hi, I am trying to understand linked lists and the different ways to write a linked list and double linked list. I have been trying to get this function called insert_word to work but to no avail. The function receives a string from main and then inserts in the list the new word in alphabetical order. Here is my function: struct word { // double linked list. Here the list is global and is first built
8
2722
by: Jeff Bown | last post by:
Consider implementing a (doubly) linked list. The simplest strategy is to provide functions item_t add_item(item_t predecessor); void delete_item(item_t item); where add_item allocates memory for a new list item and returns it (or NULL), and delete_item frees that memory. However, if at startup the program immediately adds a large number of items to a list, then all those calls to malloc() become expensive.
8
718
by: dmp | last post by:
What are Linked list? Please somebody show some ready made programs of linked list
6
3254
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to read my program I appriciate it. the program suppose to print a message on the screen #include <iostream> #include <string>
20
6532
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE() function in C.. But if I try to use a single linked list with a static char array fields I can free the memory allocated with out any problems using the FREE(). So, why freeing a single linked list with dynamic char* is hard and why the FREE() function is...
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8628
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.