Hi ChelseaL,
Maybe you have a requirement ?
Are you just willing to implement your own queue system ?
What you ask for is just what we call a queue. We also call that "first in, first out" or as you stated it "first come, first serve".
If you want to make a simple implementation of that kind of stuff (a queue), you can use a vector<> for example, and a vector of strings if you wish to have texts intead of number.
For the example, I rather use number for the simplicity.
So the underlying structure used for the implementation of the queue will be a
.
Each "client" is an integer (int) and you queue them when they "arrive" and you "unqueue" them each time you take care of her.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
#include <vector>
class Queue
{
public:
void oneMoreClient(int clientID)
{
queue.push_back(clientID);
}
int processOneClient()
{
return isEmpty() ? -1 : queue[currentClient++];
}
bool isEmpty() const
{
return queue.size() <= currentClient;
}
Queue()
: currentClient(0)
{}
private:
std::vector<int> queue;
int currentClient;
};
int main()
{
Queue restaurant;
restaurant.oneMoreClient(1);
restaurant.oneMoreClient(2);
std::cout << restaurant.processOneClient() << std::endl;
restaurant.oneMoreClient(3);
restaurant.oneMoreClient(4);
std::cout << restaurant.processOneClient() << std::endl;
restaurant.oneMoreClient(5);
std::cout << restaurant.processOneClient() << std::endl;
std::cout << restaurant.processOneClient() << std::endl;
std::cout << restaurant.processOneClient() << std::endl;
std::cout << restaurant.processOneClient() << std::endl;
std::cout << restaurant.processOneClient() << std::endl;
std::cout << restaurant.processOneClient() << std::endl;
std::cout << restaurant.processOneClient() << std::endl;
//--
return 0;
}
| |
This is ugly implementation right. But it's just to show the most important :
- the two primitive functions of a queue : queue and unqueue. Here those two functionalities are called oneMoreClient() and processOneClient()
- and the isEmpty() function which reflects the status of the queue being empty or not.
In the sample test, you see that 5 clients arrive and to make it simple, they are 1, 2, 3, 4, and 5.
You can then put any of the "std::cout" line anywhere you want... each time, a client will be unqueued... at the end, if you ask for more client, the queue will give you "-1" symbolizing "no more client" or "empty queue".
And then, you can use the queue, dequeue... structure of the std:: ^_^