在我的链接列表中插入第三个节点时,我的代码似乎超时
While Inserting the 3rd node in my linked list my code seems to timeout
我正在学习 C 中的链表。所以我在网上找到了一个项目,可以帮助您体验链表。
您必须创建一个 C 文件,将一些命令作为字符串。 I 插入,P 打印,D 删除,S 搜索,Q 退出。
每当我尝试插入第三个节点并在终端中按回车键时,代码似乎超时。
可能我在某处循环连接了列表,但我无法发现错误。
我将在此感谢您的指导和支持!
int main(void)
{
// Declare the head of the linked list.
Node *head = NULL;
char *songName, *artist, *genre;
artist = (char *) malloc(MAX_LENGTH * sizeof (char));
genre = (char *) malloc(MAX_LENGTH * sizeof (char));
songName = (char*) malloc(MAX_LENGTH * sizeof (char));
// Announce at the start of the program
printf("Personal Music Library.\n\n");
printf("%s", "Commands are I (insert), D (delete), S (search by song name),\n" //I have removed the code for the other commands
"P (print), Q (quit).\n");
char response;
char input[MAX_LENGTH + 1];
do
{
inputStringFromUser("\nCommand", input, MAX_LENGTH); // Response is the first character entered by user.
response = toupper(input[0]); // Convert to uppercase to simplify later comparisons.
if (response == 'I') // Insert a song into the linked list and order it alphabeticallly by song name.
{
char *promptName = "Song name";
char *promptArtist = "Artist";
char *promptGenre = "Genre";
inputStringFromUser(promptName, songName, MAX_LENGTH);
inputStringFromUser(promptArtist, artist, MAX_LENGTH);
inputStringFromUser(promptGenre, genre, MAX_LENGTH);
if (!songAlreadyInList(head, songName))
{
head = insertNewNode(head, songName, artist, genre);
}
else
{
songNameDuplicate(songName);
}
}
} while (response != 'Q'); // Delete the entire linked list.
void inputStringFromUser(char *prompt, char *s, int maxStrLength)
{
int i = 0;
char c;
printf("%s --> ", prompt);
while (i < maxStrLength && (c = getchar()) != '\n')
s[i++] = c;
s[i] = '[=10=]';
}
Node *insertNewNode(Node *head, char songName[MAX_LENGTH], char artist[MAX_LENGTH], char genre[MAX_LENGTH])
{
Node *next = head; //to insert first node
if (head == NULL || strcmp(head -> songName, songName) > 0)
{
Node *insert = (Node *) malloc( sizeof(Node));
insert -> songName = (char *) malloc(MAX_LENGTH * sizeof(char));
insert -> artist = (char *) malloc(MAX_LENGTH * sizeof(char));
insert -> genre = (char *) malloc(MAX_LENGTH * sizeof(char));
if (insert != NULL)
{
strcpy(insert -> songName, songName);
strcpy(insert -> artist, artist);
strcpy(insert -> genre, genre);
insert -> next = next;
}
return insert;
}
Node *current = head;
//to insert in the order
while (current -> next != NULL && strcmp(current -> next -> songName, songName) < 0)
{
current = current -> next;
}
Node *insert = (Node *) malloc(sizeof (Node));
insert -> songName = (char *) malloc(MAX_LENGTH * sizeof (char));
insert -> artist = (char *) malloc(MAX_LENGTH * sizeof (char));
insert -> genre = (char *) malloc(MAX_LENGTH * sizeof (char));
if (insert != NULL)
{
strcpy(insert -> songName, songName);
strcpy(insert -> artist, artist);
strcpy(insert -> genre, genre);
insert -> next = next;
}
current -> next = insert;
return head;
}
这里是修复了以下问题的有效实现:
- 通过使用单独的函数消除节点创建的重复。这也使得指针调整更容易遵循,因为它现在只是几行代码。
- 无需强制转换 mallocreturn 的值
- 检查所有 malloc returns 并处理清理(不泄漏内存)
- 你更新了head,但是你需要更新head指向的地方(所以需要head的地址)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 10
typedef struct Node Node;
struct Node {
char *songName;
Node *next;
};
Node *createNode(char songName[MAX_LENGTH]) {
Node *newNode = malloc(sizeof(*newNode));
if(!newNode) {
printf("malloc failed\n");
return NULL;
}
// simplifies error handling with more attributes
newNode->songName = NULL;
newNode->songName = malloc(MAX_LENGTH);
if(!newNode->songName) {
printf("malloc failed\n");
goto err;
}
strcpy(newNode->songName, songName);
newNode->next = NULL;
return newNode;
err:
free(newNode->songName);
free(newNode);
return 0;
}
Node *insertNode(Node **head, Node *newNode) {
if(!head || !newNode) return NULL;
if(!*head) {
*head = newNode;
} else {
Node *current;
for(current = *head; current->next; current = current->next);
current->next = newNode;
}
return *head;
}
void printNodes(Node *head) {
for(Node *current = head; current; current = current->next) {
printf("%s\n", current->songName);
}
}
int main() {
Node *head = NULL;
insertNode(&head, createNode("hello"));
insertNode(&head, createNode("world"));
printNodes(head);
}
我正在学习 C 中的链表。所以我在网上找到了一个项目,可以帮助您体验链表。
您必须创建一个 C 文件,将一些命令作为字符串。 I 插入,P 打印,D 删除,S 搜索,Q 退出。
每当我尝试插入第三个节点并在终端中按回车键时,代码似乎超时。
可能我在某处循环连接了列表,但我无法发现错误。
我将在此感谢您的指导和支持!
int main(void)
{
// Declare the head of the linked list.
Node *head = NULL;
char *songName, *artist, *genre;
artist = (char *) malloc(MAX_LENGTH * sizeof (char));
genre = (char *) malloc(MAX_LENGTH * sizeof (char));
songName = (char*) malloc(MAX_LENGTH * sizeof (char));
// Announce at the start of the program
printf("Personal Music Library.\n\n");
printf("%s", "Commands are I (insert), D (delete), S (search by song name),\n" //I have removed the code for the other commands
"P (print), Q (quit).\n");
char response;
char input[MAX_LENGTH + 1];
do
{
inputStringFromUser("\nCommand", input, MAX_LENGTH); // Response is the first character entered by user.
response = toupper(input[0]); // Convert to uppercase to simplify later comparisons.
if (response == 'I') // Insert a song into the linked list and order it alphabeticallly by song name.
{
char *promptName = "Song name";
char *promptArtist = "Artist";
char *promptGenre = "Genre";
inputStringFromUser(promptName, songName, MAX_LENGTH);
inputStringFromUser(promptArtist, artist, MAX_LENGTH);
inputStringFromUser(promptGenre, genre, MAX_LENGTH);
if (!songAlreadyInList(head, songName))
{
head = insertNewNode(head, songName, artist, genre);
}
else
{
songNameDuplicate(songName);
}
}
} while (response != 'Q'); // Delete the entire linked list.
void inputStringFromUser(char *prompt, char *s, int maxStrLength)
{
int i = 0;
char c;
printf("%s --> ", prompt);
while (i < maxStrLength && (c = getchar()) != '\n')
s[i++] = c;
s[i] = '[=10=]';
}
Node *insertNewNode(Node *head, char songName[MAX_LENGTH], char artist[MAX_LENGTH], char genre[MAX_LENGTH])
{
Node *next = head; //to insert first node
if (head == NULL || strcmp(head -> songName, songName) > 0)
{
Node *insert = (Node *) malloc( sizeof(Node));
insert -> songName = (char *) malloc(MAX_LENGTH * sizeof(char));
insert -> artist = (char *) malloc(MAX_LENGTH * sizeof(char));
insert -> genre = (char *) malloc(MAX_LENGTH * sizeof(char));
if (insert != NULL)
{
strcpy(insert -> songName, songName);
strcpy(insert -> artist, artist);
strcpy(insert -> genre, genre);
insert -> next = next;
}
return insert;
}
Node *current = head;
//to insert in the order
while (current -> next != NULL && strcmp(current -> next -> songName, songName) < 0)
{
current = current -> next;
}
Node *insert = (Node *) malloc(sizeof (Node));
insert -> songName = (char *) malloc(MAX_LENGTH * sizeof (char));
insert -> artist = (char *) malloc(MAX_LENGTH * sizeof (char));
insert -> genre = (char *) malloc(MAX_LENGTH * sizeof (char));
if (insert != NULL)
{
strcpy(insert -> songName, songName);
strcpy(insert -> artist, artist);
strcpy(insert -> genre, genre);
insert -> next = next;
}
current -> next = insert;
return head;
}
这里是修复了以下问题的有效实现:
- 通过使用单独的函数消除节点创建的重复。这也使得指针调整更容易遵循,因为它现在只是几行代码。
- 无需强制转换 mallocreturn 的值
- 检查所有 malloc returns 并处理清理(不泄漏内存)
- 你更新了head,但是你需要更新head指向的地方(所以需要head的地址)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 10
typedef struct Node Node;
struct Node {
char *songName;
Node *next;
};
Node *createNode(char songName[MAX_LENGTH]) {
Node *newNode = malloc(sizeof(*newNode));
if(!newNode) {
printf("malloc failed\n");
return NULL;
}
// simplifies error handling with more attributes
newNode->songName = NULL;
newNode->songName = malloc(MAX_LENGTH);
if(!newNode->songName) {
printf("malloc failed\n");
goto err;
}
strcpy(newNode->songName, songName);
newNode->next = NULL;
return newNode;
err:
free(newNode->songName);
free(newNode);
return 0;
}
Node *insertNode(Node **head, Node *newNode) {
if(!head || !newNode) return NULL;
if(!*head) {
*head = newNode;
} else {
Node *current;
for(current = *head; current->next; current = current->next);
current->next = newNode;
}
return *head;
}
void printNodes(Node *head) {
for(Node *current = head; current; current = current->next) {
printf("%s\n", current->songName);
}
}
int main() {
Node *head = NULL;
insertNode(&head, createNode("hello"));
insertNode(&head, createNode("world"));
printNodes(head);
}