I created a linked list called ScanList containing nodes of object Scan. But when I remove the first Scan from the linked list and close the program, I get an exception of type "Access violation reading location" at the destructor of ScanList.
The function where I remove Scan from the ScanList is below.
bool ScanList::removeScan(int serialNumber)
{// post: IF serialNumber is present in the list
// THEN scan has been removed from it and true has been returned
// ELSE false has been returned
Scan* currentScan = scans;
Scan* previousScan = scans;
if (currentScan != NULL && currentScan->getSerialNumber() == serialNumber)
{ // if serialNumber is the first element of the linked list
*scans = currentScan->getNext();
delete currentScan;
return true;
}
while (currentScan != NULL && currentScan->getSerialNumber() != serialNumber)
{
*previousScan = currentScan;
currentScan = currentScan->getNext();
}
if (currentScan == NULL)
{ // serialNumber is not present in the list
return false;
}
// serialNumber is present in the list and is removed
previousScan->setNext(currentScan->getNext());
delete currentScan;
return true;
}
I use a Copy assignment operator, but even then it removes scans. It does return the correct values, but when I delete the currentScan it also removes the whole Scan* scans of the ScanList. This is my copy assignment operator.
Scan* Scan::operator=(const Scan* scan)
{
// check if the Scan on the left is the same as Scan on the right
if (this == scan)
{ // if they are the same, go out of the function and return
return this;
}
this->serialNumber = scan->serialNumber;
this->timesRecycled = scan->timesRecycled;
this->next = scan->next;
return this;
}
Can anyone tell me what I am doing wrong?