Data Structures
Prepared by Raja Majid Mehmood
Tutorial (Queues)
Question 1:
Based on the partial C++ code in Program listing 1, answer the following in bold text.
#include <iostream>
using namespace std;
class Node {
public:
char data;
Node* next; // pointer to next
};
class Queue {
public:
Queue() { // constructor
front = rear = NULL;
counter = 0;
}
~Queue() { // destructor
char value;
while (!IsEmpty()) Dequeue(value);
}
bool IsEmpty() {
if (counter) return false;
else return true;
}
void Enqueue(char x);
bool Dequeue(char & x);
void DisplayQueue(void);
private:
Node* front; // pointer to front node
Node* rear; // pointer to last node
int counter; // number of elements
};
void Queue::Enqueue(char x) {
//implement this function
}
bool Queue::Dequeue(char & x) {
//implement this function
}
void Queue::DisplayQueue() {
//implement this function
}
int main()
{
Queue queue;
queue.Enqueue(‘a’);
queue.Enqueue(‘b’);
queue.Enqueue(‘c’);
queue.Enqueue(‘d’);
queue.Enqueue(‘e’);
queue.DisplayQueue();
cout<<endl;
Data Structures
char value;
queue.Dequeue(value);
queue.DisplayQueue();
cout<<endl;
queue.Enqueue(‘f’);