Please help to make a code of the josephus problem in C using the circular linked list.

Please prove that you've made an honest attempt to do it yourself.

I have made this code but it seems like there is alot of change to be made to make it accurate and right.

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

struct node{
	char info[25];
	struct node *NEXT;
};
struct node *HEAD;
struct node *TAIL;
char name[25];

void myInit()
{
	HEAD=(struct node *) malloc(sizeof(struct node));
	TAIL=(struct node *) malloc(sizeof(struct node));
	HEAD->NEXT=TAIL;
	TAIL->NEXT=HEAD;
}

void myInsert()
{
	struct node *TEMP;
	TEMP=(struct node*)malloc(sizeof(struct node));
	strcpy(TEMP->info,name);
	TEMP->NEXT=HEAD->NEXT;
	HEAD->NEXT=TEMP;
}

void myRemove()
{
	struct node *TEMP;
	TEMP=(struct node*) malloc(sizeof(struct node));
	TEMP=HEAD->NEXT;
	HEAD->NEXT=HEAD->NEXT->NEXT;
	strcpy(name,TEMP->info);
}

void main()
{
	clrscr();
	int numSol, counter;
	myInit();
	printf("Enter the number of soldier: ");
	scanf("%d",&numSol);
	for(int i=1;i<=numSol; i++)
	{
		printf("Pls. Type a Name: ");
		scanf("%s",&name);
		myInsert();
	}
	printf("Value for Counter: ");
	scanf("%d",&counter);

	for(int j=1;j<=numSol;j++)
	{
	for(int k=1;k<=counter;k++)
	{
	myRemove();
	if(k==counter){
	puts(name);
	getch();}
	}
	}
}

Thanks for your message. I totally appreciate it. Hope you could help me. Thanks!!

1. You are making memory leaks all over the place. When you operate with dynamic memory allocation you have the responsibility to free every part of memory that you don't use.

2. Instead of void main() always write int main() 3. myRemove() isn't working. Why are you creating a new memory space for temp? And then when you have created new mem space, TEMP->info is empty!

sci@phy, did you really understand the problem?the answers you have written is vague and may be equal to knowledge of a child in programming. If you can not answer properly, do not answer it just to show that you are an intellectual.

commented: constructive criticism the name of the game +0
commented: This thread is from 2008. -1

What is the Josephus problem?

sci@phy, did you really understand the problem?the answers you have written is vague and may be equal to knowledge of a child in programming. If you can not answer properly, do not answer it just to show that you are an intellectual.

Is there a reason you resurrected a 4-year old thread just to scold someone that hasn't been here sin Oct 13th, 2010?

Let sleeping threads die...

What is the Josephus problem?

Clickity Click

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.