Skip to content

Instantly share code, notes, and snippets.

Created September 29, 2016 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f00c910da6c6cd90859ae234855566a3 to your computer and use it in GitHub Desktop.
Save anonymous/f00c910da6c6cd90859ae234855566a3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include "cqueue2.h"
using namespace std;
Cqueue2::Cqueue2(int n)
{
Size = 0;
Rear = Front = -1;
Max = n;
Queue = new string[n]; //the Queue array is dynamically allocated
}
Cqueue2::~Cqueue2()
{
delete[] Queue; //have to delete dynamically allocated memory
}
int Cqueue2::Is_Empty()
{
if (Size == 0) return true;
else return false;
}
int Cqueue2::Is_Full()
{
if (Size == Max) return true;
else return false;
}
void Cqueue2::Add(string Element)
{
if (!Is_Full()) Rear = (Rear + 1) % Max;
Queue[Rear] = Element;
Size++;
}
string Cqueue2::Delete()
{
if (Is_Empty())
{
cout << "The Cqueue is empty." << endl;
return "";
}
else
{
Front = (Front + 1) % Max;
Size--;
return Queue[Front];
}
}
string Cqueue2::getFront()
{
int Temp;
if (Is_Empty())
{
return "";
}
else
{
Temp = (Front + 1) % Max;
return Queue[Temp];
}
}
void Cqueue2::write_Cqueue_to_Console() //write all of the class variables to the console
{
int i;
int Temp;
if (Is_Empty())
cout << "Queue is empty.\n";
else
{
for (i = 1; i <= Size; i++)
{
Temp = (Front + i) % Max;
cout << Queue[Temp] << " ";
}
cout << endl;
}
int Cqueue2::get_size() //this function is added to Cqueue2 so can tell when have 1 member in the queue
{
return Size;
}
//2: Do this
string Cqueue2::remove_nth_member(int n) //for Josephus problem to remove a member
{
string name;
int i;
int index;
int start;
name = Queue[(n - 1) % Size];
//we'll be deleteing the soldier at index n%Size
//subtract 1 to account for the fact that indexing starts at 0
start = n%Size;
string * TempQ = new string[Size - 1];
//some looping has to go on here, to move stuff in current queue to temp queue that is smaller
for (i = 0; i < Size; i++)
{
index(Start++i) % Max;
tempQ[i] = Queue[index];
}
//refresh Queue with the member who is kicked out gone
//Change its size
//delete Queue
for (int j = 0; j < Size; j++)
{
Size--;
}
//create a new Queue with new Size
//iterate and move things from tempQ into Queue
//deduct from the Max variable
return name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment