Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linked List.... 1

Status
Not open for further replies.

chmilz

Programmer
Jan 10, 2001
94
CA
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 &quot;COList.h&quot;
#include &quot;Memcheck.h&quot;

// 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
<< &quot;Please enter one of the following characters:\n\n&quot;
<< &quot;a Add data to the list.\n&quot;
<< &quot;d Delete data from the list.\n&quot;
<< &quot;l List (i.e. display) all the data in the list.\n&quot;
<< &quot;x Exit from the program.\n\n&quot;
<< &quot;Menu selection ===> &quot;;

cin >> cMenuChoice;
cMenuChoice = tolower(cMenuChoice); //convert to lower case and

while ( strchr(&quot;adlx&quot;,cMenuChoice) == 0 ) //accept letters a d l x only
{
cout << &quot;\nInvalid selection! Please enter a, d, l, or x.\n\n&quot;
<< &quot;Menu selection ===> &quot;;
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 << &quot;\n\aLOGIC ERROR! We should never get here!\n\n&quot;;
}

//for testing purposes only (set DEBUG to 1 in order to use it):
if ( bSuccess == false && DEBUG )
cerr << &quot;\a\nDEBUG WARNING: Transaction failed!\n&quot;;
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 << &quot;Node &quot; << pData->GetId() << &quot; added to the list.\n&quot;;
else
cout << &quot;\a\nERROR: No Node added to the list.&quot; << endl;

return bSuccess;
}

bool DeleteTransaction(COList & List)
{
bool bSuccess = true;

if( List.ListIsEmpty() )
cout << &quot;\a\nERROR: The list is empty!\a\n&quot;;
else
{
NodeId szNodeId;
int nPriority = 127;
cout << &quot;\nEnter ID number of the node to remove: &quot;;
cin >> szNodeId;
bSuccess = List.RemoveNodeFromList(szNodeId);

if ( bSuccess )
{
cout << &quot;Node &quot; << szNodeId << &quot; removed from the list.\n&quot;;
}
else
cout << &quot;\a\nERROR: The list does not contain a nodeID &quot; << szNodeId << endl;

}

return bSuccess;
}

bool ListTransaction(COList & List)
{
bool bSuccess = List.DisplayNodesInList();

if ( bSuccess == false )
cout << &quot;\a\nERROR: The list is empty!\n&quot;;

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 << &quot;\nEnter ID number for the new node: &quot;;
cin >> szNodeId;
cout << &quot;\nEnter a positive priority number in range 0 - 127: &quot;;
cin >> nPriority;
nPriority = (nPriority < 0 ? -nPriority : nPriority);
nPriority = (nPriority > 127 ? 127 : nPriority);
pData = new CData(szNodeId,nPriority);
if ( pData )
bSuccess = true;
else
cerr << &quot;\n\aMemory Allocation Error for data object!\n\n&quot;;
return bSuccess;
}
----------------------------------------------------------

CDATA.cpp

#include <iostream.h>
#include <string.h>
#include &quot;CDATA.H&quot;

#ifndef CDATA_CPP
#define CDATA_CPP

CData::CData(const char * pNewNodeId, int nNewPriority)
{
// cout << &quot;... CData constructor (Id: &quot; << pNewNodeId << &quot;)\n&quot;;
if (pNewNodeId)
strcpy(szNodeId,pNewNodeId);
else
{
cerr << &quot;\nInvalid Node Id passed to CData object!\n&quot;;
strcpy(szNodeId,&quot;INVALID ID&quot;);
}
SetPriority(nNewPriority);
}

CData::~CData()
{
// cout << &quot;... CData destructor (Id: &quot; << szNodeId << &quot;)\n&quot;;
}

const char * CData::GetId() const
{
return szNodeId;
}

int CData::GetPriority() const
{
return nPriority;
}

void CData::SetPriority(int nNewPriority)
{
nPriority = nNewPriority;
return;
}

bool CData::eek:perator == (const CData & OtherObject)
{
return nPriority == OtherObject.nPriority;
}

bool CData::eek:perator != (const CData & OtherObject)
{
return nPriority != OtherObject.nPriority;
}

bool CData::eek:perator < (const CData & OtherObject)
{
return nPriority < OtherObject.nPriority;
}

bool CData::eek:perator <= (const CData & OtherObject)
{
return nPriority <= OtherObject.nPriority;
}

bool CData::eek:perator > (const CData & OtherObject)
{
return nPriority > OtherObject.nPriority;
}

bool CData::eek: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 &quot;CData.h&quot;
#include &quot;CNode.h&quot;

CNode::CNode(const PointerToData pNewData)
{
// cout << &quot;... CNode constructor (Id: &quot; << pNewData->GetId() << &quot;)\n&quot;;
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}

CNode::~CNode()
{
// cout << &quot;... CNode destructor (Id: &quot; << pData->GetId() << &quot;)\n&quot;;
}

#endif
#ifndef CNODE_CPP
#define CNODE_CPP

#include <iostream.h>
#include &quot;CData.h&quot;
#include &quot;CNode.h&quot;

CNode::CNode(const PointerToData pNewData)
{
// cout << &quot;... CNode constructor (Id: &quot; << pNewData->GetId() << &quot;)\n&quot;;
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}

CNode::~CNode()
{
// cout << &quot;... CNode destructor (Id: &quot; << pData->GetId() << &quot;)\n&quot;;
}

#endif
#ifndef CNODE_CPP
#define CNODE_CPP

#include <iostream.h>
#include &quot;CData.h&quot;
#include &quot;CNode.h&quot;

CNode::CNode(const PointerToData pNewData)
{
// cout << &quot;... CNode constructor (Id: &quot; << pNewData->GetId() << &quot;)\n&quot;;
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}

CNode::~CNode()
{
// cout << &quot;... CNode destructor (Id: &quot; << pData->GetId() << &quot;)\n&quot;;
}

#endif

#ifndef CNODE_CPP
#define CNODE_CPP

#include <iostream.h>
#include &quot;CData.h&quot;
#include &quot;CNode.h&quot;

CNode::CNode(const PointerToData pNewData)
{
// cout << &quot;... CNode constructor (Id: &quot; << pNewData->GetId() << &quot;)\n&quot;;
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}

CNode::~CNode()
{
// cout << &quot;... CNode destructor (Id: &quot; << pData->GetId() << &quot;)\n&quot;;
}

#endif

#ifndef CNODE_CPP
#define CNODE_CPP

#include <iostream.h>
#include &quot;CData.h&quot;
#include &quot;CNode.h&quot;

CNode::CNode(const PointerToData pNewData)
{
// cout << &quot;... CNode constructor (Id: &quot; << pNewData->GetId() << &quot;)\n&quot;;
pData = pNewData;
pPreviousNode = 0;
pNextNode = 0;
}

CNode::~CNode()
{
// cout << &quot;... CNode destructor (Id: &quot; << pData->GetId() << &quot;)\n&quot;;
}

#endif
----------------------------------------------------------
CNode.h

#ifndef CNODE_H
#define CNODE_H

#include &quot;CData.h&quot;

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 &quot;CData.h&quot;

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 &quot;CData.h&quot;

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 &quot;CData.h&quot;
#include &quot;COList.h&quot;

COList::COList()
{
// cout << &quot;... COList constructor\n&quot;;
pHead = pTail = pCurrent = 0;
nNumberOfNodes = 0;
}

COList::~COList()
{
// cout << &quot;... COList destructor\n&quot;;

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::DisplayNodesInList()
{
bool bSuccess = true;

if ( ListIsEmpty() )
bSuccess = false;
else
{
cout << &quot;\nThere are currently &quot; << nNumberOfNodes
<< &quot; nodes in this List:\n\nNode ID Pri\n&quot;
<< &quot;========== ===\n&quot;;
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 &quot;CData.h&quot;
#include &quot;COList.h&quot;

COList::COList()
{
// cout << &quot;... COList constructor\n&quot;;
pHead = pTail = pCurrent = 0;
nNumberOfNodes = 0;
}

COList::~COList()
{
// cout << &quot;... COList destructor\n&quot;;

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::DisplayNodesInList()
{
bool bSuccess = true;

if ( ListIsEmpty() )
bSuccess = false;
else
{
cout << &quot;\nThere are currently &quot; << nNumberOfNodes
<< &quot; nodes in this List:\n\nNode ID Pri\n&quot;
<< &quot;========== ===\n&quot;;
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 &quot;CData.h&quot;
#include &quot;CNode.h&quot;

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 &quot;CData.h&quot;
#include &quot;CNode.h&quot;

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 &quot;List Of Nodes&quot; please help me out!!! Thank you

Chmilz
 
Hi
You can find information on programming linked lists in every book about algorithms and data structures. Most of them even have code examples. ;-)
 
Thank you very much for your help NitramR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top