私は二重にリンクされたリストをやっています。私の知る限り、それは機能していますが、正しい方法で行っているかどうかを確認するためにここに来ました。
反対に、これを作成したとき、二重リンクリストに関連するのではなく、構造と C ファイル間の「可視性」に関連する他の質問に出くわしました。この他の 2 つの疑問に対して別の質問を作成する必要があることを理解したら、教えてください。それ以外の場合は、お気軽に教えてください。
私のfile1.cにはこれがあります:
コード
#include <stdio.h>
#include <stdlib.h>
typedef struct team{
char *name;
char *teamPlace;
}Team;
typedef struct nodeTeam{
int numberOfTeams;
Team team;
struct nodeTeam *next;
struct nodeTeam *prev;
}NodeTeam;
int createsListOfTeams(NodeTeam **head, NodeTeam **tail);
void printListOfTeams(NodeTeam *listofTeams);
int addNodeTeamsSorted(NodeTeam *head, NodeTeam **tail, Team team);
int main()
{
NodeTeam *headEquipas,*tailEquipas;
Team eq;
/*Creates the doubly linked list*/
if(createsListOfTeams(&headEquipas,&tailEquipas)){
printf("\nError\n");
return 0;
}
/*Add the teams to the doubly linked list. At the end, all teams will be sorted by name*/
eq.name = "D team";
eq.teamPlace = "D team place";
if (addNodeTeamsSorted(headEquipas,&tailEquipas,eq)){
printf("\nError\n");
return 0;
}
eq.name = "A team";
eq.teamPlace = "A team place";
if (addNodeTeamsSorted(headEquipas,&tailEquipas,eq)){
printf("\nError\n");
return 0;
}
eq.name = "C team";
eq.teamPlace = "C team place";
if (addNodeTeamsSorted(headEquipas,&tailEquipas,eq)){
printf("\nError\n");
return 0;
}
eq.name = "B team";
eq.teamPlace = "B team place";
if (addNodeTeamsSorted(headEquipas,&tailEquipas,eq)){
printf("\nError\n");
return 0;
}
/*Will print all the teams*/
printListOfTeams(headEquipas);
return 0;
}
そして、私のfile2.cにはこれがあります
コード
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct team{
char *name;
char *teamPlace;
}Team;
typedef struct nodeTeam{
int numberOfTeams;
Team team;
struct nodeTeam *next;
struct nodeTeam *prev;
}NodeTeam;
/*Add the teams to the doubly linked list. At the end, all teams will be sorted by name*/
int createsListOfTeams(NodeTeam **head, NodeTeam **tail){
(*head) = (NodeTeam *)malloc(sizeof(NodeTeam));
if ((*head) == NULL){
return -1;
}
(*head)->numberOfTeams = 0;
(*head)->team.teamPlace = "";
(*head)->team.name = "";
(*head)->next = NULL;
(*head)->prev = NULL;
*tail = *head;
return 0;
}
/*Creates the doubly linked list*/
int addNodeTeamsSorted(NodeTeam *head, NodeTeam **tail, Team team){
NodeTeam *no,*listIni;
no = (NodeTeam*) malloc(sizeof(NodeTeam));
if (no == NULL){
return -1;
}
/*copy of my list*/
listIni = head;
no->team = team;
/*to see is it's the first element of my list*/
if(head->numberOfTeams == 0)
{
no->next = head->next;
no->prev = head;
head->next = no;
*tail = no;
}
else{ /*If not the first element*/
head = head->next;
while(head->prev != *tail && strcmp(head->team.name,no->team.name) < 0 && strcmp((*tail)->team.name,no->team.name)>0){
head = head->next;
(*tail) = (*tail)->prev;
}
if(strcmp(head->team.name,no->team.name) >= 0 || head->prev == *tail){
no->next = head;
no->prev = head->prev;
(head->prev)->next = no;
head->prev = no;
}
else if(strcmp((*tail)->team.name,no->team.name) <= 0){
no->next = (*tail)->next;
no->prev = (*tail);
(*tail)->next = no;
*tail = no;
}
}
/*Updates the number of element of the list*/
head = listIni;
head->numberOfTeams++;
return 0;
}
/*Prints my lists*/
void printListOfTeams(NodeTeam *listofTeams){
printf("| number of teams %22d |\n",listofTeams->numberOfTeams);
printf("| team name | team place |\n");
printf("--------------------------------------------------\n");
listofTeams = listofTeams->next;
while (listofTeams != NULL){
printf("| %-21s | %-22s |\n",listofTeams->team.name,listofTeams->team.teamPlace);
listofTeams = listofTeams->next;
}
printf("--------------------------------------------------\n\n");
}
だからここに私のツリーの質問があります:
Q1 - これは、先頭と末尾がそれぞれリストの先頭と末尾を指す双方向リンク リストを実装する正しい方法ですか?
Q2struct team
- ファイルの両方でとを宣言するのはなぜstruct nodeTeam
ですか? それらはすべて同じプロジェクトにあるため、宣言はプロジェクトのすべてのファイルに「表示」されるべきではありませんか?
Q3 -struct team
なぜchar *name
代わりに宣言しなければならないのchar name[31]
ですか?