Question
I am getting a weird error when I try get the rear number from my linked list. The error is. Access violation reading location 0xDDDDDDDD.
I am getting a weird error when I try get the rear number from my linked list. The error is. Access violation reading location 0xDDDDDDDD.
My code is
#include "QueueLinked.h"
template
QueueLinked
{
dataItem = nodeData;
next = nextPtr;
}
template
QueueLinked
{
front = NULL;
back = NULL;
}
template
QueueLinked
{
front = NULL;
back = NULL;
this = other;
}
template
QueueLinked
{
QueueNode * temp = other.front;
while (temp != NULL)
{
insert(temp->dataItem);
temp = temp->next;
}
return *this;
}
template
QueueLinked
{
clear();
}
template
void QueueLinked
{
if (!isFull())
if (!isEmpty())
back = back->next = new QueueNode(newDataItem, NULL);
else
front = back = new QueueNode(newDataItem, NULL);
else
throw logic_error("Linked list is full");
}
template
DataType QueueLinked
{
if (isEmpty())
throw logic_error("dequeue() while queue empty");
else
{
DataType dTemp = front->dataItem;
if (front == back)
{
delete front;
front = back = NULL;
}
else
{
QueueNode* temp = front;
front = front->next;
delete front;
}
return dTemp;
}
}
template
void QueueLinked
{
while (front != NULL)
dequeue();
front = NULL;
back = NULL;
}
template
bool QueueLinked
{
if (front == NULL)
return true;
else
return false;
}
template
bool QueueLinked
{
return false;
}
template
void QueueLinked
{
if (!isFull())
{
front = new QueueNode(newDataItem, front);
if (front->next == NULL)
back = front;
}
else
throw logic_error("Linked list is full");
}
template
DataType QueueLinked
{
if (isEmpty())
throw logic_error("getRear() while queue empty");
else
{
DataType dTemp = back->dataItem;
if (back != front)
{
QueueNode* temp = front;
while (temp->next != back)
temp = temp->next;
delete back;
back = temp;
}
else
{
delete back;
back = front = NULL;
}
return dTemp;
}
}
template
int QueueLinked
{
int a = 0;
QueueNode* temp = front;
while (temp != NULL)
{
temp = temp->next;
a++;
}
return a;
}
template
void QueueLinked
{
QueueNode *p; // Iterates through the queue
if ( isEmpty() )
cout << "Empty queue" << endl;
else
{
cout << "Front\t";
for ( p = front ; p != 0 ; p = p->next )
{
if( p == front )
{
cout << '[' << p->dataItem << "] ";
}
else
{
cout << p->dataItem << " ";
}
}
cout << "\trear" << endl;
}
}
.h file
// QueueArray.h
#ifndef QUEUEARRAY_H #define QUEUEARRAY_H
#include
using namespace std;
#include "Queue.h"
template
void enqueue(const DataType& newDataItem) throw (logic_error); DataType dequeue() throw (logic_error);
void clear();
bool isEmpty() const; bool isFull() const;
void putFront(const DataType& newDataItem) throw (logic_error); DataType getRear() throw (logic_error); int getLength() const;
void showStructure() const;
private: int maxSize; int front; int back; DataType* dataItems; };
#endif
test file
//--------------------------------------------------------------------
//
// Laboratory 7 test7.cpp
//
// Test program for the operations in the Queue ADT
//
//--------------------------------------------------------------------
#include
#include "config.h"
using namespace std;
#if LAB7_TEST1
# include "QueueLinked.cpp"
#else
# include "QueueArray.cpp"
#endif
//--------------------------------------------------------------------
void print_help();
//--------------------------------------------------------------------
template
void test_queue(Queue
//void test_queue(Queue
{
char cmd; // Input command
char testData; // Queue data item
print_help();
do
{
try {
testQueue.showStructure(); // Output queue
cout << endl << "Command: "; // Read command
cin >> cmd;
if ( cmd == '+' || cmd == '>' )
cin >> testData;
switch ( cmd )
{
case 'H' : case 'h' :
print_help();
break;
case '+' : // enqueue
cout << "Enqueue " << testData << endl;
testQueue.enqueue(testData);
break;
case '-' : // dequeue
cout << "Dequeued " << testQueue.dequeue() << endl;
break;
case 'C' : case 'c' : // clear
cout << "Clear the queue" << endl;
testQueue.clear();
break;
case 'E' : case 'e' : // empty
if ( testQueue.isEmpty() )
cout << "Queue is empty" << endl;
else
cout << "Queue is NOT empty" << endl;
break;
case 'F' : case 'f' : // full
if ( testQueue.isFull() )
cout << "Queue is full" << endl;
else
cout << "Queue is NOT full" << endl;
break;
#if LAB7_TEST2
case '>' : // Programming Exercise 2
cout << "Put " << testData << " in front " << endl;
testQueue.putFront(testData);
break;
case '=' : // Programming Exercise 2
cout << "Got " << testQueue.getRear() << " from rear "
<< endl;
break;
#endif
#if LAB7_TEST3
case '#' : // Programming Exercise 3
cout << "Length = " << testQueue.getLength() << endl;
break;
#endif
case 'Q' : case 'q' : // Quit test program
break;
default : // Invalid command
cout << "Inactive or invalid command" << endl;
}
}
catch (logic_error e) {
cout << "Error: " << e.what() << endl;
}
}
while ( cin && cmd != 'Q' && cmd != 'q' );
if( !cin ) {
cout << "input error" << endl;
}
}
//--------------------------------------------------------------------
int main()
{
#if !LAB7_TEST1
cout << "Testing array implementation" << endl;
QueueArray
test_queue(s1);
#else
cout << "Testing linked implementation" << endl;
QueueLinked
test_queue(s2);
#endif
return 0;
}
//--------------------------------------------------------------------
void print_help()
{
cout << endl << "Commands:" << endl;
cout << " H : Help (displays this message)" << endl;
cout << " +x : Enqueue x" << endl;
cout << " - : Dequeue" << endl;
cout << " C : Clear the queue" << endl;
cout << " E : Empty queue?" << endl;
cout << " F : Full queue?" << endl;
cout << " >x : Put x at front ("
#if LAB7_TEST2
<< " Active "
#else
<< "Inactive "
#endif
<< ": Programming Exercise 2)"
<< endl;
cout << " = : Get x from rear ("
#if LAB7_TEST2
<< " Active "
#else
<< "Inactive "
#endif
<< ": Programming Exercise 2)"
<< endl;
cout << " # : Length ("
#if LAB7_TEST3
<< " Active "
#else
<< "Inactive "
#endif
<< ": Programming Exercise 3)"
<< endl;
cout << " Q : Quit the test program" << endl;
cout << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started