hi,
I have been writing a program that will allow a user to add a "node" to a list of "nodes" along with a priority number. After the user has created the "node" he will be able to view a list of all created "nodes" shown in ascending order according to priority number. I have completed most of the code... but two functions, "Add Node To List" and "Remove Node From List" require me to implement a Linked list of pointers and I have had little experience in that area... I will show you all of my code so far... if anyone can help me implement these two functions I would appreciate it! Thank you
Menu Setup.cpp
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <iostream.h>
#include "COList.h"
#include "Memcheck.h"
// Set DEBUG to 1 in order to activate some debugging features:
#define DEBUG 0
int main()
{ //local prototypes:
void DisplayMenu(char &);
void ProcessMenuChoice(char &, COList &);
//local variable declarations:
char cMenuChoice;
COList List;
//the process loop:
DisplayMenu(cMenuChoice);
while (cMenuChoice != 'x')
{
ProcessMenuChoice(cMenuChoice,List);
DisplayMenu(cMenuChoice);
}
return 0;
}
void DisplayMenu(char & cMenuChoice)
{
/*
This function displays a simple menu, prompts the user to
enter his/her choice and performs some rudimentary
verification of the user's response.
*/
cout << endl
<< "Please enter one of the following characters:\n\n"
<< "a Add data to the list.\n"
<< "d Delete data from the list.\n"
<< "l List (i.e. display) all the data in the list.\n"
<< "x Exit from the program.\n\n"
<< "Menu selection ===> ";
cin >> cMenuChoice;
cMenuChoice = tolower(cMenuChoice); //convert to lower case and
while ( strchr("adlx",cMenuChoice) == 0 ) //accept letters a d l x only
{
cout << "\nInvalid selection! Please enter a, d, l, or x.\n\n"
<< "Menu selection ===> ";
cin >> cMenuChoice;
cMenuChoice = tolower(cMenuChoice);
}
return;
}
void ProcessMenuChoice(char & cMenuChoice, COList & List)
{
//function prototypes:
bool AddTransaction(COList &);
bool DeleteTransaction(COList &);
bool ListTransaction(COList &);
//define a boolean Return Code variable:
bool bSuccess = true;
//process the menu selection:
switch(cMenuChoice)
{
case 'a':
bSuccess = AddTransaction(List);
break;
case 'd':
bSuccess = DeleteTransaction(List);
break;
case 'l':
bSuccess = ListTransaction(List);
break;
default:
cerr << "\n\aLOGIC ERROR! We should never get here!\n\n";
}
//for testing purposes only (set DEBUG to 1 in order to use it):
if ( bSuccess == false && DEBUG )
cerr << "\a\nDEBUG WARNING: Transaction failed!\n";
return;
}
bool AddTransaction(COList & List)
{
bool GetNodeInfo(PointerToData & pData);
PointerToData pData;
bool bSuccess = true;
bSuccess = GetNodeInfo(pData); // Get a Data object
if ( bSuccess ) // Add the Data object to the List
bSuccess = List.AddNodeToList(pData);
if ( bSuccess ) // And display a message
cout << "Node " << pData->GetId() << " added to the list.\n";
else
cout << "\a\nERROR: No Node added to the list." << endl;
return bSuccess;
}
bool DeleteTransaction(COList & List)
{
bool bSuccess = true;
if( List.ListIsEmpty() )
cout << "\a\nERROR: The list is empty!\a\n";
else
{
NodeId szNodeId;
int nPriority = 127;
cout << "\nEnter ID number of the node to remove: ";
cin >> szNodeId;
bSuccess = List.RemoveNodeFromList(szNodeId);
if ( bSuccess )
{
cout << "Node " << szNodeId << " removed from the list.\n";
}
else
cout << "\a\nERROR: The list does not contain a nodeID " << szNodeId << endl;
}
return bSuccess;
}
bool ListTransaction(COList & List)
{
bool bSuccess = List.DisplayNodesInList();
if ( bSuccess == false )
cout << "\a\nERROR: The list is empty!\n";
return bSuccess;
}
bool GetNodeInfo(PointerToData & pData)
{
/*
This method prompts the user for a Node ID(a string of up to ten characters) and a numeric priority. The priority number is then edited in order to make sure it is in the range 0 through 127. The Node ID and the priority number are then stored in a dynamically allocated Data object. The pointer to that Data object is passed back to the calling method. Note that the string input is not fool proof: if the user enters more than ten characters, the program may crash!
*/
NodeId szNodeId;
int nPriority = 127;
bool bSuccess = false;
cout << "\nEnter ID number for the new node: ";
cin >> szNodeId;
cout << "\nEnter a positive priority number in range 0 - 127: ";
cin >> nPriority;
nPriority = (nPriority < 0 ? -nPriority : nPriority);
nPriority = (nPriority > 127 ? 127 : nPriority);
pData = new CData(szNodeId,nPriority);
if ( pData )
bSuccess = true;
else
cerr << "\n\aMemory Allocation Error for data object!\n\n";
return bSuccess;
}
----------------------------------------------------------
CDATA.cpp
#include <iostream.h>
#include <string.h>
#include "CDATA.H"
#ifndef CDATA_CPP
#define CDATA_CPP
CData::CData(const char * pNewNodeId, int nNewPriority)
{
// cout << "... CData constructor (Id: " << pNewNodeId << "\n";
if (pNewNodeId)
strcpy(szNodeId,pNewNodeId);
else
{
cerr << "\nInvalid Node Id passed to CData object!\n";
strcpy(szNodeId,"INVALID ID"
}
SetPriority(nNewPriority);
}
CData::~CData()
{
// cout << "... CData destructor (Id: " << szNodeId << "\n";
}
const char * CData::GetId() const
{
return szNodeId;
}
int CData::GetPriority() const
{
return nPriority;
}
void CData::SetPriority(int nNewPriority)
{
nPriority = nNewPriority;
return;
}
bool CData:perator == (const CData & OtherObject)
{
return nPriority == OtherObject.nPriority;
}
bool CData:perator != (const CData & OtherObject)
{
return nPriority != OtherObject.nPriority;
}
bool CData:perator < (const CData & OtherObject)
{
return nPriority < OtherObject.nPriority;
}
bool CData:perator <= (const CData & OtherObject)
{
return nPriority <= OtherObject.nPriority;
}
bool CData:perator > (const CData & OtherObject)
{
return nPriority > OtherObject.nPriority;
}
bool CData:perator >= (const CData & OtherObject)
{
return nPriority >= OtherObject.nPriority;
}
#endif
----------------------------------------------------------
CData.h
#ifndef CDATA_H
#define CDATA_H
const short int NODEID_LENGTH = 11;
typedef char NodeId [NODEID_LENGTH];
class CData
{
protected:
NodeId szNodeId;
int nPriority;
public:
CData(const char * pNewNodeId, int nNewPriority);
~CData();
const char * GetId() const;
int GetPriority() const;
void SetPriority(int nNewPriority);
bool operator == (const CData & OtherObject);
bool operator != (const CData & OtherObject);
bool operator < (const CData & OtherObject);
bool operator <= (const CData & OtherObject);
bool operator > (const CData & OtherObject);
bool operator >= (const CData & OtherObject);
};
typedef CData * PointerToData;
#endif
----------------------------------------------------------
CNode.cpp
#ifndef CNODE_CPP
#define CNODE_CPP
#include <iostream.h>
#include "CData.h"
#include "CNode.h"
CNode::CNode(const PointerToData pNewData)
{
// cout << "... CNode constructor (Id: " << pNewData->GetId() << "\n";
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}
CNode::~CNode()
{
// cout << "... CNode destructor (Id: " << pData->GetId() << "\n";
}
#endif
#ifndef CNODE_CPP
#define CNODE_CPP
#include <iostream.h>
#include "CData.h"
#include "CNode.h"
CNode::CNode(const PointerToData pNewData)
{
// cout << "... CNode constructor (Id: " << pNewData->GetId() << "\n";
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}
CNode::~CNode()
{
// cout << "... CNode destructor (Id: " << pData->GetId() << "\n";
}
#endif
#ifndef CNODE_CPP
#define CNODE_CPP
#include <iostream.h>
#include "CData.h"
#include "CNode.h"
CNode::CNode(const PointerToData pNewData)
{
// cout << "... CNode constructor (Id: " << pNewData->GetId() << "\n";
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}
CNode::~CNode()
{
// cout << "... CNode destructor (Id: " << pData->GetId() << "\n";
}
#endif
#ifndef CNODE_CPP
#define CNODE_CPP
#include <iostream.h>
#include "CData.h"
#include "CNode.h"
CNode::CNode(const PointerToData pNewData)
{
// cout << "... CNode constructor (Id: " << pNewData->GetId() << "\n";
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}
CNode::~CNode()
{
// cout << "... CNode destructor (Id: " << pData->GetId() << "\n";
}
#endif
#ifndef CNODE_CPP
#define CNODE_CPP
#include <iostream.h>
#include "CData.h"
#include "CNode.h"
CNode::CNode(const PointerToData pNewData)
{
// cout << "... CNode constructor (Id: " << pNewData->GetId() << "\n";
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}
CNode::~CNode()
{
// cout << "... CNode destructor (Id: " << pData->GetId() << "\n";
}
#endif
----------------------------------------------------------
CNode.h
#ifndef CNODE_H
#define CNODE_H
#include "CData.h"
class CNode; // needed for next typedef
typedef CNode * PointerToNode; // alias for a pointer to a Node
class CNode
{
friend class COList; friend class CQueue; public:
CNode(const PointerToData pNewData);// constructor function
~CNode(); // destructor function
protected:
PointerToData pData; // points to the data object
PointerToNode pPreviousNode; // points to the previous node
PointerToNode pNextNode; // points to the next node
};
#endif
#ifndef CNODE_H
#define CNODE_H
#include "CData.h"
class CNode; // needed for next typedef
typedef CNode * PointerToNode; // alias for a pointer to a Node
class CNode
{
friend class COList; // needed for lab 1 & 2
friend class CQueue; // needed for lab 2
public:
CNode(const PointerToData pNewData);// constructor function
~CNode(); // destructor function
protected:
PointerToData pData; // points to the data object
PointerToNode pPreviousNode; // points to the previous node
PointerToNode pNextNode; // points to the next node
};
#endif
#ifndef CNODE_H
#define CNODE_H
#include "CData.h"
class CNode; // needed for next typedef
typedef CNode * PointerToNode; // alias for a pointer to a Node
class CNode
{
friend class COList; // needed for lab 1 & 2
friend class CQueue; // needed for lab 2
public:
CNode(const PointerToData pNewData);// constructor function
~CNode(); // destructor function
protected:
PointerToData pData; // points to the data object
PointerToNode pPreviousNode; // points to the previous node
PointerToNode pNextNode; // points to the next node
};
#endif
----------------------------------------------------------
COList.cpp
#ifndef COLIST_CPP
#define COLIST_CPP
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include "CData.h"
#include "COList.h"
COList::COList()
{
// cout << "... COList constructor\n";
pHead = pTail = pCurrent = 0;
nNumberOfNodes = 0;
}
COList::~COList()
{
// cout << "... COList destructor\n";
while ( !ListIsEmpty() )
{
RemoveNodeFromList(pHead->pData->GetId());
}
}
int COList::GetNumberOfNodes() const
{
return nNumberOfNodes;
}
bool COList::AddNodeToList(PointerToData pData)
{
bool bSuccess = true;
// Can't figure this one out.
return bSuccess;
}
bool COList::RemoveNodeFromList(const char* szNodeId)
{
bool bSuccess = true;
//Can't figure this one out!
return bSuccess;
}
bool COList::ListIsEmpty() const
{
return nNumberOfNodes == 0;
}
bool COList:isplayNodesInList()
{
bool bSuccess = true;
if ( ListIsEmpty() )
bSuccess = false;
else
{
cout << "\nThere are currently " << nNumberOfNodes
<< " nodes in this List:\n\nNode ID Pri\n"
<< "========== ===\n";
PointerToNode pNode = pHead;
while ( pNode )
{
cout << setw(13) << setiosflags(ios::left)
<< pNode->pData->GetId()
<< setw(3) << setiosflags(ios::right)
<< pNode->pData->GetPriority() << endl;
pNode = pNode->pNextNode;
}
}
return bSuccess;
}
PointerToData COList::GetPointerToHeadData() const
{
return pHead->pData;
}
bool COList::IsNodeIdInList(const char *szNodeId)
{
bool bInList = false;
pCurrent = pHead;
for( int nNode = 0; nNode < nNumberOfNodes; nNode++ )
{
if( strcmp(pCurrent->pData->GetId(), szNodeId) == 0 )
{
bInList = true;
nNode = nNumberOfNodes;
}
else
pCurrent = pCurrent->pNextNode;
}
return bInList;
}
#endif
#ifndef COLIST_CPP
#define COLIST_CPP
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include "CData.h"
#include "COList.h"
COList::COList()
{
// cout << "... COList constructor\n";
pHead = pTail = pCurrent = 0;
nNumberOfNodes = 0;
}
COList::~COList()
{
// cout << "... COList destructor\n";
while ( !ListIsEmpty() )
{
RemoveNodeFromList(pHead->pData->GetId());
}
}
int COList::GetNumberOfNodes() const
{
return nNumberOfNodes;
}
bool COList::AddNodeToList(PointerToData pData)
{
bool bSuccess = true;
// To be completed by student.
return bSuccess;
}
bool COList::RemoveNodeFromList(const char* szNodeId)
{
bool bSuccess = true;
// To be completed by student.
return bSuccess;
}
bool COList::ListIsEmpty() const
{
return nNumberOfNodes == 0;
}
bool COList:isplayNodesInList()
{
bool bSuccess = true;
if ( ListIsEmpty() )
bSuccess = false;
else
{
cout << "\nThere are currently " << nNumberOfNodes
<< " nodes in this List:\n\nNode ID Pri\n"
<< "========== ===\n";
PointerToNode pNode = pHead;
while ( pNode )
{
cout << setw(13) << setiosflags(ios::left)
<< pNode->pData->GetId()
<< setw(3) << setiosflags(ios::right)
<< pNode->pData->GetPriority() << endl;
pNode = pNode->pNextNode;
}
}
return bSuccess;
}
PointerToData COList::GetPointerToHeadData() const
{
return pHead->pData;
}
bool COList::IsNodeIdInList(const char *szNodeId)
{
bool bInList = false;
pCurrent = pHead;
for( int nNode = 0; nNode < nNumberOfNodes; nNode++ )
{
if( strcmp(pCurrent->pData->GetId(), szNodeId) == 0 )
{
bInList = true;
nNode = nNumberOfNodes;
}
else
pCurrent = pCurrent->pNextNode;
}
return bInList;
}
#endif
----------------------------------------------------------
COList.h
#ifndef COLIST_H
#define COLIST_H
#include "CData.h"
#include "CNode.h"
class COList
{
public:
COList();
~COList();
int GetNumberOfNodes() const;
bool AddNodeToList(PointerToData);
bool RemoveNodeFromList(const char* szNodeId);
bool ListIsEmpty() const;
bool DisplayNodesInList();
PointerToData GetPointerToHeadData() const;
bool IsNodeIdInList(const char* szNodeId);
protected:
int nNumberOfNodes;
PointerToNode pHead;
PointerToNode pTail;
PointerToNode pCurrent;
};
#endif
#ifndef COLIST_H
#define COLIST_H
#include "CData.h"
#include "CNode.h"
class COList
{
public:
COList();
~COList();
int GetNumberOfNodes() const;
bool AddNodeToList(PointerToData);
bool RemoveNodeFromList(const char* szNodeId);
bool ListIsEmpty() const;
bool DisplayNodesInList();
PointerToData GetPointerToHeadData() const;
bool IsNodeIdInList(const char* szNodeId);
protected:
int nNumberOfNodes;
PointerToNode pHead;
PointerToNode pTail;
PointerToNode pCurrent;
};
#endif
----------------------------------------------------------
There, that is all I have done so far... I do have a memory checker.cpp file but it is not needed to solve this problem!! If anyone has any idea on how to set up a linked list to add and delete nodes from my "List Of Nodes" please help me out!!! Thank you
Chmilz
I have been writing a program that will allow a user to add a "node" to a list of "nodes" along with a priority number. After the user has created the "node" he will be able to view a list of all created "nodes" shown in ascending order according to priority number. I have completed most of the code... but two functions, "Add Node To List" and "Remove Node From List" require me to implement a Linked list of pointers and I have had little experience in that area... I will show you all of my code so far... if anyone can help me implement these two functions I would appreciate it! Thank you
Menu Setup.cpp
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <iostream.h>
#include "COList.h"
#include "Memcheck.h"
// Set DEBUG to 1 in order to activate some debugging features:
#define DEBUG 0
int main()
{ //local prototypes:
void DisplayMenu(char &);
void ProcessMenuChoice(char &, COList &);
//local variable declarations:
char cMenuChoice;
COList List;
//the process loop:
DisplayMenu(cMenuChoice);
while (cMenuChoice != 'x')
{
ProcessMenuChoice(cMenuChoice,List);
DisplayMenu(cMenuChoice);
}
return 0;
}
void DisplayMenu(char & cMenuChoice)
{
/*
This function displays a simple menu, prompts the user to
enter his/her choice and performs some rudimentary
verification of the user's response.
*/
cout << endl
<< "Please enter one of the following characters:\n\n"
<< "a Add data to the list.\n"
<< "d Delete data from the list.\n"
<< "l List (i.e. display) all the data in the list.\n"
<< "x Exit from the program.\n\n"
<< "Menu selection ===> ";
cin >> cMenuChoice;
cMenuChoice = tolower(cMenuChoice); //convert to lower case and
while ( strchr("adlx",cMenuChoice) == 0 ) //accept letters a d l x only
{
cout << "\nInvalid selection! Please enter a, d, l, or x.\n\n"
<< "Menu selection ===> ";
cin >> cMenuChoice;
cMenuChoice = tolower(cMenuChoice);
}
return;
}
void ProcessMenuChoice(char & cMenuChoice, COList & List)
{
//function prototypes:
bool AddTransaction(COList &);
bool DeleteTransaction(COList &);
bool ListTransaction(COList &);
//define a boolean Return Code variable:
bool bSuccess = true;
//process the menu selection:
switch(cMenuChoice)
{
case 'a':
bSuccess = AddTransaction(List);
break;
case 'd':
bSuccess = DeleteTransaction(List);
break;
case 'l':
bSuccess = ListTransaction(List);
break;
default:
cerr << "\n\aLOGIC ERROR! We should never get here!\n\n";
}
//for testing purposes only (set DEBUG to 1 in order to use it):
if ( bSuccess == false && DEBUG )
cerr << "\a\nDEBUG WARNING: Transaction failed!\n";
return;
}
bool AddTransaction(COList & List)
{
bool GetNodeInfo(PointerToData & pData);
PointerToData pData;
bool bSuccess = true;
bSuccess = GetNodeInfo(pData); // Get a Data object
if ( bSuccess ) // Add the Data object to the List
bSuccess = List.AddNodeToList(pData);
if ( bSuccess ) // And display a message
cout << "Node " << pData->GetId() << " added to the list.\n";
else
cout << "\a\nERROR: No Node added to the list." << endl;
return bSuccess;
}
bool DeleteTransaction(COList & List)
{
bool bSuccess = true;
if( List.ListIsEmpty() )
cout << "\a\nERROR: The list is empty!\a\n";
else
{
NodeId szNodeId;
int nPriority = 127;
cout << "\nEnter ID number of the node to remove: ";
cin >> szNodeId;
bSuccess = List.RemoveNodeFromList(szNodeId);
if ( bSuccess )
{
cout << "Node " << szNodeId << " removed from the list.\n";
}
else
cout << "\a\nERROR: The list does not contain a nodeID " << szNodeId << endl;
}
return bSuccess;
}
bool ListTransaction(COList & List)
{
bool bSuccess = List.DisplayNodesInList();
if ( bSuccess == false )
cout << "\a\nERROR: The list is empty!\n";
return bSuccess;
}
bool GetNodeInfo(PointerToData & pData)
{
/*
This method prompts the user for a Node ID(a string of up to ten characters) and a numeric priority. The priority number is then edited in order to make sure it is in the range 0 through 127. The Node ID and the priority number are then stored in a dynamically allocated Data object. The pointer to that Data object is passed back to the calling method. Note that the string input is not fool proof: if the user enters more than ten characters, the program may crash!
*/
NodeId szNodeId;
int nPriority = 127;
bool bSuccess = false;
cout << "\nEnter ID number for the new node: ";
cin >> szNodeId;
cout << "\nEnter a positive priority number in range 0 - 127: ";
cin >> nPriority;
nPriority = (nPriority < 0 ? -nPriority : nPriority);
nPriority = (nPriority > 127 ? 127 : nPriority);
pData = new CData(szNodeId,nPriority);
if ( pData )
bSuccess = true;
else
cerr << "\n\aMemory Allocation Error for data object!\n\n";
return bSuccess;
}
----------------------------------------------------------
CDATA.cpp
#include <iostream.h>
#include <string.h>
#include "CDATA.H"
#ifndef CDATA_CPP
#define CDATA_CPP
CData::CData(const char * pNewNodeId, int nNewPriority)
{
// cout << "... CData constructor (Id: " << pNewNodeId << "\n";
if (pNewNodeId)
strcpy(szNodeId,pNewNodeId);
else
{
cerr << "\nInvalid Node Id passed to CData object!\n";
strcpy(szNodeId,"INVALID ID"
}
SetPriority(nNewPriority);
}
CData::~CData()
{
// cout << "... CData destructor (Id: " << szNodeId << "\n";
}
const char * CData::GetId() const
{
return szNodeId;
}
int CData::GetPriority() const
{
return nPriority;
}
void CData::SetPriority(int nNewPriority)
{
nPriority = nNewPriority;
return;
}
bool CData:perator == (const CData & OtherObject)
{
return nPriority == OtherObject.nPriority;
}
bool CData:perator != (const CData & OtherObject)
{
return nPriority != OtherObject.nPriority;
}
bool CData:perator < (const CData & OtherObject)
{
return nPriority < OtherObject.nPriority;
}
bool CData:perator <= (const CData & OtherObject)
{
return nPriority <= OtherObject.nPriority;
}
bool CData:perator > (const CData & OtherObject)
{
return nPriority > OtherObject.nPriority;
}
bool CData:perator >= (const CData & OtherObject)
{
return nPriority >= OtherObject.nPriority;
}
#endif
----------------------------------------------------------
CData.h
#ifndef CDATA_H
#define CDATA_H
const short int NODEID_LENGTH = 11;
typedef char NodeId [NODEID_LENGTH];
class CData
{
protected:
NodeId szNodeId;
int nPriority;
public:
CData(const char * pNewNodeId, int nNewPriority);
~CData();
const char * GetId() const;
int GetPriority() const;
void SetPriority(int nNewPriority);
bool operator == (const CData & OtherObject);
bool operator != (const CData & OtherObject);
bool operator < (const CData & OtherObject);
bool operator <= (const CData & OtherObject);
bool operator > (const CData & OtherObject);
bool operator >= (const CData & OtherObject);
};
typedef CData * PointerToData;
#endif
----------------------------------------------------------
CNode.cpp
#ifndef CNODE_CPP
#define CNODE_CPP
#include <iostream.h>
#include "CData.h"
#include "CNode.h"
CNode::CNode(const PointerToData pNewData)
{
// cout << "... CNode constructor (Id: " << pNewData->GetId() << "\n";
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}
CNode::~CNode()
{
// cout << "... CNode destructor (Id: " << pData->GetId() << "\n";
}
#endif
#ifndef CNODE_CPP
#define CNODE_CPP
#include <iostream.h>
#include "CData.h"
#include "CNode.h"
CNode::CNode(const PointerToData pNewData)
{
// cout << "... CNode constructor (Id: " << pNewData->GetId() << "\n";
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}
CNode::~CNode()
{
// cout << "... CNode destructor (Id: " << pData->GetId() << "\n";
}
#endif
#ifndef CNODE_CPP
#define CNODE_CPP
#include <iostream.h>
#include "CData.h"
#include "CNode.h"
CNode::CNode(const PointerToData pNewData)
{
// cout << "... CNode constructor (Id: " << pNewData->GetId() << "\n";
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}
CNode::~CNode()
{
// cout << "... CNode destructor (Id: " << pData->GetId() << "\n";
}
#endif
#ifndef CNODE_CPP
#define CNODE_CPP
#include <iostream.h>
#include "CData.h"
#include "CNode.h"
CNode::CNode(const PointerToData pNewData)
{
// cout << "... CNode constructor (Id: " << pNewData->GetId() << "\n";
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}
CNode::~CNode()
{
// cout << "... CNode destructor (Id: " << pData->GetId() << "\n";
}
#endif
#ifndef CNODE_CPP
#define CNODE_CPP
#include <iostream.h>
#include "CData.h"
#include "CNode.h"
CNode::CNode(const PointerToData pNewData)
{
// cout << "... CNode constructor (Id: " << pNewData->GetId() << "\n";
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}
CNode::~CNode()
{
// cout << "... CNode destructor (Id: " << pData->GetId() << "\n";
}
#endif
----------------------------------------------------------
CNode.h
#ifndef CNODE_H
#define CNODE_H
#include "CData.h"
class CNode; // needed for next typedef
typedef CNode * PointerToNode; // alias for a pointer to a Node
class CNode
{
friend class COList; friend class CQueue; public:
CNode(const PointerToData pNewData);// constructor function
~CNode(); // destructor function
protected:
PointerToData pData; // points to the data object
PointerToNode pPreviousNode; // points to the previous node
PointerToNode pNextNode; // points to the next node
};
#endif
#ifndef CNODE_H
#define CNODE_H
#include "CData.h"
class CNode; // needed for next typedef
typedef CNode * PointerToNode; // alias for a pointer to a Node
class CNode
{
friend class COList; // needed for lab 1 & 2
friend class CQueue; // needed for lab 2
public:
CNode(const PointerToData pNewData);// constructor function
~CNode(); // destructor function
protected:
PointerToData pData; // points to the data object
PointerToNode pPreviousNode; // points to the previous node
PointerToNode pNextNode; // points to the next node
};
#endif
#ifndef CNODE_H
#define CNODE_H
#include "CData.h"
class CNode; // needed for next typedef
typedef CNode * PointerToNode; // alias for a pointer to a Node
class CNode
{
friend class COList; // needed for lab 1 & 2
friend class CQueue; // needed for lab 2
public:
CNode(const PointerToData pNewData);// constructor function
~CNode(); // destructor function
protected:
PointerToData pData; // points to the data object
PointerToNode pPreviousNode; // points to the previous node
PointerToNode pNextNode; // points to the next node
};
#endif
----------------------------------------------------------
COList.cpp
#ifndef COLIST_CPP
#define COLIST_CPP
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include "CData.h"
#include "COList.h"
COList::COList()
{
// cout << "... COList constructor\n";
pHead = pTail = pCurrent = 0;
nNumberOfNodes = 0;
}
COList::~COList()
{
// cout << "... COList destructor\n";
while ( !ListIsEmpty() )
{
RemoveNodeFromList(pHead->pData->GetId());
}
}
int COList::GetNumberOfNodes() const
{
return nNumberOfNodes;
}
bool COList::AddNodeToList(PointerToData pData)
{
bool bSuccess = true;
// Can't figure this one out.
return bSuccess;
}
bool COList::RemoveNodeFromList(const char* szNodeId)
{
bool bSuccess = true;
//Can't figure this one out!
return bSuccess;
}
bool COList::ListIsEmpty() const
{
return nNumberOfNodes == 0;
}
bool COList:isplayNodesInList()
{
bool bSuccess = true;
if ( ListIsEmpty() )
bSuccess = false;
else
{
cout << "\nThere are currently " << nNumberOfNodes
<< " nodes in this List:\n\nNode ID Pri\n"
<< "========== ===\n";
PointerToNode pNode = pHead;
while ( pNode )
{
cout << setw(13) << setiosflags(ios::left)
<< pNode->pData->GetId()
<< setw(3) << setiosflags(ios::right)
<< pNode->pData->GetPriority() << endl;
pNode = pNode->pNextNode;
}
}
return bSuccess;
}
PointerToData COList::GetPointerToHeadData() const
{
return pHead->pData;
}
bool COList::IsNodeIdInList(const char *szNodeId)
{
bool bInList = false;
pCurrent = pHead;
for( int nNode = 0; nNode < nNumberOfNodes; nNode++ )
{
if( strcmp(pCurrent->pData->GetId(), szNodeId) == 0 )
{
bInList = true;
nNode = nNumberOfNodes;
}
else
pCurrent = pCurrent->pNextNode;
}
return bInList;
}
#endif
#ifndef COLIST_CPP
#define COLIST_CPP
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include "CData.h"
#include "COList.h"
COList::COList()
{
// cout << "... COList constructor\n";
pHead = pTail = pCurrent = 0;
nNumberOfNodes = 0;
}
COList::~COList()
{
// cout << "... COList destructor\n";
while ( !ListIsEmpty() )
{
RemoveNodeFromList(pHead->pData->GetId());
}
}
int COList::GetNumberOfNodes() const
{
return nNumberOfNodes;
}
bool COList::AddNodeToList(PointerToData pData)
{
bool bSuccess = true;
// To be completed by student.
return bSuccess;
}
bool COList::RemoveNodeFromList(const char* szNodeId)
{
bool bSuccess = true;
// To be completed by student.
return bSuccess;
}
bool COList::ListIsEmpty() const
{
return nNumberOfNodes == 0;
}
bool COList:isplayNodesInList()
{
bool bSuccess = true;
if ( ListIsEmpty() )
bSuccess = false;
else
{
cout << "\nThere are currently " << nNumberOfNodes
<< " nodes in this List:\n\nNode ID Pri\n"
<< "========== ===\n";
PointerToNode pNode = pHead;
while ( pNode )
{
cout << setw(13) << setiosflags(ios::left)
<< pNode->pData->GetId()
<< setw(3) << setiosflags(ios::right)
<< pNode->pData->GetPriority() << endl;
pNode = pNode->pNextNode;
}
}
return bSuccess;
}
PointerToData COList::GetPointerToHeadData() const
{
return pHead->pData;
}
bool COList::IsNodeIdInList(const char *szNodeId)
{
bool bInList = false;
pCurrent = pHead;
for( int nNode = 0; nNode < nNumberOfNodes; nNode++ )
{
if( strcmp(pCurrent->pData->GetId(), szNodeId) == 0 )
{
bInList = true;
nNode = nNumberOfNodes;
}
else
pCurrent = pCurrent->pNextNode;
}
return bInList;
}
#endif
----------------------------------------------------------
COList.h
#ifndef COLIST_H
#define COLIST_H
#include "CData.h"
#include "CNode.h"
class COList
{
public:
COList();
~COList();
int GetNumberOfNodes() const;
bool AddNodeToList(PointerToData);
bool RemoveNodeFromList(const char* szNodeId);
bool ListIsEmpty() const;
bool DisplayNodesInList();
PointerToData GetPointerToHeadData() const;
bool IsNodeIdInList(const char* szNodeId);
protected:
int nNumberOfNodes;
PointerToNode pHead;
PointerToNode pTail;
PointerToNode pCurrent;
};
#endif
#ifndef COLIST_H
#define COLIST_H
#include "CData.h"
#include "CNode.h"
class COList
{
public:
COList();
~COList();
int GetNumberOfNodes() const;
bool AddNodeToList(PointerToData);
bool RemoveNodeFromList(const char* szNodeId);
bool ListIsEmpty() const;
bool DisplayNodesInList();
PointerToData GetPointerToHeadData() const;
bool IsNodeIdInList(const char* szNodeId);
protected:
int nNumberOfNodes;
PointerToNode pHead;
PointerToNode pTail;
PointerToNode pCurrent;
};
#endif
----------------------------------------------------------
There, that is all I have done so far... I do have a memory checker.cpp file but it is not needed to solve this problem!! If anyone has any idea on how to set up a linked list to add and delete nodes from my "List Of Nodes" please help me out!!! Thank you
Chmilz