我正在尝试创建一个动态列表,该列表应读取输入字符串并将每个字符串保存到lis的新节点中。该程序将打印每个节点,之后我想添加一个函数来删除节点,但是我遇到了很多错误,主要是因为我不太擅长使用指针并且我对数据结构不熟悉,有人可以帮我吗?
编辑:我更正了代码,现在我不再遇到错误了,但是程序似乎为每个节点打印一个字符,而不是为每个节点打印一个字符串,这是当前输出:
// Inserting and deleting nodes in a list
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// self-referential structure
struct listNode {
char *data; // each listNode contains a character
struct listNode *nextPtr; // pointer to next node
};
typedef struct listNode ListNode; // synonym for struct listNode
typedef ListNode *ListNodePtr; // synonym for ListNode*
// prototypes
void insert(ListNodePtr *sPtr, char *value);
int isEmpty(ListNodePtr sPtr);
void printList(ListNodePtr currentPtr);
void instructions(void);
int main(void)
{
ListNodePtr startPtr = NULL; // initially there are no nodes
char item; // char entered by user
instructions(); // display the menu
printf("%s", "? ");
unsigned int choice; // user's choice
scanf("%u", &choice);
// loop while user does not choose 3
while (choice != 3) {
switch (choice) {
case 1:
printf("%s", "Enter a character: ");
scanf("%c", &item);
insert(&startPtr, &item); // insert item in list
printList(startPtr);
break;
default:
puts("Invalid choice.\n");
instructions();
break;
} // end switch
printf("%s", "? ");
scanf("%u", &choice);
}
puts("End of run.");
}
// display program instructions to user
void instructions(void)
{
puts("Enter your choice:\n"
" 1 to insert an element into the list.\n"
" 2 to delete an element from the list.\n"
" 3 to end.");
}
// insert a new value into the list in sorted order
void insert(ListNodePtr *sPtr, char *value)
{
ListNodePtr newPtr = malloc(sizeof(ListNode)); // create node
if (newPtr != NULL) { // is space available
newPtr->data= malloc(strlen(value)+1);
strcpy(newPtr->data, value);
newPtr->nextPtr = NULL; // node does not link to another node
ListNodePtr previousPtr = NULL;
ListNodePtr currentPtr = *sPtr;
// loop to find the correct location in the list
while (currentPtr != NULL && value > currentPtr->data) {
previousPtr = currentPtr; // walk to ...
currentPtr = currentPtr->nextPtr; // ... next node
}
// insert new node at beginning of list
if (previousPtr == NULL) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else { // insert new node between previousPtr and currentPtr
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else {
printf("%s not inserted. No memory available.\n", value);
}
}
int isEmpty(ListNodePtr sPtr)
{
return sPtr == NULL;
}
// print the list
void printList(ListNodePtr currentPtr)
{
// if list is empty
if (isEmpty(currentPtr)) {
puts("List is empty.\n");
}
else {
puts("The list is:");
// while not the end of the list
while (currentPtr != NULL) {
printf("%s --> ", currentPtr->data);
currentPtr = currentPtr->nextPtr;
}
puts("NULL\n");
}
}
以下是当前输出的示例:
./test
Enter your choice:
1 to insert an element into the list.
2 to delete an element from the list.
3 to end.
? 1
Enter a character: The list is:
--> NULL
? Test
Enter a character: The list is:
--> T --> NULL
? Enter a character: The list is:
--> T --> e --> NULL
? Enter a character: The list is:
--> T --> e --> s --> NULL
? Enter a character: The list is:
--> T --> e --> s --> t --> NULL
?
我想去的地方,例如:单词一->单词二等。
答案 0 :(得分:3)
要获取用户输入,您正在使用
scanf("%c", &item);
这只会从他们的输入中得到一个字符。如果要获取字符串,可以将item
更新为字符数组:
char item[20];
我还相信fgets
更好地获取用户输入的字符串,如here所述。因此,使用以下代码可能会使您的程序更持久:
fgets(item, sizeof(item), stdin);
这些更改之后,输出如下:
Enter your choice:
1 to insert an element into the list.
2 to delete an element from the list.
3 to end.
? 1
Enter a character: The list is:
--> NULL
? Testing
Enter a character: The list is:
--> Testing
--> NULL
? What
Enter a character: The list is:
--> Testing
--> What
--> NULL
? About
Enter a character: The list is:
--> Testing
--> What
--> About
--> NULL
? This
Enter a character: The list is:
--> Testing
--> What
--> About
--> This
--> NULL
?
编辑:问题的代码中已经解决了以下问题。
newPtr->data
是char
,但是似乎您希望它是一个字符串。您应该更改结构定义,以使data
改为char *
。
如果您尝试打印字符串而不是单个字符,则应替换:
printf("%c not inserted. No memory available.\n", value);
// as well as
printf("%c --> ", currentPtr->data);
使用:
printf("%s not inserted. No memory available.\n", value);
// and
printf("%s --> ", currentPtr->data);
%c
用于格式化单个字符,而%s
用于打印字符串,并且必须与char *
匹配