Cairo University**We aren't endorsed by this school
Course
CMP 202
Subject
Computer Science
Date
Dec 20, 2024
Pages
3
Uploaded by PresidentMusicPorcupine40
circularQueue.h// File:circularQueue.h // Definition of Simple circular Queue Template Class template <class Type> class Queue { public: Queue(int nelements = 128); // Constructor Queue (const Queue &); // Copy Constructor ~Queue(); // Destructor void enqueue(Type ); // Add to rear void dequeue(Type &); // Remove from front void queueFront(Type &) const; // retrieve front bool queueIsEmpty() const; // Test for Empty queue bool queueIsFull() const; // Test for Full queue int queueLength() const; // Queue Length private: Type *queue; // pointer to dynamic array int front, rear, count, MaxSize; }; circularQueue.cpp// File:circularQueue.cpp // circular Queue class implementation #include<iostream> #include"circularQueue.h" using namespace std; // Constructor with argument, size is nelements, default 128 template <class Type> Queue<Type>::Queue(int nelements) { MaxSize = nelements; queue = new int[MaxSize]; front = 0; rear = -1; count = 0; } // Copy Constructor template <class Type> Queue<Type>::Queue(const Queue &original) :MaxSize(original.MaxSize), front(original.front), rear(original.rear), count(original.count) { queue = new int[MaxSize]; for (int i = 0; i < MaxSize; i++) queue[i] = original.queue[i]; } // Destructor template <class Type> Queue<Type>::~Queue()
{ delete [] queue;} // Add to rear template <class Type> void Queue<Type>::enqueue(Type v) { if(queueIsFull()) cout << "Queue is Full" << endl; else { rear = (rear + 1) % MaxSize; queue[rear] = v; count++; } } // Remove from front template <class Type> void Queue<Type>::dequeue(Type &v) { if(queueIsEmpty()) cout << "Queue is Empty" << endl; else { v = queue[front]; front = (front + 1) % MaxSize; count--; } } // Retrieve front without removing it template <class Type> void Queue<Type>::queueFront(Type &v) const { if(queueIsEmpty()) cout << "Queue is Empty" << endl; else v = queue[front]; } // Test for Empty queue template <class Type> bool Queue<Type>:: queueIsEmpty() const { return (count == 0); } // Test for Full queue template <class Type> bool Queue<Type>::queueIsFull() const { return (count == MaxSize); } // Queue Length template <class Type> int Queue<Type>::queueLength() const { return count; } //Q1: Implement a method that uses a stack to reverse a queue. template <class Type> void Queue<Type>::reverse() { Stack<Type> s; Type el; while(!queueIsEmpty()) {dequeue(el);
s.push(el); } while(!s.stackIsEmpty()) {s.pop(el); enqueue(el); } } /*Q2: A queue (Q) contains an even number of elements. Write a function Split (Q,Q1,Q2) to copy the 1st half of (Q) into (Q1) and the 2nd half into (Q2), leaving the original queue unchanged */void Queue<Type>:: split(Queue<Type> & Q, Queue<Type> & Q1, Queue<Type> & Q2) {Queue<Type> temp(Q); int length = Q.queueLength(); Type el; for(int i = 0 ; i < length / 2 ; i++) {temp.dequeue(el); Q1.enqueue(el); } int remain = temp.queueLength(); for(int i = 0 ; i < remain ; i++) {temp.dequeue(el); Q2.enqueue(el); }} /*Q3: Write a recursive function Append (Q1,Q2) to append a queue (Q2) to the end of a queue (Q1), leaving Q2 empty. */template <class Type> void Queue<Type>:: Append(Queue<Type> & Q1, Queue<Type> & Q2) { Type el; if(!Q2.queueIsEmpty()) {Q2.dequeue(el); Q1.enqueue(el); Append(Q1, Q2); } }