look at the class defined in queue.h and create the corresponding implementation
ID: 3878687 • Letter: L
Question
look at the class defined in queue.h and create the corresponding implementation in queue.cpp . You may
not change any of the existing declarations but you may add more functions as you deem necesarry.
queu.h
----------
#ifndef Q_H
#define Q_H
// include this library to use NULL, otherwise use nullptr instead
#include <cstddef>
#include <iostream>
/* Struct which will be the building block of our list */
struct node{
int val;
node* next;
};
/* Linked list class definition */
class QUEUE{
public:
QUEUE();
void enq(int);
bool deq();
bool isEmpty();
node* getFront();
void printq();
private:
node* front;
node* back;
};
#endif
Explanation / Answer
#ifndef Q_H
#define Q_H
// include this library to use NULL, otherwise use nullptr instead
#include <cstddef>
#include <iostream>
using namespace std;
/* Struct which will be the building block of our list */
struct node{
int val;
node* next;
};
/* Linked list class definition */
class QUEUE{
private:
node* front;
node* back;
node* temp;
node* current;
public:
QUEUE(){
front=new node;
front->val=0;
front->next=NULL;
back=front;
}
void enq(int a){
if(front->val==0) front->val=a;
else{
current=new node;
current->val=a;
current->next=back;
back=current;
}
}
bool deq(){
if(isEmpty())
return false;
else
{
temp=back;
while(temp->next!=front){
temp=temp->next;
}
front=temp;
delete(temp->next);
return true;
}
}
bool isEmpty(){
if(front==NULL)
return true;
else
return false;
}
node* getFront(){
if(!isEmpty())
return front;
else
cout<<"Queue is empty"<<endl;
}
void printq(){
temp=back;
while(temp!=NULL){
cout<<temp->val<<" ";
temp=temp->next;
}
}
};
#endif
int main(){
QUEUE queue;
queue.enq(3);
queue.enq(4);
queue.printq();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.