迭代链表并添加新值

时间:2016-07-05 21:51:21

标签: c singly-linked-list cs50

我是链接列表主题的新手,并且很难想出如何添加“' u'在每个' o'之后用户输入。非常感谢任何帮助或帮助。

节目输入:

  

./ queensenglish

     

输入字符串?:

     

我喜欢黄色的颜色

该计划的预期输出:

  

庸医!我使用女王的英语:

     

我倾向于你的吼叫

    #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 != '\0'; 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 = '\0';
    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);
}

2 个答案:

答案 0 :(得分:1)

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
}

答案 1 :(得分:1)

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

接下来,每当您获得charmyChar'o''O'的节点时,您需要:

  1. 创建一个新节点,其myChar等于u
  2. 使此新节点指向“o”或“O”节点指向的节点。
  3. 使“o”或“O”节点指向此新节点。
  4. 所以你可以这样做:

    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;
    }