首页 > 解决方案 > 编写一个函数来返回节点的位置

问题描述

我正在尝试添加一个函数来在用户输入值(字符)时查找节点位置,并返回节点的位置。

我尝试按照删除功能中的步骤操作,因为它看起来很相似,但我无法弄清楚。

#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


// prototypes
void insert(ListNode* *sPtr, char value);
char delete(ListNode* *sPtr, char value);
int find(ListNode* *sPtr, char value);
void printList(ListNode* currentPtr);
void menu(void);

int main(void)
{ 
   ListNode* Head = NULL; // initially there are no nodes
   char item; // char entered by user

   menu(); // display the menu
   printf("%s", "? ");
   unsigned int choice; // user's choice
   scanf("%u", &choice);

   // loop while user does not choose 3
   while (choice != 4) { 

      switch (choice) { 
         case 1:
            printf("%s", "Enter a character: ");
            scanf("\n%c", &item);
            insert(&Head, item); // insert item in list
            printList(Head);
            break;
         case 2: // delete an element
            // if list is not empty
            if (Head != NULL) { 
               printf("%s", "Enter character to be deleted: ");
               scanf("\n%c", &item);

               // if character is found, remove it
               if (delete(&Head, item)) { // remove item
                  printf("%c deleted.\n", item);
                  printList(Head);
               } 
               else {
                  printf("%c not found.\n\n", item);
               } 
            } 
            else {
               puts("List is empty.\n");
            } 
            break;
         case 3: // find node position
            // if list is not empty
            if (Head != NULL) { 
               printf("%s", "Enter character to find position: ");
               scanf("\n%c", &item);

               printf("Element %c is at index %d", item, find(&Head,item));
            } 
            else {
               puts("List is empty.\n");
            } 
            break;
         default:
            puts("Invalid choice.\n");
            menu();
            break;
      } // end switch

      printf("%s", "? ");
      scanf("%u", &choice);
   } 

   puts("End of run.");
} 

// display program instructions to user
void menu(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 find node position\n."
      "   4 to end.");
} 

int find(ListNode* *sPtr, char value)
{
    int count = 0;
    while (sPtr != NULL || value != (*sPtr)->data)
    {
        count++;
        *sPtr = (*sPtr)->nextPtr;
    }
    return count;
}

我首先使用插入函数添加节点,例如:A -> B -> C。当用户输入 B 作为值时,我希望它返回索引 1。每当我运行下面的代码时,我都会转储分段错误核心。

标签: cfunctionstructlinked-listsingly-linked-list

解决方案


循环不正确。对于初学者而不是这个while循环

while (sPtr != NULL || value != (*sPtr)->data)

应该有以下循环

while ( *sPtr != NULL && value != (*sPtr)->data)

这个说法

    *sPtr = (*sPtr)->nextPtr;

更改您的列表。

该功能可以如下所示

int find( ListNode **sPtr, char value )
{
    int count = 0;

    while ( *sPtr != NULL && value != (*sPtr)->data )
    {
        count++;
        sPtr = &(*sPtr)->nextPtr;
    }

    return *sPtr == NULL ? - 1 : count;
}

即节点位置从 0 开始。如果未找到具有给定值的节点,则函数返回 -1。

事实上,由于函数中的列表没有更改,因此第一个参数可以声明为单个指针。在这种情况下,函数看起来像

int find( ListNode *sPtr, char value )
{
    int count = 0;

    while ( sPtr != NULL && value != sPtr->data )
    {
        count++;
        sPtr = sPtr->nextPtr;
    }

    return sPtr == NULL ? - 1 : count;
}

推荐阅读