Deleting nodes in a queue with linked lists
Oct 23, 2014 at 7:10am UTC
Hi, I can't for the life of me figure out this memory access error! I have highlighted what I think is causing the error, but it could also be the while loop that follows it, so any feedback is appreciated! The program also stops when the head of the queue is deleted, but I'm certain my dequeue function is not the error. Please help!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
int queue::play(queue& game)
{
game.readNamesAndQ();
game.ptr = game.head;
int rounds = randomGenerator(LIMIT);
int m_length = 15;
while (m_length >= 0)
{
qNode *q, *delPtr;
try
{
q = new qNode;
}
catch (bad_alloc)
{
cerr << "Bad Allocation" ;
return -1; // -1 means error!
}
q = game.head;
delPtr = game.head;
game.ptr = game.head;
/**************************************/
while (rounds >= 0)
{
rounds--;
delPtr = game.ptr;
game.ptr = game.ptr->next;
if (game.ptr == NULL)
game.ptr = head;
}
/**************************************/
//print the queue
while (q != NULL)
{
cout << q->name << endl;
q = q->next;
}
// print the one without a chair
cout << endl << "The child without a chair is: " << ptr->name << endl;
if (game.ptr == game.head)
game.head == game.head->next;
q = game.ptr;
delPtr->next = q->next;
delete q;
m_length--;
}
}
Oct 23, 2014 at 10:44am UTC
Line 6: rounds
is independant from the amount of elements in your list, but it shouldn't
Line 32: What happens when game.ptr
is NULL before this line?
Line 48: What happens when game.ptr
and game.head
are NULL?
Topic archived. No new replies allowed.