-2
我只是想在二叉搜索樹中addAlbum
,但樹會根據發佈年份構建。我寫的代碼,但它不運行...二叉搜索樹插入
#include<stdio.h>
#include<stdlib.h>
typedef struct treeNode {
int releaseYear;
char singerName[50];
char albumTitle[50];
struct treeNode *left;
struct treeNode *right;
} treeNode;
treeNode *addAlbum(treeNode *node,int releaseYear,char singerName[50],char albumTitle[50]) {
if(node==NULL) {
treeNode *temp;
temp=(treeNode *)malloc(sizeof(treeNode));
temp -> releaseYear=releaseYear;
temp -> singerName[50]=singerName[50];
temp -> albumTitle[50]=albumTitle[50];
temp ->left = NULL;
temp ->right = NULL;
return temp;
}
if(releaseYear > (node -> releaseYear)) {
node ->right=addAlbum(node->right,releaseYear ,singerName,albumTitle);
}
else if(releaseYear<(node -> releaseYear)) {
node ->left=addAlbum(node->left,releaseYear, singerName,albumTitle);
}
else {
return node;
}
}
int main() {
treeNode *root;
int releaseYear;
char singerName[50];
char albumTitle[50];
root=addAlbum(root,1995,"a","d");
root=addAlbum(root,1998,"b","c");
printf("singers = s\n",singerName[50]);
printf("albumTitles = %c\n",albumTitle[50]);
printf("years = %d\n",releaseYear);
return 0;
}
請了解如何使用調試器。 –
...並編譯並啓用警告('-Wall'到編譯器)。 – Arkku
你不應該在最後釋放分配的空間嗎? – kero