我在编码方面相当新,并且使用链表和结构获得了一项艰巨的任务。任务是创建一个音乐数据库,以便您可以读取艺术家的专辑和曲目,并将它们存储在节点中。
目前,当我运行程序时,当我从 .txt 文件中读取数据时,没有存储任何节点。最终,我开始怀疑我对某些指针的使用是否正确,因为我遇到了分段错误。我做错了什么,我可以做一个修复来让它发挥作用吗?
我的代码(已包含所需的库):
结构和宏定义:
#define LINEBUFFERSIZE 256
struct song
{
char *songName_p;
int trackNumber;
struct song *nextSong_p;
};
struct disc
{
char *discName_p;
int year;
struct song *song_p;
struct disc *nextDisc_p;
};
struct artist
{
char name[20];
char *artistName_p;
struct disc *disc_p;
struct artist *nextArtist_p;
};
struct artist *end = (struct artist *) NULL; //NEW
struct artist *startPtr = (struct artist *) NULL; //NEW
struct artist *find(struct artist *, char * );//NEW
//NEW
类型定义:
typedef struct artist artist_t;
typedef struct disc disc_t;
typedef struct song song_t;
typedef struct artist *artistNodePtr;
typedef struct disc *discNodePtr;
typedef struct song *songNodePtr;
功能定义:
void InsertArtist(struct artist *New);
artistNodePtr initializenode(char *name);
artistNodePtr findOrInsertArtist(artistNodePtr *sPtr, char *name);
discNodePtr findOrInsertDisc(discNodePtr *sPtr, char *discID, int releaseYear);
void findOrInsertSong(songNodePtr *sPtr, char *songID, int trackID);
void getNextLine(char buffer[], int bufferSize, FILE *fptr);
void printlist( struct artist *ptr );
void printnode(struct artist *ptr);
主要方法:
int main(int argc, char *argv[])
{
char name[20];
struct artist *newNodePointer;
char lineBuffer[LINEBUFFERSIZE];
artistNodePtr startPtr = NULL; /* initially the artist list is empty */
FILE *musicFile;
char *artistTemp, *discTemp, *yearTemp, *trackTemp, *songTemp;
int year, track, menu = 1;
artistNodePtr theArtist;
// discNodePtr theDisc;
if (argc==1)
{
printf(" Must supply a file name as command line argument/n");
return 0;
}
if ((musicFile = fopen(argv[1], "r")) == NULL)
{
printf ("Error opening music file. Program terminated/n");
return 0;
}
getNextLine(lineBuffer, LINEBUFFERSIZE, musicFile);
while (!feof(musicFile))
{
artistTemp = strtok(lineBuffer,";");
if (artistTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
discTemp = strtok(NULL ,";");
if (discTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
yearTemp = strtok(NULL ,";");
if (yearTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
trackTemp = strtok(NULL ,";");
if (trackTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
songTemp = strtok(NULL ,"\n");
if (songTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
year = atoi(yearTemp);
track = atoi(trackTemp);
theArtist = findOrInsertArtist(&startPtr, artistTemp);
// theDisc = findOrInsertDisc(&(theArtist->disc_p), discTemp, year);
//findOrInsertSong(&(theDisc->song_p), songTemp, track);
getNextLine(lineBuffer, LINEBUFFERSIZE, musicFile);
} /* end of while loop */
while (menu != 0)
{
printf("1 to display entire catalog \n");
printf("2 to display alls songs by a given artist\n");
printf("3 to display all songs on a given disc\n");
printf("0 to exit the library\n ");
scanf("%d", &menu);
switch (menu){
case 1: printlist(startPtr);
break;
case 2:
printf("enter an artist name");
scanf("%s", name );
newNodePointer = find(startPtr, name );
if (newNodePointer==NULL)
{
newNodePointer = initializenode(name );
InsertArtist(newNodePointer);
}
}
} /* end main */
}
函数定义:
artistNodePtr findOrInsertArtist(artistNodePtr *sPtr, char *name )
{
sPtr = initializenode(name);
InsertArtist(sPtr);
if(!sPtr)
sPtr = find(startPtr, name);
return sPtr;
}
void printlist( struct artist *ptr ){
while(ptr!= NULL){
printnode(ptr);
ptr = ptr->nextArtist_p;
}
}
void printnode(struct artist *ptr)
{
printf("Name %s\n", ptr->name);
}
artistNodePtr initializenode(char *name)
{
struct artist *newNodePtr = (artistNodePtr)malloc(sizeof(artist_t));
if (newNodePtr != NULL)
{
newNodePtr->artistName_p = (char*)malloc((strlen(name)+1)*sizeof(char));
if (newNodePtr->artistName_p != NULL)
{
strcpy(newNodePtr->artistName_p, name);
newNodePtr->nextArtist_p = NULL;
newNodePtr->disc_p = NULL;
return newNodePtr;
}
}
}
void InsertArtist(struct artist *New)
{
//NEW
struct artist *temp, *prev;
if(startPtr == NULL)
{
startPtr = New;
end = New;
startPtr->nextArtist_p = NULL;
return;
}
temp = startPtr;
while(strcmp(temp->name, New->name) < 0)
{
temp = temp->nextArtist_p;
if(temp == NULL)
break;
}
if(temp == startPtr)
{
New->nextArtist_p = startPtr;
startPtr = New;
}
else
{
prev = startPtr;
while(prev->nextArtist_p != temp)
{
prev = prev->nextArtist_p;
}
prev->nextArtist_p = New;
New-> nextArtist_p = temp;
if(end == prev)
end = New;
}
}
struct artist *find(struct artist *newNodePointer, char *name)
{
//NEW
while (strcmp(name, newNodePointer->name )!=0)
{
newNodePointer = newNodePointer->nextArtist_p;
if (newNodePointer == NULL)
break;
}
return newNodePointer;
}
void getNextLine(char buffer[], int bufferSize, FILE *fptr)
{
char temp;
int i = 0;
buffer[0] = fgetc(fptr);
while ( (!feof(fptr)) && (buffer[i] != '\n') && i<(bufferSize-1))
{
i = i +1;
buffer[i]=fgetc(fptr);
}
if ((i == (bufferSize-1)) && (buffer[i] != '\n'))
{
temp = fgetc(fptr);
while (temp != '\n')
{
temp = fgetc(fptr);
}
}
buffer[i] = '\0';
}