i have a couple of should be simple linked list questions one of which is how to make it insert in a sorted fashion i have a way i think should work but it doesnt i can post the code if needed. also i have to delete the last n amount of items of the list i also have some code that i think should work but it will delete the last n and for some reason an extra item and i cant figure out why any ideas
bool LinkedList::insert(elementType elt)
{
nodePtr currPtr = head;
nodePtr prevPtr = head;
int num = elt;
nodePtr insertPtr = getNode(elt);
if (insertPtr == NULL)
return false;
if (head == NULL)
head = insertPtr;
else
{/*
while (currPtr->item < elt)
{
prevPtr = currPtr;
currPtr = currPtr->next;
}
insertPtr = currPtr;
prevPtr->next = insertPtr;*/
insertPtr->next = head;
head = insertPtr;
}
return true;
}
void LinkedList::deleteLastN(int n)
{
nodePtr currPtr = head;
nodePtr prevPtr = head;
for (int i = 0; i < (size() - n); i++)
{
currPtr = currPtr->next;
}
while (currPtr != NULL)
{
prevPtr = currPtr;
currPtr = currPtr->next;
delete prevPtr;
}
}