Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Home Library Index Site Map  Links Contact About

A functional Double Linked List


 
 
 
 

 

A functional Double Linked List
Headers
// INCLUDES
// the data structure contained in the list
#include "TDataStr.h"

// An initialised TDataStruct for testing
#include "DbugList.h"
#include <string.h> // strcpy
#include <conio.h> // getch()

#include "LexCmp.cpp" // lexical comparison function
Home Library Index Top

A functional Double Linked List
class Node
class Node
{
public:
friend class List;
friend class Itor;

TDataStruct Data() {return data;};
void Data(TDataStruct t) {data =t;};

private:
TDataStruct data;
Node *pNext, *pPrev;
};

Note:
Home Library Index Top

 

A functional Double Linked List
class List
class List
{
public:
friend class Itor;

List();
List(int num);
~List() {Clear();};

int Append();
int Pop();
int Clear();
void ShowList(); // debug only
void InitList(); // debug only

private:
Node *pNode, *pFirst, *pLast;
int length;
};
// List::List()
//----------------------------
List::List() : pNode(0), pLast(0), pFirst(0) // initialise pointers
{
Node *pNode = new Node; // initialise first empty node
pNode->pNext=0;
pNode->pPrev=0;
pLast =pNode;
pFirst =pNode;
length =1;

};

// List::List(int num)
//----------------------------
List::List(int num): pNode(0), pLast(0), pFirst(0) // initialise pointers
{
Node *pNode = new Node; // initialise first empty node
pNode->pNext=0;
pNode->pPrev=0;
pLast =pNode;
pFirst =pNode;
length =1;

for(int i=0; i<num-1; i++)
{
Append();
}
}

// List::APPEND() // adds node to end of list
//----------------------------
int
List::Append()
{
Node* pTemp =new Node;
if(!pTemp || !pLast) return 0;

pLast->pNext =pTemp;
pTemp->pNext =0;
pTemp->pPrev =pLast;
pLast =pTemp;

length++;
return 1;
};



// List::CLEAR(); // clears all but one nodes from list
//----------------------------
int
List::Clear()
{
if(!pNode || !pLast) return 0;
pNode =pLast;

while(pNode->pPrev)
{
Pop();
}
length =1;
return 1;
};

// List::POP(); // deletes end node
//----------------------------
int List::Pop()
{
Node* pTemp =pLast;
pNode =pLast->pPrev;
pNode->pNext =0;
delete pTemp;
pLast =pNode;

length--;
return 1;
};

// List::SHOWList() // debug only
//----------------------------
void
List::ShowList()
{
pNode =pFirst;
cout<<"\n\nShowList() "
<<"\n Address\tData\n\n"
<<pNode<<'\t'<<pNode->data.custNo;
cout<<'\t'<<pNode->data.companyName;
while (pNode->pNext)
{
pNode =pNode->pNext;
cout<<'\n'<<pNode<<'\t'<<pNode->data.custNo
<<'\t'<<pNode->data.companyName;
}
};

// List::INITList() // currently debug only
//----------------------------
void
List::InitList()
{
pNode =pFirst;
int i=0;
pNode->data.custNo =i;
strcpy(pNode->data.companyName,Companies[i]);
do
{
pNode =pNode->pNext;
i++;
pNode->data.custNo =i;
strcpy(pNode->data.companyName,Companies[i]);

}while(pNode!=pLast);
};
Note:
Home Library Index Top

A functional Double Linked List
class Itor
class Itor
{
public:
Itor(List& rL) : list(rL){};

void First(){pCur =list.pFirst;};
void Last(){pCur =list.pLast;};
int Attach(TDataStruct& t);
// int Detach();
Node* Cur(){/*cout<<"\npCur =="<<pCur;*/ return pCur;};
const List& GetList() {return list;};

void operator--(){pCur =pCur->pPrev;};
void operator++(){pCur =pCur->pNext;};
private:
Node *pCur;
List& list;
};

// Itor::ATTACH()
//----------------------------
int
Itor::Attach(TDataStruct& t)
{
First();
if(!pCur) return 0;


while(LexCmp(t.companyName, pCur->Data().companyName)>0)
{
if(!pCur->pNext) // at end
{
list.Append();
Last();
pCur->Data(t);
return 2;
}
pCur= pCur->pNext;
}

if(!pCur->pPrev) // at beginning
{
Node* pTemp =new Node;

pTemp->pPrev =0;
pTemp->pNext =pCur;
pCur->pPrev =pTemp;

pCur=pTemp;
pCur->Data(t);

list.pFirst =pCur;
list.length++;

return 1;
}
else
{
pCur=pCur->pPrev;
Node* pTemp =new Node;
pTemp->pNext =pCur->pNext;
pTemp->pPrev =pCur;
pCur->pNext->pPrev =pTemp;
pCur->pNext=pTemp;

pCur=pTemp;
pCur->Data(t);
list.length++;

return 3;
}

}
Note:
Home Library Index Top

A functional Double Linked List
Driver for testing classes node, list + itor
void main()
{
TDataStruct first,middle,last; // initialise some data
strcpy(first.companyName,"AT&T"); first.custNo=0;
strcpy(middle.companyName,"Logica"); middle.custNo=0;
strcpy(last.companyName,"Salomons");last.custNo=0;

const int LEN=7;

List alist(LEN); // 1 x list
Itor listItor(alist); // 1 x Itor
alist.InitList(); // populate outlist
alist.ShowList(); // display it

cout<<'\n'<<"Press any key to continue...";
getch();

listItor.Attach(first); //
alist.ShowList(); // display it
cout<<'\n'<<"Press any key to continue...";
getch();

listItor.Attach(middle); //
alist.ShowList(); // display it
cout<<'\n'<<"Press any key to continue...";
getch();

listItor.Attach(last);
alist.ShowList(); // display it
cout<<'\n'<<"Press any key to continue...";
getch();


cout<<'\n'<<"Press any key to exit...";
getch();
}; // MAIN
Note:
Home Library Index Top

 

STL Database
Arrays
Binary File Input / Output
Character Input
Containers
Standard Template Library
Streams
Templates
Utility Functions
Win32 Programming
Miscellaneous
 

 
copyright notice

Copyright Robert Mitchell. Last Revised : 28 April, 2000

e-mail me
1