\"In this assignment, you need to implement a template classcalled MyQueue , whi
ID: 3609751 • Letter: #
Question
"In this assignment, you need to implement a template classcalled MyQueue, whichimplements a queue or a First-In-First-Out (FIFO) datastructure. A MyQueue object can store a dataitem at the tail of the queue (Enqueue) and retrieve a data itemfrom the head of the queue (Dequeue). In the object, the queue isimplemented by a dynamically allocated array called elements."
The class definition for MyQueue is given. I think I'mgoing about this wrong, and mixing up arrays (which I have a betterunderstanding of) with queues. I wrote the definition ofEnqueue like this:
template<class T> //enqueues user-defined item
MyQueue<T>::Enqueue(const T &item)
{
elements[tail] = item;
for (int i = 0; elements[i]>0; ++i)
{
elements[i] = tail;
}
for (int i = 0; i<length; ++i)
{
if(elements[i] != 0)
{
count = count + 1;
}
}
What I'm trying to do is set the user-defined "item" to stack onthe end (tail) of the queue. Then I'm assuming I have toupdate the location of the tail, because it effectively works likea pointer, right? Finally, I have to keep a count of theamount of elements in the queue (as opposed to the length of thequeue itself), which is what the second loop is (supposed to be)for. I don't really want to even try compiling this until Ihave a firmer grasp of Queues and how to implement them,particularly how the head and tail pointers work. I hope thisis descriptive enough and I appreciate any help you can offer.
Explanation / Answer
Dear,//C++ Program to implement Dequeue
#include <iostream.h>
#include <conio.h>
#include <time.h>
class node
{
int data;
char ch1;
node *start,*left,*temp1,*right,*fresh,*temp,*last;
public:
node()
{
start=NULL;
last=NULL;
}
void menu()
{
cout<<" LIST OPERATIONS"<<endl;
cout<<"==============="<<endl;
cout<<"1.INSERT"<<endl;
cout<<"2.DELETE"<<endl;
cout<<"3.EXIT"<<endl;
}
void create()
{
fresh=new node();
cout<<"Enter the data"<<endl;
cin>>fresh->data;
fresh->left=NULL;
fresh->right=NULL;
}
void insbeg()
{
create();
if(start==NULL && last==NULL)
{
start=fresh;
last=fresh;
}
else
{
fresh->right=start;
start->left=fresh;
start=fresh;
}
}
void insend()
{
create();
temp=start;
while(temp->right!=NULL)
temp=temp->right;
temp->right=fresh;
fresh->left=temp;
last=fresh;
}
void delbeg()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
temp=start;
cout<<"Deleted element is:"<<start->data<<endl;
start=start->right;
start->left=NULL;
delete temp;
}
}
void delend()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
temp=last;
cout<<"Deleted element is:"<<last->data<<endl;
last=last->left;
last->right=NULL;
delete temp;
cout<<last;
}
}
void menuins()
{
do
{
cout<<"INSERT OPERATIONS"<<endl;
cout<<"================="<<endl;
cout<<"a.INSERT AT START"<<endl;
cout<<"b.INSERT AT END"<<endl;
cout<<"c.TRAVERSE(FORWARD)"<<endl;
cout<<"d.TRAVERSE(BACKWARD)"<<endl;
cout<<"e.EXIT"<<endl;
cout<<"Enter ur choice"<<endl;
cin>>ch1;
switch(ch1)
{
case 'a':insbeg();break;
case 'b':insend();break;
case 'c':traversefront();break;
case 'd':traverseback();break;
case 'e':break;
default:cout<<"Please enter the rightchoice"<<endl;break;
}
}while(ch1!='e');
}
void menudel()
{
do
{
cout<<"DELETE OPERATIONS"<<endl;
cout<<"================="<<endl;
cout<<"a.DELETE AT START"<<endl;
cout<<"b.DELETE AT END"<<endl;
cout<<"c.TRAVERSE(FORWARD)"<<endl;
cout<<"d.TRAVERSE(BACKWARD)"<<endl;
cout<<"e.EXIT"<<endl;
cout<<"Enter ur choice"<<endl;
cin>>ch1;
switch(ch1)
{
case 'a':delbeg();break;
case 'b':delend();break;
case 'c':traversefront();break;
case 'd':traverseback();break;
case 'e':break;
default:cout<<"Please enter the rightchoice"<<endl;break;
}
}while(ch1!='e');
}
void traversefront()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
cout<<"The elements in the dll(forward)are:"<<endl;
for(temp=start;temp!=NULL;temp=temp->right)
cout<<temp->left<<":"<<temp->data<<":"<<temp<<"->"<<endl;
cout<<endl;
}
}
void traverseback()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
cout<<"The elements in the dll(backward)are:"<<endl;
// end();
for(temp=last;temp!=NULL;temp=temp->left)
cout<<temp->right<<":"<<temp->data<<":"<<temp<<"->";
cout<<endl;
}
}
};
void main()
{
int ch;
node n;
clrscr();
do
{
n.menu();
cout<<"Enter ur choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:n.menuins();
break;
case 2:n.menudel();
break;
case 3:break;
default:cout<<"Pleaseenter the right choice"<<endl;break;
}
}while(ch!=3);
getch();
} I hope this will helpfulfor you...................
//C++ Program to implement Dequeue
#include <iostream.h>
#include <conio.h>
#include <time.h>
class node
{
int data;
char ch1;
node *start,*left,*temp1,*right,*fresh,*temp,*last;
public:
node()
{
start=NULL;
last=NULL;
}
void menu()
{
cout<<" LIST OPERATIONS"<<endl;
cout<<"==============="<<endl;
cout<<"1.INSERT"<<endl;
cout<<"2.DELETE"<<endl;
cout<<"3.EXIT"<<endl;
}
void create()
{
fresh=new node();
cout<<"Enter the data"<<endl;
cin>>fresh->data;
fresh->left=NULL;
fresh->right=NULL;
}
void insbeg()
{
create();
if(start==NULL && last==NULL)
{
start=fresh;
last=fresh;
}
else
{
fresh->right=start;
start->left=fresh;
start=fresh;
}
}
void insend()
{
create();
temp=start;
while(temp->right!=NULL)
temp=temp->right;
temp->right=fresh;
fresh->left=temp;
last=fresh;
}
void delbeg()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
temp=start;
cout<<"Deleted element is:"<<start->data<<endl;
start=start->right;
start->left=NULL;
delete temp;
}
}
void delend()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
temp=last;
cout<<"Deleted element is:"<<last->data<<endl;
last=last->left;
last->right=NULL;
delete temp;
cout<<last;
}
}
void menuins()
{
do
{
cout<<"INSERT OPERATIONS"<<endl;
cout<<"================="<<endl;
cout<<"a.INSERT AT START"<<endl;
cout<<"b.INSERT AT END"<<endl;
cout<<"c.TRAVERSE(FORWARD)"<<endl;
cout<<"d.TRAVERSE(BACKWARD)"<<endl;
cout<<"e.EXIT"<<endl;
cout<<"Enter ur choice"<<endl;
cin>>ch1;
switch(ch1)
{
case 'a':insbeg();break;
case 'b':insend();break;
case 'c':traversefront();break;
case 'd':traverseback();break;
case 'e':break;
default:cout<<"Please enter the rightchoice"<<endl;break;
}
}while(ch1!='e');
}
void menudel()
{
do
{
cout<<"DELETE OPERATIONS"<<endl;
cout<<"================="<<endl;
cout<<"a.DELETE AT START"<<endl;
cout<<"b.DELETE AT END"<<endl;
cout<<"c.TRAVERSE(FORWARD)"<<endl;
cout<<"d.TRAVERSE(BACKWARD)"<<endl;
cout<<"e.EXIT"<<endl;
cout<<"Enter ur choice"<<endl;
cin>>ch1;
switch(ch1)
{
case 'a':delbeg();break;
case 'b':delend();break;
case 'c':traversefront();break;
case 'd':traverseback();break;
case 'e':break;
default:cout<<"Please enter the rightchoice"<<endl;break;
}
}while(ch1!='e');
}
void traversefront()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
cout<<"The elements in the dll(forward)are:"<<endl;
for(temp=start;temp!=NULL;temp=temp->right)
cout<<temp->left<<":"<<temp->data<<":"<<temp<<"->"<<endl;
cout<<endl;
}
}
void traverseback()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
cout<<"The elements in the dll(backward)are:"<<endl;
// end();
for(temp=last;temp!=NULL;temp=temp->left)
cout<<temp->right<<":"<<temp->data<<":"<<temp<<"->";
cout<<endl;
}
}
};
void main()
{
int ch;
node n;
clrscr();
do
{
n.menu();
cout<<"Enter ur choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:n.menuins();
break;
case 2:n.menudel();
break;
case 3:break;
default:cout<<"Pleaseenter the right choice"<<endl;break;
}
}while(ch!=3);
getch();
} I hope this will helpfulfor you................... I hope this will helpfulfor you...................
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.