gpt4 book ai didi

c - 链表中的多次出现删除功能仅删除字符的第一个实例

转载 作者:行者123 更新时间:2023-11-30 16:19:43 25 4
gpt4 key购买 nike

我正在尝试删除链接列表中存储的某个单词的多次出现。但是,这些单词按字符存储在单个节点中,而不是作为列表中的整个单词存储。例如,粉红色的火烈鸟存储为:T->h->e->->p->i->n->k->->f->l->a->m->i->n->g->o。假设用户想要找到粉色。他们必须循环遍历并删除每个节点。

我尝试创建一个循环,将链接列表的内容复制到搜索函数中。我想将这个搜索函数复制到另一个字符串中。我能够成功删除第一次出现的所需字符。但是,我无法做很多其他事情。我曾多次尝试在删除后将节点推送到下一个节点,但这也不起作用。它产生了同样的错误。

void nodeDeletions(struct node** reference_to_headNode, struct node* deleteNode){

if(*reference_to_headNode == NULL){
return;
}
if(*reference_to_headNode == deleteNode){

printf("Test A\n");
*reference_to_headNode = deleteNode->nextNode;
}
if(deleteNode->nextNode != NULL){

printf("Test B\n");
deleteNode->nextNode->previousNode = deleteNode->previousNode;
}
if(deleteNode->previousNode != NULL){

printf("Test C\n");
deleteNode->previousNode->nextNode = deleteNode ->nextNode;
}

free(deleteNode);
}

void deleteWord(struct node** reference_to_headNode, char word_to_delete[]){

struct node *tempNode;
struct node *nextNode;

int searchIndex = 0;
int characterIndex = 0;
const int arraySize = 101;
const int arraySize2 = 202;

char searchWordIndex[arraySize];
char searchWordCopyIndex[arraySize2];

if(*reference_to_headNode == NULL){
return;
}

else {

for (tempNode = *reference_to_headNode; tempNode != NULL; tempNode = tempNode->nextNode) {

searchWordIndex[searchIndex] = tempNode->character;
searchIndex++;

}

strcpy_s(searchWordCopyIndex, searchWordIndex);

int length_of_searchIndex = strlen(searchWordCopyIndex);
int length_of_deletionWord = strlen(word_to_delete);

tempNode = *reference_to_headNode;

for (searchIndex = 0; searchIndex < length_of_searchIndex; searchIndex++) {

printf("Test 1\n");
if(tempNode != NULL) {

if(tempNode->character == word_to_delete[0]) {

for (characterIndex = 0; characterIndex < length_of_deletionWord; characterIndex++) {


printf("Test 2\n");
if (searchWordCopyIndex[searchIndex] == word_to_delete[characterIndex]) {


printf("Test 3\n");
if (tempNode->character == word_to_delete[characterIndex]) {

printf("Test 4\n");
printf("%c\n", tempNode->character);
printf("%c\n%c\n", word_to_delete[characterIndex], searchWordCopyIndex[searchIndex]);

nextNode = tempNode->nextNode;

nodeDeletions(reference_to_headNode, tempNode);

tempNode = nextNode;
}
else {
printf("Test 5\n");

tempNode = tempNode->nextNode;

}

}


}


}

}

tempNode = tempNode->nextNode;

}

}
}

最佳答案

唷!这是很多缩进的clode block 。还有很多辅助数组和索引。还有很长的变量名。 :)

基本上,您正在处理三种不同类型的向前迭代,这些都存在于您的代码中:

  • 遍历字符串:

    while (*s) {
    // do stuff with *s
    s++;
    }
  • 遍历链表:

    while (p) {
    // do stuff with *p
    p = p->next;
    }
  • 通过对源的引用遍历链表,以便可以对其进行修改:

    while (*p) {
    // do stuff with **p
    p = &(*p)->next;;
    }

您只需组合这三个基本循环即可。

您可以使用第三种方法浏览列表(因为您需要在删除时能够更新标题或下一个链接)。对于您访问的每个节点,同时使用其他两种方法将该节点的“尾部”与辅助指针 p 和字符串 s 进行比较。当字符串匹配时,*s == '\0'p 指向单词后的第一个节点。通过前进头直到头为 p 来删除所有节点。

换句话说:

  • 通过*head遍历列表。
  • 在每个节点:
    • p = *heads 设置为字符串的开头;
    • 在字母匹配时遍历列表和单词;
    • 如果*s == '\0',则存在匹配。现在,*head指向列表中要删除的单词的开头,p指向列表中该单词之后的第一个节点,可能是 NULL
    • 如果存在匹配,则前进 *head 直到 *head == p,同时删除节点。

或者,在代码中:

void delete_word(struct node **head, const char *str)
{
while (*head) {
struct node *p = *head;
const char *s = str;

while (p && *s && p->c == *s) {
p = p->next;
s++;
}

if (*s == '\0') {
while (*head != p) {
struct node *del = *head;
*head = (*head)->next;

delete_node(del);
}
} else {
head = &(*head)->next;
}
}
}

关于c - 链表中的多次出现删除功能仅删除字符的第一个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55541206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com