【发布时间】:2020-09-12 09:48:16
【问题描述】:
我有一个链表和两个给定的单词。我需要在列表中找到第一个单词的索引,然后在第一个单词之后立即添加第二个单词。在 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 line\n2 - Add person to line\n3 - Remove person from line\n4 - VIP guest\n5 - Search in line\n6 - Reverse line\n7 - Exit\n");
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] = '\0';
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] = '\0';
}
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] = '\0';
deleteNode(&newHead, name);
break;
case (4):
getchar();
printf("VIP GUEST!\n");
printf("Enter name: ");
fgets(name, SIZE, stdin);
name[strlen(name) - 1] = '\0';
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] = '\0';
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;
}
}
这是我的代码,我有更多功能,但它们并不相关。 我没有成功理解问题所在以及为什么它没有 以朋友的名字命名
【问题讨论】:
-
在将当前节点指向新节点之前,您错过了
newNode->next = current->next;。 -
它仍然不起作用:(
-
您究竟在哪里需要帮助?你能提供更多细节吗?
-
简短的 sn-p 和“它不起作用”作为描述无法让任何人走得更远。
-
软件继续将第二个单词添加到列表末尾,而不是放在第一个单词之后
标签: c struct linked-list singly-linked-list function-definition