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;
	
}

Recommended Answers

All 4 Replies

nice but there is small problem by y++

I made it like this:

#include <iostream>
#include <conio.h>
using namespace std;

struct s
{
       int info;
       s* next;
};

int main()
{

    int n, i, pos, freq;
    s *ptr, *start, *loc;
    cout<<"Tell the total number of players: ";
    cin>>n;
    ptr= new s;
    start=ptr;
    for(i=1; i<=n; i++)
    {
             if(i<n)
             {
                    ptr->info = i;
                    ptr->next = new s;
                    ptr = ptr->next;
             }
             if(i==n)
             {
                     ptr->info = i;
                     ptr->next = start;
             }
    }

    cout<<"Enter the starting position: ";
    cin>>pos;
    ptr=start;
    i=1;
    while(i!=pos)
    {
                 ptr=ptr->next;
                 i++;
    }
    cout<<"Enter the intermidiate leaving frequency: ";
    cin>>freq;
    cout<<endl;

    while(ptr->next != ptr)
    {
                    for(i=1;i<=freq;i++)
                    {
                                        ptr = ptr->next;
                                        loc = ptr->next;
                    }
                    cout<<"Eliminated: PLAYER "<<loc->info<<endl;
                    ptr->next = loc->next;      //TO ELIMINATE THE PLAYER
                    ptr = ptr->next;   //TO TRANSFER THE CONTROL TO NEXT PLAYER
    }

    cout<<endl<<"Winner is: PLAYER "<<ptr->info;                                

    getch();
    return (0);

}

It's been six years, so I don't think the OP is interested anymore. Also, your code contains huge memory-leaks. If you use new you should delete your memory afterwards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.