// 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;
};
// 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)�
{
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)�
{
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 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()
// 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);
};
//------------------------------------------------
// Itor
class Itor
{
public:
Itor(List& rL) : list(rL){};
void First(){pCur =list.pFirst;};
void Last(){pCur =list.pLast; cout<<'\n'<<pCur;};
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
{
ist.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;
}
}
�