I am trying create my own Singly Linked list template. When I try to remove an element from the start it works fine for the first time, when it runs for second time, it get lost some time. I do not know what happens.
1: remove 2: reset
are causing the same error.
bool reset() {
Node<TYPE>* currentPtr = listStart;
if (!isEmpty()) {
while (currentPtr->nextPtr != nullptr) {
currentPtr = currentPtr->nextPtr;
listStart = currentPtr;
delete currentPtr->nextPtr;
}
delete currentPtr->nextPtr;
return true;
}
else
return false;
}
bool remove() {
if(!isEmpty()){
Node<TYPE>* temp = listStart;
listStart = temp->nextPtr;
delete temp->nextPtr;
return true;
}
else {
std::cout << "List is Already Empty";
return false;
}
}
private:
Node<TYPE>* listStart;
};```
User contributions licensed under CC BY-SA 3.0