I am having and issue getting my add to list function to work in my data structure list class. I have all functions working except addToPostion. addToHead, addToTail, deleteFromhead, deleteFromTail, and DeleteFromPosistion all work as expected. I'm confused on the addToPosistion. The entire .cpp file is posted. Any and all help would be greatly appreciated along with any ideas on improving the overall code.
Problem Area
//*****************************************************************
// METHOD: addByPosistion
// PURPOSE: Adds a Node to some posistion in the list
// PARAMETERS: N/A
//*****************************************************************
void SalesmenList::addByPosition(int index, Salesmen *pItem)
{
if (index < 0)
{
return; // Invalid index
}
// Empty List
if (pHead == 0)
{
return;
}
// List is size of 1
if (pHead == pTail)
{
if (index !=0)
{
return;
}
// addToHead(Salesmen *pItem);
return;
}
// Multiple Items in List
if (index == 0)
{
pHead = pTail = pItem;
return;
}
Salesmen *pTemp = pHead;
int tempIndex = 0;
Salesmen *pPrev = pHead;
while (pTemp != NULL)
{
if (tempIndex == index)
{
// Add Item to List
pPrev->setNext(pTemp->getNext());
if (pTemp == pTail)
{
pTail = pPrev;
}
delete pTemp;
count--;
return;
}
pPrev = pTemp;
tempIndex++;
pTemp = pTemp->getNext();
}
}
The rest of the .cpp File
//=================================================================
#include "SalesmenList.h"
#include "Salesmen.h"
#include <iostream>
//=================================================================
//*****************************************************************
// METHOD Constructor
// PURPOSE: Constructor
// PARAMETERS: pHead, pTail list_size
//*****************************************************************
SalesmenList::SalesmenList()
{
pHead = pTail = 0;
count = 0;
}
//*****************************************************************
// METHOD: Destructor
// PURPOSE: Destructor
// PARAMETERS: N/A
//*****************************************************************
SalesmenList::~SalesmenList()
{
Salesmen *p = pHead;
while ( p != NULL )
{
p = p->getNext();
delete p;
}
count = 0;
}
//*****************************************************************
// METHOD: IsInList
// PURPOSE: Check if List is free
// PARAMETERS: N/A
//*****************************************************************
bool SalesmenList::isInList(Salesmen *pItem) const
{
Salesmen *pTemp;
pTemp = pHead;
while ( pTemp != NULL )
{
if ( pTemp->getName() == pItem->getName() )
{
break;
}
pTemp = pTemp->getNext();
}
return pTemp !=0;
}
//*****************************************************************
// METHOD: deleteFromHead
// PURPOSE: Inserts Salesmen at the front of the list
// PARAMETERS: N/A
//*****************************************************************
void SalesmenList::deleteFromHead()
{
Salesmen *pTemp = pHead;
if (pHead == pTail)
{
pHead = pTail = 0;
count = 0;
}
else
{
pHead = pHead->getNext();
count--;
}
delete pTemp;
}
//*****************************************************************
// METHOD: deleteByPosition
// PURPOSE: Remove Salesmen Object
// PARAMETERS: N/A
//*****************************************************************
void SalesmenList::deleteByPosition(int index)
{
if (index < 0)
{
return; // Invalid index
}
// Empty List
if (pHead == 0)
{
return;
}
// List is size of 1
if (pHead == pTail)
{
if (index !=0)
{
return;
}
delete pHead;
pHead = NULL;
pTail = NULL;
count = 0;
return;
}
// Multiple Items in List
if (index == 0)
{
deleteFromHead();
return;
}
Salesmen *pTemp = pHead;
int tempIndex = 0;
Salesmen *pPrev = pHead;
while (pTemp != NULL)
{
if (tempIndex == index)
{
// Delete item and readjust Next Pointer
pPrev->setNext(pTemp->getNext());
if (pTemp == pTail)
{
pTail = pPrev;
}
delete pTemp;
count--;
return;
}
pPrev = pTemp;
tempIndex++;
pTemp = pTemp->getNext();
}
}
//*****************************************************************
// METHOD: deleteFromTail
// PURPOSE: Deletes Salesmen at the tail of the list
// PARAMETERS: N/A
//*****************************************************************
void SalesmenList::deleteFromTail()
{
Salesmen *pTemp = pHead;
if (pTemp == NULL)
{
// Empty List
return;
}
if (pHead == pTail)
{
// Single item in List
delete pTail;
pHead = NULL;
pTail = NULL;
count = 0;
return;
}
// Multiple Items in List
Salesmen *pPrev = NULL;
while (pTemp != pTail)
{
pPrev = pTemp;
pTemp = pTemp->getNext();
}
delete pTemp;
pTail = pPrev;
pTail->setNext(NULL);
count--;
}
//*****************************************************************
// METHOD: addToHead
// PURPOSE: Inserts Salesmen at the front of the list
// PARAMETERS: N/A
//*****************************************************************
void SalesmenList::addToHead(Salesmen *pItem)
{
Salesmen *pTemp = pHead;
pHead = pItem;
pItem->setNext(pTemp);
count++;
}
//*****************************************************************
// METHOD: addByPosistion
// PURPOSE: Adds a Node to some posistion in the list
// PARAMETERS: N/A
//*****************************************************************
void SalesmenList::addByPosition(int index, Salesmen *pItem)
{
if (index < 0)
{
return; // Invalid index
}
// Empty List
if (pHead == 0)
{
return;
}
// List is size of 1
if (pHead == pTail)
{
if (index !=0)
{
return;
}
// addToHead(Salesmen *pItem);
return;
}
// Multiple Items in List
if (index == 0)
{
pHead = pTail = pItem;
return;
}
Salesmen *pTemp = pHead;
int tempIndex = 0;
Salesmen *pPrev = pHead;
while (pTemp != NULL)
{
if (tempIndex == index)
{
// Add Item to List
pPrev->setNext(pTemp->getNext());
if (pTemp == pTail)
{
pTail = pPrev;
}
delete pTemp;
count--;
return;
}
pPrev = pTemp;
tempIndex++;
pTemp = pTemp->getNext();
}
}
//*****************************************************************
// METHOD: addToTail
// PURPOSE: Inserts salesmen at the end of the list
// PARAMETERS: N/A
//*****************************************************************
void SalesmenList::addToTail(Salesmen *pItem)
{
if (pTail != 0)
{
pTail->setNext(pItem);
pTail = pItem;
}
else
{
pHead = pTail = pItem;
}
count++;
}
#if 0
//*****************************************************************
// METHOD: isEmpty
// PURPOSE: Inserts salesmen at the end of the list
// PARAMETERS: N/A
//*****************************************************************
int SalesmenList::isEmpty()
{
return pHead == 0;
}
#endif
//*****************************************************************
// METHOD: print
// PURPOSE: prints the current list of salesmen
// PARAMETERS: N/A
//*****************************************************************
void SalesmenList::print()
{
cout << "Debug: count = " << count << endl;
Salesmen *pCurr = pHead;
while ( pCurr != NULL )
{
pCurr->PrintToStream(cout);
pCurr = pCurr->getNext();
}
}