I am have some trouble implementing my client code for this program could someone give me some ideas.
This problem is an exercise in the implementation of queue. We will look at the implementation of a circular array. Then we will use the implementation to solve a problem called the Josephus problem.
Josephus Flavius was a famous historian of the first century at the time of the Second Temple destruction. During the war he got trapped in a cave with a group of 39 soldiers surrounded by Romans. The legend has it that preferring suicide to capture, the people decided to form a circle and, proceeding clockwise around it, to kill every seventh(It will be given as an input) person until only one was left, who must then commit suicide. Josephus, an accomplished mathematician, quickly found the safe spot in the circle (24th) to be the last to go. But when the time came, instead of killing himself he joined the Roman side. The problem rightfully raises the question of how someone might be able to quickly compute the correct place to stand.
Code:
#include <iostream>
#include <string>
using namespace std;
const int MAXITEMS = 20;
struct ItemType
{
string name;
int position;
};
class QueType
{
public:
QueType( );
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType newitem);
void Dequeue(ItemType & item);
int LengthIs() const;
private:
int rear, front;
ItemType items[MAXITEMS];
};
QueType::QueType()
{
rear = MAXITEMS-1;
front = MAXITEMS-1;
}
bool QueType::IsEmpty() const
{
return (rear == front);
}
bool QueType::IsFull() const
{
return ((rear + 1) % MAXITEMS == front);
}
void QueType::Enqueue(ItemType newitem)
{
rear = (rear +1 ) % MAXITEMS;
items[rear] = newitem;
}
void QueType::Dequeue(ItemType & item)
{
front = (front + 1) % MAXITEMS;
item = items[front];
}
int QueType::LengthIs() const
{
if(rear > front)
return (rear - front);
else
return (rear + MAXITEMS - front);
}
int main()
{
QueType queue;
int nosols;
ItemType soldier;
cout<<"How many soldiers? ";
cin >> nosols;
for(int i = 0 ; i < nosols; i++)
{
cout<<" Type the soldier name ";
cin >> soldier.name;
soldier.position = i+1;
if(queue.IsFull())
{
cout<<"Queue is full. Can not insert it.";
return 1;
}
else
{
queue.Enqueue(soldier);
}
}
for (int i = 0; i < nosols; i++)
Dequeue(
return 0;
}