Question
Complete the queue and de queue fcns for a queue in the following code: Quell: #include #include QueueLL.hpp using namespace std; QueueLL::QueueLL() { queueFront =
Complete the queue and de queue fcns for a queue in the following code:
Quell:
#include <iostream>
#include "QueueLL.hpp"
using namespace std;
QueueLL::QueueLL()
{
queueFront = nullptr;
queueEnd = nullptr;
}
QueueLL::~QueueLL()
{
while( !isEmpty() )
dequeue();
}
bool QueueLL::isEmpty()
{
/*if(queueFront == nullptr || queueEnd == nullptr)
return true;
return false;*/
return (!queueFront || !queueEnd);
}
// TODO
void QueueLL::enqueue(int key)
{
Node *nn = new Node;
nn->key = key;
nn->next = nullptr;
// TODO Complete this function, handle the case when you're enqueuing in an empty queue
}
//TODO
void QueueLL::dequeue()
{
if(!isEmpty())
{
// TODO Complete this function, handle the case when your queue becomes empty after dequeuing
}
else{
cout<<"queue is empty. can not deque"<<endl;
}
}
int QueueLL::peek()
{
if( !isEmpty() )
return queueFront->key;
else
{
cout<< " queue is empty. can not peek"<<endl;
return -1;
}
//return 0;
}
driver.cpp (make sure quell works w this)
#include
#include "QueueLL.hpp"
using namespace std;
int main()
{
// test queues
QueueLL queue;
// TC1: queue empty after created?
cout << "(1) ";
cout << "Queue empty? ";
cout << (queue.isEmpty() ? "yes." : "no.");
if(!queue.isEmpty()) cout << " -- FAIL";
cout << endl;
// TC2: Push items
// TODO - Complete your enqueue function
cout << "(2) ";
cout << "Enqueuing 1, 2, 3" << endl;
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
// TC3: Queue not empty after pushing items
cout << "(3) ";
cout << "Queue empty? ";
cout << (queue.isEmpty() ? "yes." : "no.");
if(queue.isEmpty()) cout << " -- FAIL";
cout << endl;
// TC4: peek
cout << "(4) ";
int topKey = queue.peek();
cout << "Peeked key: " << topKey;
if(topKey != 1) {
cout << " -- FAIL";
}
cout << endl;
// TC5: pop items
// TODO: Complete your dequeue function
cout << "(5) Dequeuing everything" << endl;
int testint = 0;
int testvals[] = {1, 2, 3};
while(!queue.isEmpty()) {
topKey = queue.peek();
queue.dequeue();
cout << " - dequeue: " << topKey;
if(topKey != testvals[testint++]) {
cout << " -- FAIL";
}
cout << endl;
}
if(testint != 3) cout << " -- FAIL" << endl;
return 0;
}
Step by Step Solution
3.42 Rating (165 Votes )
There are 3 Steps involved in it
Step: 1
First TODO Since we are enqueuing in an empty queue simply make the fist and last pointer ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started