c - 编写一个函数来返回节点的位置
问题描述
我正在尝试添加一个函数来在用户输入值(字符)时查找节点位置,并返回节点的位置。
我尝试按照删除功能中的步骤操作,因为它看起来很相似,但我无法弄清楚。
#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;
}
推荐阅读
- mongodb - 如何使用保存命令停止 mongodb 中的重复数据
- javascript - 加载页面内容而不重新加载整个页面
- r - 运行库(ggplot2)时如何解决错误?
- javascript - 如何将 Jquery.canvascrop 裁剪图像上传到数据库
- php - Wordpress 在博客页面上显示私人和公共帖子
- amazon-web-services - 是否有任何替代方法可以增加 aws lambda 调用时间或管道结果
- php - wordpress 上传主题 您关注的链接已过期。(已经 set_time 限制 400 )
- android - 从存储和回收视图列表中删除选定的文件
- javascript - DOM 中的节点发生变化的事件顺序是什么?
- python - 为什么它不包含我的附加列表?我试图在循环时继续向列表中添加更多项目,但是当循环继续时它消失了