【问题标题】:How to add an organ to a specific location in a linked list in C如何将器官添加到C中链表中的特定位置
【发布时间】: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-&gt;next = current-&gt;next;
  • 它仍然不起作用:(
  • 您究竟在哪里需要帮助?你能提供更多细节吗?
  • 简短的 sn-p 和“它不起作用”作为描述无法让任何人走得更远。
  • 软件继续将第二个单词添加到列表末尾,而不是放在第一个单词之后

标签: c struct linked-list singly-linked-list function-definition


【解决方案1】:
    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添加到单词匹配的节点旁边。

【讨论】:

  • 我完全按照你写的做,但它仍然没有在第一个单词之后添加第二个单词,而是将它添加到列表的末尾
  • @אבילוי 你能否再添加一些代码,以便我弄清楚你到底在哪里得到了错误?
【解决方案2】:

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

如果是这样,那么函数名称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;
    }
}

【讨论】:

  • 谢谢你的工作kk我爱你你是我的英雄谢谢!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 1970-01-01
  • 2011-12-15
  • 1970-01-01
  • 2012-04-01
  • 2014-07-14
  • 2017-07-07
相关资源
最近更新 更多