Question: DATA STRUCTURES (C++) Evaluate code in Post Fix expression using: queue1.enqueue(1) queue1.enqueue(2) queue2.enqueue(3) queue2.enqueue(4) queue1.dequeue() queueFront = queue2.peekFront() queue1.enqueue(queueFront) queue1.enqueue(5) queue2.dequeue() queue2.enqueue(6) CODE: #include using

DATA STRUCTURES (C++)

Evaluate code in Post Fix expression using:

queue1.enqueue(1) queue1.enqueue(2) queue2.enqueue(3) queue2.enqueue(4) queue1.dequeue() queueFront = queue2.peekFront() queue1.enqueue(queueFront) queue1.enqueue(5) queue2.dequeue() queue2.enqueue(6)

CODE:

#include using namespace std;

template class QueueInterface { public: virtual bool isEmpty() const = 0; virtual bool enqueue(const ItemType& newEntry) = 0; virtual bool dequeue() = 0; virtual ItemType peekFront() const = 0; }; // end QueueInterface

//#endif

class PrecondViolatedExcep:public logic_error

{ public: PrecondViolatedExcep(const string& message = ""); }; // end PrecondViolatedExcep

/////////////////////////////////////////////////

/** @file PrecondViolatedExcep.cpp */ PrecondViolatedExcep::PrecondViolatedExcep(const string& message): logic_error("Precondition Violated Exception: " + message) {

} // end constructor

///////////////////////////// const int MAX_QUEUE = 3; template class ArrayQueue : public QueueInterface { private: ItemType items[MAX_QUEUE]; // Array of queue items int front; // Index to front of queue int back; // Index to back of queue int count; // Number of items currently in the queue public: ArrayQueue(); // Copy constructor and destructor supplied by compiler bool isEmpty() const; bool enqueue(const ItemType& newEntry); bool dequeue(); /** @throw PrecondViolatedExcep if queue is empty. */ ItemType peekFront() const throw(PrecondViolatedExcep); }; // end ArrayQueue

template

ArrayQueue::ArrayQueue() : front(0), back(MAX_QUEUE - 1), count(0) { } // end default constructor

template

bool ArrayQueue::isEmpty() const { return count == 0; } // end isEmpty

template bool ArrayQueue::enqueue(const ItemType& newEntry) { bool result = false; if (count < MAX_QUEUE) { // Queue has room for another item back = (back + 1) % MAX_QUEUE; items[back] = newEntry; count++; result = true; } // end if return result; } // end enqueue

template bool ArrayQueue::dequeue() { bool result = false; if (!isEmpty()) { front = (front + 1) % MAX_QUEUE; count--; result = true; } // end if return result; } // end dequeue

template ItemType ArrayQueue::peekFront() const throw(PrecondViolatedExcep) { // Enforce precondition if (isEmpty()) throw PrecondViolatedExcep("peekFront() called with empty queue"); // Queue is not empty; return front return items[front]; }

int main(){ ArrayQueue queue1; int queueFront; queue1.enqueue(9); queue1.dequeue(); queue1.enqueue(7); queue1.enqueue(6); queue1.enqueue(3); queue1.enqueue(0); queue1.enqueue(2); queue1.enqueue(4); queueFront = queue1.peekFront(); queue1.enqueue(queueFront); cout << queue1.peekFront() << endl; system("pause"); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!