Im trying to delete a node in a linked list in c++ but it keeps saying that _pending_tx is not nullptr after deleting transaction. Also valgrind says I have a memory leak and I can't figure it out
bool BankData::void_transaction(const size_t& customer_id, const size_t& timestamp) {
bool var8 = false;
if (transaction_exists(customer_id, timestamp) == true)
{
int blah3 = customerVector.size();
for (int i = 0; i < blah3; i++)
{
if (customerVector.at(i)._customer_id == customer_id)
{
if (customerVector.at(i)._pending_txs != nullptr)
{
CSE250::dTransactionNode *temp = customerVector.at(i)._pending_txs;
while (temp->_tx._timestamp != timestamp)
{
temp = temp->_next;
if ((temp->_next == nullptr) && (temp->_tx._timestamp != timestamp))
{
var8 = false;
}
}
if (temp->_tx._timestamp == timestamp)
{
if ((temp->_prev == nullptr) && (temp->_next == nullptr)) //head and only node
{
delete customerVector.at(i)._pending_txs;
customerVector.at(i)._pending_txs = nullptr; //after i delete the head and only node i reset it to a nullptr
var8 = true;
}
else if ((temp->_prev == nullptr) && (temp->_next != nullptr)) //head
{
temp = customerVector.at(i)._pending_txs->_next;
delete customerVector.at(i)._pending_txs;
customerVector.at(i)._pending_txs = temp;
customerVector.at(i)._pending_txs->_prev = nullptr;
var8 = true;
}
else if ((temp->_prev != nullptr) && (temp->_next == nullptr)) //tail
{
temp = customerVector.at(i)._pending_txs->_prev;
delete customerVector.at(i)._pending_txs;
customerVector.at(i)._pending_txs = temp;
customerVector.at(i)._pending_txs->_next = nullptr;
var8 = true;
}
else //middle node
{
temp = customerVector.at(i)._pending_txs->_next;
customerVector.at(i)._pending_txs->_next->_prev = customerVector.at(i)._pending_txs->_prev;
delete customerVector.at(i)._pending_txs;
customerVector.at(i)._pending_txs = temp;
//temp->_prev->_next = temp->_next;
//temp->_next->_prev = temp->_prev;
//temp = nullptr;
//delete temp;
var8 = true;
}
}
}
}
}
}
return var8;
This is the node struct that I'm trying to delete:
namespace CSE250 {
struct dTransactionNode;}
struct CSE250::dTransactionNode {
Transaction _tx;
dTransactionNode* _prev;
dTransactionNode* _next;
dTransactionNode(size_t time, double amount) : _tx(time, amount), _prev(nullptr), _next(nullptr) { }};
I also can't figure out why when i try to delete it, it will only delete the timestamp and not the timestamp and amount. so when I run my transaction exists function it still says part of the transaction exists.