Hi,
I am currently working on a linked list program which will allow a user to add and delete items from a list. I have the "Add" function complete but I am having troubles with the delete function.. Would anyone be able to help me??? I will post my code from the "Add" function here:
bool COList::AddNodeToList(PointerToData pData)
{
bool bSuccess = true;
if(IsNodeIdInList(pData->GetId()))
{
bSuccess = false;
cerr << "\nDuplicate Node ID is not allowed." << endl;
delete pData;
pData = NULL;
}
else
{
int nPriority = pData->GetPriority();
PointerToNode pNewNode = new CNode(pData);
if (!pNewNode)
{
cerr <<"\nMemory Allocation failure in AddNodeToList()" << endl;
bSuccess = false;
}
else
{
if(ListIsEmpty())
{
pHead = pNewNode;
pTail = pNewNode;
}
else if(nPriority < pHead->pData->GetPriority())
{
pNewNode->pNextNode = pHead;
pHead->pPreviousNode = pNewNode;
pHead = pNewNode;
}
else if (nPriority >= pTail->pData->GetPriority())
{
pNewNode->pPreviousNode = pTail;
pTail->pNextNode = pNewNode;
pTail = pNewNode;
}
else
{
pCurrent = pHead;
for(int nIndex = 0; nIndex < nNumberOfNodes; nIndex++)
{
if (nPriority >= pCurrent->pData->GetPriority()
&& nPriority < pCurrent->pNextNode->pData->GetPriority())
{
pNewNode->pPreviousNode = pCurrent->pNextNode->pPreviousNode;
pCurrent->pNextNode->pPreviousNode = pNewNode;
pNewNode->pNextNode = pCurrent->pNextNode;
pCurrent->pNextNode = pNewNode;
nIndex = nNumberOfNodes;
}
else
pCurrent = pCurrent->pNextNode;
}
}
nNumberOfNodes++;
}
}
return bSuccess;
}
Any help on how I go about deleting an item would be great. Thanks!
I am currently working on a linked list program which will allow a user to add and delete items from a list. I have the "Add" function complete but I am having troubles with the delete function.. Would anyone be able to help me??? I will post my code from the "Add" function here:
bool COList::AddNodeToList(PointerToData pData)
{
bool bSuccess = true;
if(IsNodeIdInList(pData->GetId()))
{
bSuccess = false;
cerr << "\nDuplicate Node ID is not allowed." << endl;
delete pData;
pData = NULL;
}
else
{
int nPriority = pData->GetPriority();
PointerToNode pNewNode = new CNode(pData);
if (!pNewNode)
{
cerr <<"\nMemory Allocation failure in AddNodeToList()" << endl;
bSuccess = false;
}
else
{
if(ListIsEmpty())
{
pHead = pNewNode;
pTail = pNewNode;
}
else if(nPriority < pHead->pData->GetPriority())
{
pNewNode->pNextNode = pHead;
pHead->pPreviousNode = pNewNode;
pHead = pNewNode;
}
else if (nPriority >= pTail->pData->GetPriority())
{
pNewNode->pPreviousNode = pTail;
pTail->pNextNode = pNewNode;
pTail = pNewNode;
}
else
{
pCurrent = pHead;
for(int nIndex = 0; nIndex < nNumberOfNodes; nIndex++)
{
if (nPriority >= pCurrent->pData->GetPriority()
&& nPriority < pCurrent->pNextNode->pData->GetPriority())
{
pNewNode->pPreviousNode = pCurrent->pNextNode->pPreviousNode;
pCurrent->pNextNode->pPreviousNode = pNewNode;
pNewNode->pNextNode = pCurrent->pNextNode;
pCurrent->pNextNode = pNewNode;
nIndex = nNumberOfNodes;
}
else
pCurrent = pCurrent->pNextNode;
}
}
nNumberOfNodes++;
}
}
return bSuccess;
}
Any help on how I go about deleting an item would be great. Thanks!