如何在 C 语言中将器官添加到链表中的特定位置



我有一个链表,有两个给定的单词。我需要找到列表中第一个单词的索引,然后在第一个单词之后立即添加第二个单词。在 C 语言中 怎么办? 感谢所有的帮助! 这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 20
typedef struct personNode
{
char name[SIZE];
int age;
struct personNode* next;
}personNode;
struct personNode* newHead;
personNode* createSong(char name[], int age);
void printList(personNode* head);
void insertAtEnd(personNode** head, personNode* newNode, char friend[]);
void deleteNode(personNode** head, char* name);
void freeList(personNode** head);
void lenght(personNode* p);
void insertAtStart(char name[], int age);
void search(personNode* head, char name[]);
void reverse(personNode** head);
int main(void)
{
personNode* first = NULL;
char name[SIZE] = { 0 };
char friend[SIZE] = { 0 };
int age = 0;
int choice = 0;
int i = 0;
int bigPlace = 0;
printf("nWelcome to MagshiParty Line Management Software!n");
do {
printf("Please enter your choice from the following options:n1 - Print linen2 - Add person to linen3 - Remove person from linen4 - VIP guestn5 - Search in linen6 - Reverse linen7 - Exitn");
scanf("%d", &choice); //ask option from the user
switch (choice)
{
case (1):
lenght(newHead);
printList(newHead);
break;
case (2):
getchar();
printf("Welcome guest!n");
printf("Enter name: ");
fgets(name, SIZE, stdin);
name[strlen(name) - 1] = '';
printf("Enter age: ");
scanf("%d", &age);
getchar();
printf("Enter names of 3 friends:n");
for (i = 0;i <3; i++)
{
printf("Friend %d:", i + 1);
fgets(friend, SIZE, stdin);
friend[strlen(friend) - 1] = '';
}
first = createSong(name, age);
insertAtEnd(&newHead, first, friend);
break;
case (3):
getchar();
printf("Enter name to remove:n");
fgets(name, SIZE, stdin);
name[strlen(name) - 1] = '';
deleteNode(&newHead, name);
break;
case (4):
getchar();
printf("VIP GUEST!n");
printf("Enter name: ");
fgets(name, SIZE, stdin);
name[strlen(name) - 1] = '';
printf("Enter age: ");
scanf("%d" ,&age);
insertAtStart(name, age);
break;
case (5):
getchar();
printf("Enter name to search:n");
fgets(name, SIZE, stdin);
name[strlen(name) - 1] = '';
search(newHead, name);
break;
case (6):
reverse(&newHead);
printf("Line reversed!n");
break;
case (7):
printf("GoodBye");
}
}while(choice!=7);
getchar();
getchar();
return 0;
}

void insertAtEnd(personNode** head, personNode* newNode, char friend[])
{
personNode* current = head;
while (current != NULL) 
{ 
if (0 == strcmp(current->name, friend))
{
personNode* nxt=current->next; //presently the node which is next to current node
current->next = newNode;        //now current node will point to new node
newNode->next=nxt;              // new node will point to the node that was infront of current node
return;
}
current = current->next; 
}
personNode* curr = *head;
if (!*head) // empty list!
{
*head = newNode;
}
else
{
while (curr->next) // while the next is NOT NULL (when next is NULL - that is the last node)
{
curr = curr->next;
}
curr->next = newNode;
newNode->next = NULL;
}
}

这是我的代码,我有更多的功能,但它们也不是相关的。 我没有成功了解问题是什么以及为什么它没有把 以朋友的名字命名

personNode* current = head;
while (current != NULL) 
{ 
if (0 == strcmp(current->name, friend))
{
personNode* nxt=current->next; //presently the node which is next to current node
current->next = newNode;        //now current node will point to new node
newNode->next=nxt;              // new node will point to the node that was infront of current node
return;
}
current = current->next; 
}

上面的代码将在单词匹配的节点旁边添加 newNode。

我有一个链表,有两个给定的单词。我需要找到索引 列表中的第一个单词,然后立即添加第二个单词 在第一个单词之后

如果是这样,那么函数名称insertAtEnd非常混乱。

对于初学者,编译器将在函数中为此语句发出错误

personNode* current = head;  // Initialize current 

因为声明的指针具有personNode *类型,而初始值设定项具有personNode **类型,并且没有从一种指针类型到另一种指针类型的隐式转换。

void insertAtEnd(personNode** head, personNode* newNode, char friend[])
{
personNode* current = head;  // Initialize current 
//... 

此外,如果为空列表调用该函数,则该函数将具有未定义的行为。

该函数可以如下所示

int insertAtEnd( personNode **head, personNode *newNode, const char *friend )
{
while ( *head && strcmp( ( *head )->name, friend ) != 0 )
{
head = &( *head )->next;
}
int success = *head != NULL;
if ( success )
{
newNode->next = ( *head )->next;
( *head )->next = newNode;
}
return success;
}

最好是在函数中创建新节点,而不是将其作为参数传递,因为如果未找到具有给定字符串的节点,则必须释放它。

所以函数调用可以看起来像

if ( !insertAtEnd( &head, some_new_node, "friend" ) ) free( some_new_node );

如果找不到具有给定字符串的节点,也许您应该将其附加到列表的尾部。在这种情况下,函数可能看起来像

void insertAtEnd( personNode **head, personNode *newNode, const char *friend )
{
while ( *head && strcmp( ( *head )->name, friend ) != 0 )
{
head = &( *head )->next;
}
if ( *head == NULL )
{
*head = newNode;
}
else
{
newNode->next = ( *head )->next;
( *head )->next = newNode;
}
}

相关内容

  • 没有找到相关文章

最新更新