im new to programming and learning data structures at the moment, i am working on a program that takes input from a file in the following format :
*TEAM1
1-player1
2-player2
3-player3
4-player4
5-player5
6-player6
7-player7
8-player8
9-player9
10-player10
11-player11
12-player12
13-player13
14-player14
15-player15
16-player16
17-player17
18-player18
19-player19
20-player20
21-player21
22-player22
*TEAM2
1-player1
2-player2
3-player3
4-player4
5-player5
6-player6
7-player7
8-player8
9-player9
10-player10
11-player11
12-player12
13-player13
14-player14
15-player15
16-player16
17-player17
18-player18
19-player19
20-player20
21-player21
22-player22
23-player23
24-player24
25-player25
im using a nested linked list because I need each team have its own linked list of players, and if there are more than 11 players in the team they will be inserted into a queue for the same team only
these are the linked lists I created:
typedef struct PLAYERS{
int num;
char name [50];
struct PLAYERS* next;
}PLAYERS;
typedef struct QUEUE{
PLAYERS* front;
PLAYERS* rear;
}QUEUE;
typedef struct teamList{
char code [50];
PLAYERS* player;
QUEUE* playerQueue;
struct teamList* next;
}teamList;
my issue Right now is when I try to insert values from the file in the "Player" linked list part of the nested linked list. it seems like it only saves the latest value inserted into it only, that was made clear to me when I tried to print the contents of the player linked list for a specific team and it only printed the last inserted value, which was 11-player11
, then when I tried to print the next value in the list the program finished executing with a NULL value, which im assuming means I tried to print the NULL value the list is pointing at.
here are the parts of the code related to this issue:
teamList* MakeEmptyList(teamList* L) {
L = (teamList*)malloc(sizeof(teamList));
if(L == NULL)
printf("Out of memory!\n");
L->player = makePlayer(L->player); ;
L->playerQueue = makeQueue(L->playerQueue);
L->next = NULL;
return L;
}
PLAYERS* makePlayer(PLAYERS* player){
player = (PLAYERS*)malloc(sizeof(PLAYERS));
player->next = NULL;
return player;
}
void readFile(teamList* team){
teamList* temp = team;
FILE *f = fopen("teamsinfo.txt","r");
int i, counter = 1;
char playerName [50];
char teamCode [50];
if(fscanf(f," *%s",teamCode)> 0){
printf("*%s\n", teamCode);
strcpy(temp->code,teamCode);
}
while(!feof(f)){
if(fscanf(f," *%s",teamCode)> 0){
counter = 1;
temp->next = MakeEmptyList(temp->next);
temp =temp->next;
strcpy(temp->code,teamCode);
}
else if(counter<12){
if(fscanf(f," %d-%s",&i,playerName)> 0){
temp->player = insert(playerName,i,temp->player);
temp->player = temp->player->next;
}
counter++;
}
else if(counter >11){
if(fscanf(f," %d-%s",&i,playerName)> 0){
enQueue(temp->playerQueue,playerName,i);
}
}
}
fclose(f);
}
PLAYERS* insert(char x[],int num, PLAYERS* p) {
PLAYERS* temp ;
temp = (PLAYERS*)malloc(sizeof (PLAYERS));
strcpy(temp->name,x);
temp->num = num;
temp->next=p->next;
p->next=temp;
return p;
}
how can this be solved?