Can anyone tell me what is wrong with my code?
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct Josephous
{
int pos;
struct Josephous *next;
}node;
node *create(node*,int);
int Calculate(node*,int ,int);
int main()
{
int n,m,res;
node *head;
printf("Enter no. of players = ");
scanf("%d",&n);
printf("Enter position to eliminate = ");
scanf("%d",&m);
head=(node*)malloc(sizeof(node));
head->pos=1;
head->next=head;
head=create(head,n);
res=calculate(head,n,m);
printf("The winner is = %d",res);
return 0;
}
node *create(node *head,int n)
{
node *q,*temp;
int i;
q=head;
for(i=2;i<=n;i++)
{
temp=(node*)malloc(sizeof(node));
temp->pos=i;
temp->next=q;
q->next=temp;
q=temp;
}
return head;
}
int calculate(node *head,int n,int m)
{
node *q,*temp;
int i;
while(head->next!=head)
{
q=head;
temp=q;
for(i=1;i<m;i++)
{
temp=q;
q=q->next;
}
temp->next=q->next;
head=temp->next;
q->next=NULL;
free(q);
q=NULL;
}
return (head->pos);
}