gpt4 book ai didi

c - 编写一个函数来返回节点的位置

转载 作者:太空宇宙 更新时间:2023-11-04 04:09:55 24 4
gpt4 key购买 nike

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

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

#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。每当我运行下面的代码时,我都会收到段错误核心转储。

最佳答案

循环不正确。对于初学者而不是这个 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;
}

关于c - 编写一个函数来返回节点的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58463942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com