Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,999 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,325 people online right now. Registration is fast and FREE... Join Now!




how to use circular linked list to solve the Josephus problems

 
Reply to this topicStart new topic

how to use circular linked list to solve the Josephus problems

joeGuru
28 Nov, 2006 - 11:13 AM
Post #1

New D.I.C Head
*

Joined: 28 Nov, 2006
Posts: 1


My Contributions
// Hey,solvingAttached File  Assignment.doc ( 31k ) Number of downloads: 176
Attached File  Assignment.doc ( 31k ) Number of downloads: 176
my data structure using C assignment. I dosen't know how to use circular linked list to solve the Josephus problems. anyone know please help me. The problem i facing now is that i using most of the variable is global and i doesn't know how to link them using pointer. if anyone could modify my program, i would be grateful.last but not least. the program need a random generator to run the N number which will be picked. Below is what i have write. so help me out.

i attach a assignment question ...so pls look up for it.thank


CODE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


struct node
{
    int position;
    struct node *next;
    struct node *prev;

};


void josephus(int,int);
void eliminate(int,int,int);
struct node *head;
struct node *tail;
struct node *temp;
struct node *curr;
struct node *end;
struct node *before;
struct node *after;
struct node *out;


int main()
{
    int soldier;
    int startPos;
    int passby;
    
    
    printf("\n Enter the number of soldiers?(maximun of 50):");
    scanf("%d",&soldier);


    if(soldier>50)
    {
        printf("\n\n\t Invalid entry......\n\n");
        
    }
    printf("\n\n\t Which position would you like to start with?");
    scanf("%d",&startPos);

    printf("\n\n\t How many people do you want to skip each round? ");
    scanf("%d",&passby);

    
    josephus(soldier,startPos);
    eliminate(soldier,passby,startPos);
    scanf("%d",&soldier);
    
    
return 0;
}
void josephus(int soldier,int startPos)
{

    int x, y=1;
    head = tail = NULL;
    
    for(x = startPos; x < (soldier + startPos);x++)
    {
        if(x == startPos)
        {
             temp= (struct node *)malloc(sizeof(struct node));
             temp ->prev =NULL;
             temp ->next =NULL;
             temp ->position = x;
             head = tail = temp;
        }

        else
        {
            temp=(struct node *)malloc(sizeof(struct node));
            curr = head = tail;
            
            while(curr ->next != NULL)
            {
                curr = curr ->next;
            }

            if(x > soldier)
            {
                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++;
    }
    end = head = tail;

    while(end ->next !=NULL)
    {
        end = end ->next;
    }

    end ->next = head = tail;
    head ->prev =end;
    tail ->prev =end;
}

void eliminate(int soldier,int passby,int startPos)
{
    int x,z;
    curr = head = tail;

    for(x =1; x < soldier; x++)
    {
        for(z=1;z <=passby; z++)
        {
            curr = curr ->next;
        }
            out= curr;
            before= curr ->prev;
            after= curr ->next;

            
            printf("\n\n the person who was sitting at seat",out->position );
            before ->next = after;
            after ->prev = before;
            curr = after;
            free(out);
        }
    

        printf("\n\n the last soldier is sitting at seat", curr ->next);
}


edit: added [code] tags ~ jayman9
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: How To Use Circular Linked List To Solve The Josephus Problems
1 Dec, 2006 - 01:21 AM
Post #2

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 549



Thanked: 4 times
Dream Kudos: 125
My Contributions
after

josephus(soldier,startPos);
eliminate(soldier,passby,startPos);

you dont do anything with the result, so thats not going to work.
Let me advise you to first get your head around using pointers.
There is enough online material available to learn this.
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: How To Use Circular Linked List To Solve The Josephus Problems
1 Dec, 2006 - 01:30 AM
Post #3

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 549



Thanked: 4 times
Dream Kudos: 125
My Contributions
As for random numbers, a quick search in this forum would have given your answer.
You can try something like this:

srandomdev();

x = 1 + random() % 99;

to generate randomw numbers between 1 and 100
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 08:24PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month