C++ Write the circular queue class (you may use the linked list version or dynam
ID: 3724880 • Letter: C
Question
C++
Write the circular queue class (you may use the linked list version or dynamic array/vector implementation). Call it cqueue. Use your circular queue class to simulate a real world 3-color (Green, Yellow, and Red) traffic light system. The Red and the Green lights stay on for a duration selected by the user, while the Yellow stays on for a very small amount of time that is already predefined in the system. After the duration has passed, the light switches to the next color.
Circular Queue using Arrays AR AI0] AN-1 Circular Queue using Linked list AIN-2 Arl] AI2] FRONT REAR A[2]Explanation / Answer
# include<iostream>
using namespace std;
class trafficQ
{
char *a;
int delay1,delay2;//Delay1 is duration of blinking for RED and Green,Delay2 is for YELLOW
int r,f,size;
public:
trafficQ();
void enque(char );//ADDS the colors RED,YELLOW and GREEN to circular queue
void display();
};
trafficQ::trafficQ()
{
size=3;
delay2=5;
cout<<" Enter the duration for blinking of RED AND GREEN light:";
cin>>delay1;
a=new char[size];
r=-1;
f=-1;
}
void trafficQ::enque(char x)
{
if((r+1)%size==f)
cout<<" Circular Q overflow";
else
{
if(r==size-1)
r=0;
else
{
r=r+1;
a[r]=x;
if(f==-1)
f++;
}
}
}
void trafficQ::display()
{
int i,j;
for (i=f;i<=r;i++)
{
cout<<" ";
if(i==1)
{
for(j=0;j<delay2;j++)
cout<<a[i];
}
else
{
for(j=0;j<delay1;j++)
cout<<a[i];
}
}
}
int main()
{
trafficQ q;
q.enque('R');
q.enque('Y');
q.enque('G');
q.display();
}
Sample output:
Enter the duration for blinking of RED AND GREEN light: 10
RRRRRRRRRR
YYYYY
GGGGGGGGGG
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.