C++ LAB ASSIGNMENT Circular Queue When using a static array, creating a way impl
ID: 3703800 • Letter: C
Question
C++ LAB ASSIGNMENT Circular Queue When using a static array, creating a way implement a static array without overflowing the memory allocated for the array When you hardcode your queue look to make it circular C++ Task 1: Implement a Queue on a char n array. Do not use::Queue:: class from the STD library for this example The user will input a string and the program will return the string with each letter in the string duplicated. Displaying correct Queue implementation Utilize the conventional static methods as needed enqueue () / push_back () dequeue )/pop front ( Sample Execution Please input String happy hhaappppyy C++ Task 2: Implement a Queue using a vector or the STD::queue:: class Note the difference in what it takes to implement using a static charfl array vs a vector or the STD::queue:: class The user will input a string and the program will return the string with each letter in the string duplicated. Displaying correct Queue implementation. Utilize the conventional methods as needed. Sample Execution Please input String happy hhaappyExplanation / Answer
Task 1
========
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
//creating class queue which implements queue structure using array
class queu
{
char q[20];
int rear,front;
public:
queu()
{
rear=-1;
front=-1;
}
void push_back(char c)
{
if(rear > 19)
{
cout <<"queue is full";
front=rear=-1;
return;
}
q[++rear]=c;
}
void pop_front()
{
if(front==rear)
{
cout <<"queue is empty";
return;
}
q[++front];
}
void display()
{
if(rear==front)
{
cout <<" queue is empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<q[i];
}
};
int main()
{
string str;
cout<<"Please input string :"<<endl;
getline(cin,str);
int length = str.length();
char c_array[length + 1];
strcpy(c_array, str.c_str()); //changing the string to char array
queu qu;//creating object of class queue
for(int i=0;i<length;i++)
{
//inserting duplicate values
qu.push_back(c_array[i]);
qu.push_back(c_array[i]);
}
cout<<"output :";
qu.display();
return 0;
}
Task 2
======
#include <iostream>
//importing queue class
#include <queue>
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
cout<<"Please input string :"<<endl;
getline(cin,str);
int length = str.length();
char c_array[length + 1];
strcpy(c_array, str.c_str());
queue <char> q;//initiaizing variable for queue class
for(int i=0;i<length;i++)
{
//duplicate entry
q.push(c_array[i]);
q.push(c_array[i]);
}
cout << "Output : ";
while (!q.empty())
{
cout<<q.front();
q.pop();
}
return 0;
}
Difference betweeen two:
------------------------------------
In task 1 we created fixed size array & programmed all function manually.
whereas in task 2 we have inherited predefined queue class. So here no need to create fixed size array & implement those necessary functions for queue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.