写一个函数到节点的return位置
Writing a function to return position of node
我正在尝试添加一个函数来在用户输入值(字符)时查找节点位置,以及 return 节点的位置。
我尝试按照删除功能中的步骤操作,因为它看起来很相似,但我无法弄清楚。
#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 作为值时,我希望它 return 索引 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开始。如果没有找到具有给定值的节点,则函数returns -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;
}
我正在尝试添加一个函数来在用户输入值(字符)时查找节点位置,以及 return 节点的位置。
我尝试按照删除功能中的步骤操作,因为它看起来很相似,但我无法弄清楚。
#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 作为值时,我希望它 return 索引 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开始。如果没有找到具有给定值的节点,则函数returns -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;
}