I'm trying to create a two sided queue with c++. I'm using Visual Studio 2012 and keep getting:
First-chance exception at 0x00D95A29 in Console_Assignment1.exe: 0xC0000005: Access violation writing location 0x00000008.
I think i'm having a pointer issue (might be trying to dereference something i shouldn't). so far i have had zero luck finding the problem and i'd really appreciate a second look.
(The code is too long to paste so i'll just copy the functions i think are giving me the problem.) Maybe just a small overview. I have a node class that holds two pointer to node (next and previous) and an int (value). and a queue class that holds two pointers to node (first and last) and an int (size).
// enqueueBeg - adds a new node at the beginning of the queue.
void DBL_Queue::enqueueBeg(int insert_val)
{
node* new_node = new node(insert_val); // Creates the new node.
new_node->setNext( this->getFirst() ); // Connects the new node to the first in the queue
this->getFirst()->setPrev( new_node ); // Connects the first node in the queue to the new one
this->setFirst( new_node ); // Sets the new node as the first in the queue
this->setSize ( this->get_queue_size() + 1 ); // adds 1 to the size of the list
// dequeueBeg - removes the first node of the queue.
int DBL_Queue::dequeueBeg()
{
int ret_value = this->getFirst()->getVal();
node* old_node = this->getFirst();
this->setFirst( this->getFirst()->getNext() ); // Sets the second node in the queue as the first.
this->getFirst()->setPrev( NULL ); // Removes the link between the new first new and the old one.
this->setSize( this->get_queue_size() - 1); // Removes 1 from queue size
delete old_node; // Deletes the node that use to be first.
return ret_value; // Returns the value of the old node.
// DBL_Queue Destructor
DBL_Queue::~DBL_Queue()
{
if (this->first == NULL) // if queue is empty do nothing
return;
else
{
while (this->first->getNext() != NULL) // go through all nodes and delete them one by one
{
node* deletion = this->getFirst();
this->setFirst( this->getFirst()->getNext() );
delete deletion;
}
}
}
Thanks in advance for the help!
std::deque? - Jerry Coffinthis->getFirst()returnsNULL)? You have similar problems with the dequeueing function. - Some programmer dude