所以我设法创建了一个动态列表,目前我只能在每个节点中插入一个字符,我可以删除一个节点并打印整个列表,但是我在尝试更改列表时遇到了很多麻烦,以便我可以在每个节点中插入一个动态数组并将其打印到文件中,有人能给我一些建议吗?
#include <stdio.h>
#include <stdlib.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);
char delete(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("\n%c", &item);
insert(&startPtr, item); // insert item in list
printList(startPtr);
break;
case 2: // delete an element
// if list is not empty
if (!isEmpty(startPtr)) {
printf("%s", "Enter character to be deleted: ");
scanf("\n%c", &item);
// if character is found, remove it
if (delete(&startPtr, item)) { // remove item
printf("%c deleted.\n", item);
printList(startPtr);
}
else {
printf("%c not found.\n\n", item);
}
}
else {
puts("List is empty.\n");
}
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 = value; // place value in node
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("%c not inserted. No memory available.\n", value);
}
}
// delete a list element
char delete(ListNodePtr *sPtr, char value)
{
// delete first node if a match is found
if (value == (*sPtr)->data) {
ListNodePtr tempPtr = *sPtr; // hold onto node being removed
*sPtr = (*sPtr)->nextPtr; // de-thread the node
free(tempPtr); // free the de-threaded node
return value;
}
else {
ListNodePtr previousPtr = *sPtr;
ListNodePtr currentPtr = (*sPtr)->nextPtr;
// loop to find the correct location in the list
while (currentPtr != NULL && currentPtr->data != value) {
previousPtr = currentPtr; // walk to ...
currentPtr = currentPtr->nextPtr; // ... next node
}
// delete node at currentPtr
if (currentPtr != NULL) {
ListNodePtr tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free(tempPtr);
return value;
}
}
return '\0';
}
// return 1 if the list is empty, 0 otherwise
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("%c --> ", currentPtr->data);
currentPtr = currentPtr->nextPtr;
}
puts("NULL\n");
}
}
您必须打开编译器的警告。它会告诉你哪里违反了 C 的某些规则。例如,newPtr->data = value;
应该抱怨因为它data
是一个指针,但它value
是一个字符。
但是,您应该使用"%s"
格式说明符从用户读取字符串scanf
到足够大的缓冲区(或在读取时给出边界),例如缓冲区char strbuf[256];
。
您将此缓冲区传递给insert
. 不是将其分配value
给data
,而是首先分配一个新字符串,然后将值复制到新字符串:
newPtr->data = malloc(strlen(value)+1);
strcpy(newPtr->data, value);
(此答案不包括您的解决方案可能存在的任何其他错误或问题。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句