Assume that you have to submit the form
to the administrator of your organization.
People have to enter in the line from the
backside, wait for their turn and then leave
after submission and their processing from the front end. Determine the most suitable data
structure. Provide both programs:
• For array based
• Linked List based
#include<iostream>
#include<vector>
#include<windows.h>
using namespace std;
struct People
{
int ID;
int waitSeconds;
People(int _ID, int _waitSeconds)
:ID(_ID),waitSeconds(_waitSeconds){}
};
struct Node
{
People* peop;
struct Node *prev;
Node(People* _peop):peop(_peop), prev(NULL) {}
};
void LinkListQueue(People* peop, int n)
{
//n people enter to the line backside
Node* Head =new Node(&peop[0]);
Node* p=Head;
for(int i=1;i<n;i++)
{
Node* temp=new Node(&peop[i]);
p->prev=temp;
p=temp;
}
//People wait for turn and go out
for(int i=0;i<n;i++)
{
cout<<"\nPeople "<<Head->peop->ID<<" make a submission...";
Sleep(Head->peop->waitSeconds);
cout<<"\nPeople "<<Head->peop->ID<<" go out from line";
Node* temp=Head;
Head=Head->prev;
delete Head;
}
}
void ArrayQueue(People* p, int n)
{
vector<People>vec;
//n people enter to the line backside
for(int i=0;i<n;i++)
{
vec.push_back(p[i]);
}
//People wait for turn and go out
for(int i=0;i<n;i++)
{
cout<<"\nPeople "<<vec.begin()->ID<<" make a submission...";
Sleep(vec.begin()->waitSeconds);
cout<<"\nPeople "<<vec.begin()->ID<<" go out from line";
vec.erase(vec.begin());
}
}
int main()
{
const int N=5;
People peops[N]={People(1,2000),People(2,4000),People(3,1000),
People(4,3000),People(5,2000)};
cout<<"Array queue: \n";
ArrayQueue(peops,N);
cout<<"\n\nLinked List queue: \n";
LinkListQueue(peops,N);
}
Comments
Leave a comment