Question: The Problem Create a class Queue. The queue should be implemented with a dynamically allocated linked list (you may want to make a listnode struct/class
The Problem
Create a class Queue. The queue should be implemented with a dynamically allocated linked list (you may want to make a listnode struct/class within Queue). Only change the Queue.h and Queue.cpp files.
Implement the following member functions of the queue class:
int getSize() - returns the number of elements in the queue (0 if empty)
bool isEmpty() - returns if the list has no elements, else false
void enqueue() - puts an item to the queue in O(1) time
int dequeue() - removes an item off the queue and returns the value in O(1) time
//main.cpp file
#include
using namespace std;
#include "queue.h"
int main() {
Queue q;
int r;
cout<<"enqueue(3) to the queue..."< q.enqueue(3); cout<<"Stack size: "< cout<<"enqueue(2) to the queue..."< q.enqueue(2); cout<<"Stack size: "< cout<<"enqueue(1) to the queue..."< q.enqueue(1); cout<<"Stack size: "< cout<<"dequeue()"< r = q.dequeue(); cout<<"Returned Value: "< cout<<"dequeue()"< r = q.dequeue(); cout<<"Returned Value: "< cout<<"dequeue()"< r = q.dequeue(); cout<<"Returned Value: "< return 0; } //queue.h file #ifndef _QUEUE_H #define _QUEUE_H class Queue { public: int size() const; bool isEmpty() const; void enqueue(int value); int dequeue(); }; #endif //queue.cpp file #include "queue.h" // `int size()` - returns the number of elements in the stack (0 if empty) int Queue::size() const { return 0; } // `bool isEmpty()` - returns if the list has no elements, else false bool Queue::isEmpty() const { return true; } // `void enqueue()`- puts an item to the queue in O(1) time void Queue::enqueue(int value) { } // `int dequeue()` - removes an item off the queue and returns the value in O(1) time int Queue::dequeue() { return -1; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
