迭代链表并添加新值

Iterating over linked list and adding new value

我是链表主题的新手,很难弄清楚如何在每个 'o' 用户输入后添加一个 'u'。非常感谢任何帮助或帮助。

程序输入:

./queensenglish

Input string?:

i love the color yellow

程序的预期输出:

Charlatan! I use the Queen’s English:

i louve the coulour yellouw

    #include <stdio.h>
    #include <cs50.h>
    #include <stdlib.h>
    #include <string.h>

typedef struct node
{
    char myChar;
    struct node* next;
} node;

node* stringToList(char* inputString)
{
    node* first = malloc(sizeof(node));
    first->myChar = 'a';
    first->next = NULL;
    node* current = first;

    char* s;
    for(s = inputString; *s != '[=10=]'; s++)
    {
        node* newNode = malloc(sizeof(node));
        newNode->myChar = *s;
        newNode->next = NULL;
        current->next = newNode;
        current = newNode;
    }
    return first->next;
}

char* listToString(node* firstChar)
{
    // get the length of the string
    int totalLen = 0;
    node* current = firstChar;
    while(current != NULL)
    {
        totalLen++;
        current = current->next;
    }

    char *newString = malloc(sizeof(char)*(totalLen+1));
    char *iter = newString;
    current = firstChar;
    for (int i = 0; i < totalLen; i++)
    {
        *iter = current->myChar;
        current = current->next;
        iter++;
    }
    *iter = '[=10=]';
    return newString;
}

int main(void)
{
    // Read in an input string
    printf("Input string?: \n");
    char* myString = GetString();

    // Convert that string into a linked list
    node* firstChar = stringToList(myString);

    node* current = firstChar;
    while(current != NULL){

        if(strcmp(&current->myChar, "o") == 0 || (strcmp(&current->myChar, "O") == 0)
        {
            //insert a 'u' after every 'o'
            //printf("Char: %c\n", current->myChar);
        }
        current = current->next;
    }

    // convert the list back into a string
    char* newString = listToString(firstChar);

    // print out the "corrected" string
    printf("Charlatan! I use the Queen's English:\n");
    printf("%s\n", newString);
}
if(current->myChar == 'o' || current->myChar == 'O')
{
    node *insertNode = malloc(sizeof(node));
    insertNode->myChar = 'u';
    insertNode->next = current->next;
    current->next = insertNode;
    current = current->next;//skip 'u' node
}

首先,您使用 strcmp 来比较 char 是错误的。 strcmp用于比较两个string不是比较两个字符。您需要使用 == 运算符来检查 current->myChar == 'o' || current->myChar == 'O'

接下来,每次你得到一个charmyChar'o''O'的节点时,你需要:

  1. 创建一个 myChar 等于 u 的新节点。
  2. 使这个新节点指向 'o' 或 'O' 节点指向的节点。
  3. 使 'o' 或 'O' 节点指向这个新节点。

所以你可以这样做:

node* current = firstChar;
while(current != NULL){
         //insert a 'u' after every 'o'
         if(current->myChar == 'o' || current->myChar == 'O') {
                 // create a new node
                 struct node *tmp = malloc(sizeof(struct node));
                 if (tmp == NULL) {
                         /* malloc failed */
                         printf("Error: Malloc fail, cannot add 'u'\n");
                 } else { /* malloc succeded */
                         tmp->myChar = 'u';
                         tmp->next = current->next;
                         current->next = tmp;
                 }
         }
         current = current->next;
}