Hey! I'm pretty much a newbie when it comes to C programming. I have a problem with lists to solve. I have ideas, I wrote a code. I believe the idea is pretty good but somehow I can't get it to work. I don't get any compile errors, I just get no results and at some point in the execution I get an error that forces me to stop.
I'll try to be short with the requirement of the problem. Kids sit in a circle - one of them starts counting to k -> and this kth person is eliminated. Eventually I have to remain only with one child.
Now...my idea. I placed the names of the kids in a file and in a circular list I simply put numbers from 1 to n representing the kids. I'd remain with one number than I'd take the name of this person from the file. But this last part isn't written yet, only the creation of the list and the game.
Code:
#include <conio.h>
#include <string.h>
#include <stdio.h>
typedef struct el
{
unsigned no;
struct el *next ;
} *adr;
unsigned n,nr;
adr bg,end;
void create(adr &bg, adr &end)
{
adr crt;
unsigned nr;
nr=1;
while (nr<=n)
{
crt=new el;
crt->no=n;
if (!bg) bg=crt;
else end->next=crt;
crt->next=NULL;
end=crt;
nr=nr+1;
}
}
void print_list(adr bg)
{
adr crt;
crt=bg;
while (crt)
{
printf("%d ",crt->no);
crt=crt->next;
}
}
void game(unsigned nr, adr &bg)
{
adr crt,r;
unsigned i;
crt=bg;
while (crt->next!=bg)
crt=crt->next;
while (bg->next!=bg)
{
for (i=1;i<nr;i++)
crt=crt->next;
r=crt->next;
crt->next=crt->next->next;
if (r=bg) bg=bg->next;
delete(r);
}
}
int main()
{
char ch,s[100],fil_nm[50]="filedir1.txt",aux[20],*gf;
char arr[100][100],name[20][20];
FILE *pf;
pf=fopen(fil_nm,"w");
printf("\nplease input the information about the files");
printf("\nthe information about a file will be on a line");
printf("\nEnd with CTRL+Z\n");
while ((ch=getc(stdin))!=EOF)
{
putc(ch, pf);
if (int(ch)==13) n=n+1;
}
fclose(pf);
create(bg,end);
print_list(bg);
printf("\nPlease chose the number of the child that will start the game: ");
scanf("%d",&nr);
game(nr,bg);
print_list(bg);
getch();
}