answer this question this is assginment plz neta and clean properly attempt 3. I
ID: 3916238 • Letter: A
Question
answer this question this is assginment plz neta and clean properly attempt
3. Implement a Queue (50 Marks) Queue is a commonly known data structure. A queue is similar to a checkout line in a supermarket -the cashier services the person at the beginning of the line first. Other customers enter the line only at the end and wait for service. Queue nodes are removed only from the head (or front) of the queue and are inserted only at the tail (or end). For this reason, a queue is a first-in, first-out (FIFO) data structure You are required to write a generic or template class ("MyQueue") that will have following methods: Method Name Description PrintQueue Prints the Queue contents to a console window Enqueue Adds an object to the end of the Queue Dequeue Removes and returns the object at the beginning of the Queue Determines whether an element is in the Queue How many items are in the queue Contains Count Note: * Your code should work with any data type and so should be a template or generic code. . Do not use STL containers. std::queue or any other container. Implement the queue yourself.Explanation / Answer
//QUESTION — IMPLEMENT A QUEUE
/*contains all the function except one i.e. Contain function*/
#include <iostream>
using namespace std;
template <class T> class node
{
private:
T nodeValue;
public:
node<T> *nextNode;
node(T t)
{
nodeValue=t;
}
T getValue()
{
return nodeValue;
}
};
template <class T>
class Queue//generic queue class
{
private:
node<T> *front;
node<T> *rear;
node<T> *temp;
bool empty;
bool full;
public:
Queue() //constructor to initialize queue
{
front=NULL;
rear=NULL;
empty=true;
full=false;
}
void PrintQueue()
{
temp=front;
if(temp!=NULL){
while(temp!=NULL){
if(temp->nextNode!=NULL){
cout<<temp->getValue()<<", ";
}
else{
cout<<temp->getValue();
}
temp=temp->nextNode;
}
cout<<endl;
}else{
cout<<"queue empty" <<endl;
}
}
void Enqueue(T data)//add element to the queue
{
node<T> *newNode=new node<T>(data);
newNode->nextNode=NULL;
if(rear==NULL){
rear=newNode;
front=newNode;
}else{
rear->nextNode=newNode;
rear=newNode;
}
}
T Dequeue()//remove element from the queue
{
T value;
if(!isempty()){
temp=front;
value=front->getValue();
front=front->nextNode;
delete(temp);
}else{
throw "Exception:Queue empty";
}
return value;
}
bool isempty()
{
if(front==NULL){
empty=true;
}else{
empty=false;
}
return empty;
}
int Count()//count number of elements in the queue
{
temp=front;
if(temp!=NULL)
{
int c=0;
while(temp!=NULL)
{
temp=temp->nextNode;
c=c+1;
}
return c;
}
else
{
return 0;
}
}
bool Contains(T data)
{
temp=front;
if(temp!=NULL){
while(temp!=NULL){
if(temp->nextNode!=NULL){
temp->getValue();
}
else{
cout<<temp->getValue();
}
temp=temp->nextNode;
}
cout<<endl;
}
else
{
cout<<"queue empty" <<endl;
}
}
~Queue()//destructor
{
while(front!=NULL){
temp=front;
front=front->nextNode;
delete(temp);
}
}
};
int main()
{
int i=0;
cout<<"Integer Type Queue"<<endl;
Queue<int> queue;
cout<<"Enqueue(enqueue):"<<endl;
for(i=1;i<=5;i=i+1){
cout<<"Enqueue:"<<i<<" ";
queue.Enqueue(i);
}
queue.PrintQueue();
cout<<" size:"<<queue.Count()<<endl;
cout<<"Dequeue:"<<endl;
int data=queue.Dequeue();
cout<<"Dequeued="<<data<<endl;
cout<<"Queue after Dequeue first element:"<<endl;
queue.PrintQueue();
return 0;
}
//QUESTION-- TO FIND PALINDROME OR NOT
#include <iostream>
#include <cstring>
using namespace std;
int isPalindrome(string A) {
string::iterator iterate;
string::reverse_iterator reverse_iterate;
iterate=A.begin();
reverse_iterate=A.rbegin();
while(iterate!=A.end() && reverse_iterate!=A.rend()){
while(reverse_iterate != A.rend() && !isalnum(*reverse_iterate)) //if char from the end is not alphanumeric, then increment the reverse iterator till we find the alphanumeric char.
++reverse_iterate;
while(iterate != A.end() && !isalnum(*iterate)) //if char from the start is not alphanumeric, then increment the iterator till we find the alphanumeric char.
++iterate;
if (iterate == A.end() || reverse_iterate == A.rend())
break;
if(tolower(*iterate)!=tolower(*reverse_iterate)) //case in-sensitive comparison
return 0;
++iterate;
++reverse_iterate;
}
return 1;
}
int main()
{
if(isPalindrome("Don't nod"))
cout<<"Palindrome";
else
cout<<"not a palindrome";
return 0;
}
//QUESTION -- TEMPLATE DEFINITION OF SQUARE FUNCTION
#include <iostream>
using namespace std;
template <class T>// template definition for square function
T square(T number)
{
return number * number;
}
int main()
{
cout << "Enter an integer: ";
int int_value;
cin >> int_value;
cout << "The square is " << square(int_value);
cout << "Enter a float: ";
float float_value;
cin >> float_value;
cout << "The square is " << square(float_value);
cout << " Enter a double: ";
double double_value;
cin >> double_value;
cout << "The square is " << square(double_value) << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.