Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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::QueueNode::QueueNode(const DataType& nodeData, QueueNode* nextPtr)

{

dataItem = nodeData;

next = nextPtr;

}

template

QueueLinked::QueueLinked(int maxNumber = Queue::MAX_QUEUE_SIZE)

{

front = NULL;

back = NULL;

}

template

QueueLinked::QueueLinked(const QueueLinked& other)

{

front = NULL;

back = NULL;

this = other;

}

template

QueueLinked& QueueLinked::operator=(const QueueLinked& other)

{

QueueNode * temp = other.front;

while (temp != NULL)

{

insert(temp->dataItem);

temp = temp->next;

}

return *this;

}

template

QueueLinked::~QueueLinked()

{

clear();

}

template

void QueueLinked::enqueue(const DataType& newDataItem) throw (logic_error)

{

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::dequeue() throw (logic_error)

{

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::clear()

{

while (front != NULL)

dequeue();

front = NULL;

back = NULL;

}

template

bool QueueLinked::isEmpty() const

{

if (front == NULL)

return true;

else

return false;

}

template

bool QueueLinked::isFull() const

{

return false;

}

template

void QueueLinked::putFront(const DataType& newDataItem) throw (logic_error)

{

if (!isFull())

{

front = new QueueNode(newDataItem, front);

if (front->next == NULL)

back = front;

}

else

throw logic_error("Linked list is full");

}

template

DataType QueueLinked::getRear() throw (logic_error)

{

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::getLength() const

{

int a = 0;

QueueNode* temp = front;

while (temp != NULL)

{

temp = temp->next;

a++;

}

return a;

}

template

void QueueLinked::showStructure() const

{

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 #include

using namespace std;

#include "Queue.h"

template class QueueArray : public Queue { public: QueueArray(int maxNumber = Queue::MAX_QUEUE_SIZE); QueueArray(const QueueArray& other); QueueArray& operator=(const QueueArray& other); ~QueueArray();

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& testQueue)

//void test_queue(Queue& testQueue)

{

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 s1;

test_queue(s1);

#else

cout << "Testing linked implementation" << endl;

QueueLinked s2;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions