如何将新节点添加到字母顺序的链接列表
我正在尝试使用链接列表来构建一个音乐库,每首歌曲都有三个标签,即歌曲名称,艺术家和类型。因此,我将用户的输入添加到链接列表中,并以其歌曲名称字母顺序排列。例如,如果我有3首歌曲,无论艺术家和类型如何,歌曲名称是Alpha,Zebra和Cool Pime,那么链接的列表应该按以下顺序:Alpha,Cool Time,Zebra。
以下是我的代码,但是如果第一个字母的ASCII值大于头部,则不会将新节点添加到正确的位置。它仅在头为null时或所有其他节点的ASCII值均为< =对新节点的ASCII值时起作用。 非常感谢您的帮助。
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
char *artist;
char *songName;
char *genre;
struct node *next;
} Node;
Node *insert (Node * Head, char *art, char *song, char *gen);
void print (Node * Head);
int main() {
char art[25], song[25], gen[25];
Node *Head = NULL;
for (int j = 0; j < 5; j++) {
printf("Artst --> ");
gets(art);
printf("Song name --> ");
gets(song);
printf("Genre --> ");
gets(gen);
Head = insert (Head, art, song, gen);
}
printf("The elements are:");
print(Head);
}
Node *insert(Node * Head, char *art, char *song, char *gen) {
Node *newelement;
newelement = (Node *)malloc(sizeof(Node));
newelement->artist = malloc(strlen(art) + 1);
strcpy(newelement->artist, art);
newelement->songName = malloc(strlen(song) + 1);
strcpy(newelement->songName, song);
newelement->genre = malloc(strlen(gen) + 1);
strcpy(newelement->genre, gen);
Node *check;
check = (Node *)malloc(sizeof(Node));
if (Head == NULL) {
Head = newelement;
Head->next = NULL;
}
else {
check = Head;
char this = newelement->songName[0];
char that = check->songName[0];
//"this" is the value of the first letter of the new songName;
//"that" is the value of the first letter of the songName we are comparing to;
//when "this" has a smaller value then that (ex. this is A, and that is B), then this should go before that.
//if the head of the linked list has a letter that has a greatere ASCII value, then the newelement should be the new head
if (this <= that) {
newelement->next = Head;
Head = newelement;
return Head;
}
else if (this>that) {
while (check->next != NULL) {
check = check->next;
that = check->songName[0];
if (this <= that)
break;
}
}
Node *temp = check->next;
check->next = newelement;
newelement->next = temp;
}
return Head;
}
void print(Node * Head) {
while (Head != NULL) {
printf("\n");
printf("%s\n", Head->artist);
printf("%s\n", Head->songName);
printf("%s\n", Head->genre);
printf("\n");
Head = Head->next;
}
}
I am trying to build a music library with five songs using a linked list, and each song has three tags, the songName, the Artist and the Genre. So I take users' input and add it to the linked list, alphabetically with its songName. For example, if I have 3 songs, regardless of the artists and the genre, the song names are Alpha, Zebra and Cool time, then the linked list should be in this order: Alpha, Cool time, Zebra.
The following is my code, but it does not add the new node to the right place if the first letter has an ASCII value that is larger than the Head. It only works when the Head is NULL, or when the ASCII values of all other nodes are <= to that of the new node.
Thanks a lot for helping.
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
char *artist;
char *songName;
char *genre;
struct node *next;
} Node;
Node *insert (Node * Head, char *art, char *song, char *gen);
void print (Node * Head);
int main() {
char art[25], song[25], gen[25];
Node *Head = NULL;
for (int j = 0; j < 5; j++) {
printf("Artst --> ");
gets(art);
printf("Song name --> ");
gets(song);
printf("Genre --> ");
gets(gen);
Head = insert (Head, art, song, gen);
}
printf("The elements are:");
print(Head);
}
Node *insert(Node * Head, char *art, char *song, char *gen) {
Node *newelement;
newelement = (Node *)malloc(sizeof(Node));
newelement->artist = malloc(strlen(art) + 1);
strcpy(newelement->artist, art);
newelement->songName = malloc(strlen(song) + 1);
strcpy(newelement->songName, song);
newelement->genre = malloc(strlen(gen) + 1);
strcpy(newelement->genre, gen);
Node *check;
check = (Node *)malloc(sizeof(Node));
if (Head == NULL) {
Head = newelement;
Head->next = NULL;
}
else {
check = Head;
char this = newelement->songName[0];
char that = check->songName[0];
//"this" is the value of the first letter of the new songName;
//"that" is the value of the first letter of the songName we are comparing to;
//when "this" has a smaller value then that (ex. this is A, and that is B), then this should go before that.
//if the head of the linked list has a letter that has a greatere ASCII value, then the newelement should be the new head
if (this <= that) {
newelement->next = Head;
Head = newelement;
return Head;
}
else if (this>that) {
while (check->next != NULL) {
check = check->next;
that = check->songName[0];
if (this <= that)
break;
}
}
Node *temp = check->next;
check->next = newelement;
newelement->next = temp;
}
return Head;
}
void print(Node * Head) {
while (Head != NULL) {
printf("\n");
printf("%s\n", Head->artist);
printf("%s\n", Head->songName);
printf("%s\n", Head->genre);
printf("\n");
Head = Head->next;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于初学者来说,这种内存分配
没有意义,并且会产生内存泄漏,因为分配的内存没有被使用,也没有被释放。
在此代码片段中,
歌曲名称小于节点
check
的歌曲名称的新节点将插入到节点check
后面。此外,函数
gets
并不安全,并且不受 C 标准支持。而是使用
scanf
(例如)或
fgets
(例如)该函数可以按以下方式声明和定义。
请注意,作为字符串指针的参数应具有限定符 <代码>常量。
For starters this memory allocation
does not make a sense and produces a memory leak because the allocated memory is not used and is not freed.
In this code snippet
the new node with song name less than the song name of the node
check
is inserted after the nodecheck
.Also the function
gets
is not safe and is not supported by the C Standard.Instead use either
scanf
like for exampleor
fgets
likeThe function can be declared and defined the following way
Pay attention to that the parameters that are pointers to strings should have the qualifier
const
.