R
Roy J
Hi all :
I'm a newbie of c language , I confronted with a problem as below :
#include <stdio.h>
#include <stdlib.h>
typedef struct player {
int number;
struct player *next;
}player;
player *player_init(int n) {
/* Create a loop linked list and initialization */
int i;
player *head,*py;
head = malloc(sizeof *head);
if (! head)
puts("malloc error !");
head->number = n;
head->next = NULL;
for (i = n-1; i > 0; i--) {
py = malloc(sizeof *head);
if (! py)
puts("malloc error !");
py->number = i;
py->next = head->next;
head->next = py;
if (i == 1)
py->next = head;
}
return head;
}
int main(void)
{
int n;
player *head,*tmp;
puts("input the number of player :");
scanf("%d",&n);
head = player_init(n);
for (tmp = head; tmp->next = head; tmp++)
printf("the node #%d\n",(*tmp).number);
return 0;
}
I'm a newbie of c language , I confronted with a problem as below :
#include <stdio.h>
#include <stdlib.h>
typedef struct player {
int number;
struct player *next;
}player;
player *player_init(int n) {
/* Create a loop linked list and initialization */
int i;
player *head,*py;
head = malloc(sizeof *head);
if (! head)
puts("malloc error !");
head->number = n;
head->next = NULL;
for (i = n-1; i > 0; i--) {
py = malloc(sizeof *head);
if (! py)
puts("malloc error !");
py->number = i;
py->next = head->next;
head->next = py;
if (i == 1)
py->next = head;
}
return head;
}
int main(void)
{
int n;
player *head,*tmp;
puts("input the number of player :");
scanf("%d",&n);
head = player_init(n);
for (tmp = head; tmp->next = head; tmp++)
printf("the node #%d\n",(*tmp).number);
return 0;
}