Я новичок в теме связанного списка и с трудом определяю, как добавить «u» после каждого «o», который вводит пользователь. Любая помощь или помощь приветствуются.Итерация по связанному списку и добавление нового значения
программыполя вход:
./queensenglish
Входная строка ?:
я люблю желтый цвет
В программы ожидается выход:
Шарлатан! Я использую королевы английского языка:
я Louve в 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 != '\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(¤t->myChar, "o") == 0 || (strcmp(¤t->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);
}
'зЬгстр (& current-> MyChar, "о") == 0 'это UB ->' current-> MyChar == «о '' – BLUEPIXY
insertNode: {' u ', NULL}, nextNode: current-> next, insertNode-> next = nextNode; current-> next = insertNode; – BLUEPIXY
'node * first = malloc (sizeof (node)); \t first-> myChar = 'a'; first-> next = NULL; ... return first-> next; 'memory leak ->' first; \t node * current = &first; ... return first.next; ' – BLUEPIXY