c - 从我的动态列表中删除一个节点

标签 c

我创建了一个动态列表,其中每个节点都包含一个单词,我创建了一个函数来删除单个节点并释放该节点正在使用的内存,但是我的函数并没有删除该节点。当我键入它应该删除的世界时,我只得到“未找到”作为输出,错误出在删除函数中,但我正在努力寻找它,是否与应该循环遍历节点的 if 语句有关并找到要删除的那个?

    // 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);
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[20]; // 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("%s", 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("%s", &item);
               // if character is found, remove it
               if (delete(&startPtr, *item)) { // remove item
                  printf("%s deleted.\n", &item);
                  printList(startPtr);
               } 
               else {
                  printf("%s 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= 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);
   } 
} 

// 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("%s --> ", currentPtr->data);
         currentPtr = currentPtr->nextPtr;   
      } 
      puts("NULL\n");
   } 
} 

最佳答案

您在使用指针时遇到问题 如果为 ListNodePtr 制作了特殊的 typedef,即 ListNode*,那么请使用它。为什么需要 ListNodePtr*? 所以在某些地方你使用指向对象指针的指针 这会使您的代码更难理解。

(currentPtr != NULL && currentPtr->data != value)

而且这里的“值”是单个字符,您正试图将它与实际上是指针的“数据”进行比较 你最好使用类似 strcmp() 的东西

所以问题从函数声明的更高层开始 你不需要那样做

char delete(ListNodePtr* sPtr, char value) but that char delete(ListNodePtr* sPtr, char* value)

并且请不要使用在语言中用作关键字的词。 在这种情况下最好使用“删除”

关于c - 从我的动态列表中删除一个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51295030/

相关文章:

c - 使用结构体和数组编写函数

c - 在C中将当前目录中的所有文件列出为字符串

c - 确定进程输出是否在 C/C++ 中被重定向

我们可以通过 mmap() 分配物理上连续的内存吗?

c++ - 什么是最好的跨平台解析文本文件的方法?

c - 使用switch计算单利和复利的程序

c - 更改后自动重新编译C程序?

python - add_swig_library 的 undefined symbol

C - 服务器/客户端井字棋游戏 - 读/写索引文件?

c++ - const char myVar* 与 const char myVar[]