included is my entire program...
i know exactly what the problem i am having with this is, i just cant figure out the logic to fix it... ive tried about 10 different things so far.
I have it so you can input which position you start at... then go around the circle, the only problem is that the starting position is the lowest position held then it will repeat other nodes.
ie: if you start at position 5 and have 15 people in a circle... once 15 is hit, it resets back to number 5 ( not 1)... how do i change this so that it will read from say 1 to 15 starting at 5 instead of 5 to 15 starting at 5.
the code snippet where i think the problem is located is at the very bottom.
thanks alot.
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
struct node {
int position;
node * next;
node * prev;
};
class circularll {
public:
void create (int, int);
void run (int, int, int);
node * head;
node * tail;
private:
};
void circularll::create (int people, int startPos) {
int x, y = 1;
head = tail = NULL;
for (x = startPos; x < (people + startPos); x++) {
if (x == startPos) {
node * temp = new node;
temp -> prev = NULL;
temp -> next = NULL;
temp -> position = x;
head = tail = temp;
}
else {
node * temp = new node;
node * curr = head = tail;
while(curr -> next != NULL) {
curr = curr -> next;
}
if (x > people) {
temp -> position = y;
temp -> next = NULL;
temp -> prev = curr;
curr -> next = temp;
}
else {
temp -> position = x;
temp -> next = NULL;
temp -> prev = curr;
curr -> next = temp;
}
}
y++;
}
node * end = head = tail;
while (end -> next != NULL) {
end = end -> next;
}
end -> next = head = tail;
head -> prev = end;
tail -> prev = end;
}
void circularll::run (int people, int passBy, int startPos) {
int x, i;
node * curr = head = tail;
for (x = 1; x < people; x++) {
for (i = 1; i <= passBy; i++) {
curr = curr -> next;
}
node * out = curr;
node * before = curr -> prev;
node * after = curr -> next;
cout << "The person who was sitting at seat " << out -> position << " was killed." << endl;
before -> next = after;
after -> prev = before;
curr = after;
delete out;
}
cout << endl;
cout << "Josephus was sitting at seat: " << curr -> position << endl << endl;
}
int main() {
int people, passBy, startPos;
menu: // start of menu
cout << "How many people are there? (Maximum of 50) ";
cin >> people;
if (people > 50) {
cout << "Invalid entry" << endl;
goto menu; // returns you to the beginning of the menu
}
cout << endl << endl;
cout << "Which position would you like to start with? " ;
cin >> startPos;
cout << endl << endl;
cout << "How many people do you want to skip each round? ";
cin >> passBy;
cout << endl << endl;
circularll josephus;
josephus.create (people, startPos);
josephus.run (people, passBy, startPos);
cin >> people;
return 0;
}
void circularll::create (int people, int startPos) {
int x, y = 1;
head = tail = NULL;
for (x = startPos; x < (people + startPos); x++) {
if (x == startPos) {
node * temp = new node;
temp -> prev = NULL;
temp -> next = NULL;
temp -> position = x;
head = tail = temp;
}
else {
node * temp = new node;
node * curr = head = tail;
while(curr -> next != NULL) {
curr = curr -> next;
}
if (x > people) {
temp -> position = y;
temp -> next = NULL;
temp -> prev = curr;
curr -> next = temp;
}
else {
temp -> position = x;
temp -> next = NULL;
temp -> prev = curr;
curr -> next = temp;
}
}
y++;
}
node * end = head = tail;
while (end -> next != NULL) {
end = end -> next;
}
end -> next = head = tail;
head -> prev = end;
tail -> prev = end;
}