无法删除单链表中的节点

标签 c gcc data-structures linked-list singly-linked-list

我在删除链表中具有指定字符的节点时遇到问题。该程序接受命令行参数,将它们组合成一个字符串,并将每个字符作为节点添加到链表中。

当我尝试使用命令行参数“mango”删除字符“a”时,它工作正常......即它成功删除了第二个节点。当我尝试对“橙色”做同样的事情时,程序不会删除它...意味着该程序无法与第三个和更远的节点一起工作..

该程序不得使用任何全局变量,因此我使用了双指针。 所有功能都正常工作这个问题可能是由于 locate() 和 deleteChar() 函数中的一些错误而发生的,但我无法弄清楚错误是什么。

这个问题可以用双指针解决吗?? 我不知道这个程序有什么问题..我是 c 编程的新手,请帮我解决这个问题..请纠正我.. 提前致谢..

代码如下:

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

struct linkedList {
    char ch;
    struct linkedList *node;
};

char* combineWithNoSpaces(int, char *[]);
void addTolinkedList(char *, struct linkedList **, int *);
void displayWithNoSpaces(struct linkedList **);
struct linkedList *locate(struct linkedList**);
void deleteChar(struct linkedList**);


int main(int argc, char *argv[]) {
    /*some variables*/
    char *str;
    struct linkedList *s;
    int indexer = 0;
    /*add data from arguments to linked list combine arguments with no spaces
     * as a single string
     */
    s = (struct linkedList *) malloc(sizeof(struct linkedList));
    str = combineWithNoSpaces(argc, argv);
    addTolinkedList(str, &s, &indexer);
    /*diaplay the added data to linked list with no spaces */
    printf("your combined argument is \n");
    displayWithNoSpaces(&s);
    printf("\n");
    /* Delete specified character */
    printf("Now Deleting the node with specified character : \n");
    deleteChar(&s);
    /* Display the data after deleting */
    printf("Displaying after deleting..\n");
    displayWithNoSpaces(&s);
    printf("\n");
    return 0;
}
int i = 0;

struct linkedList *locate(struct linkedList **s){
    if((*s)->node->ch == 'a'){
        return *s;
    }
    else if((*s)->node!=NULL){
        locate(&((*s)->node));
    }
    return NULL;
}
void deleteChar(struct linkedList **s){
    struct linkedList *temp, *tag;
    tag = locate(s);
    if(tag!= NULL){
        temp = tag->node->node;
        free(tag->node);
        tag->node = temp;
    }
}
void displayWithNoSpaces(struct linkedList **s) {
    if ((*s) != NULL) {
        printf("%c", (*s)->ch);
        displayWithNoSpaces(&(*s)->node);
    }
    return;
}
void addTolinkedList(char *str, struct linkedList **s, int *indexer) {
    if (*indexer == strlen(str)) {
        *s = NULL;
        return;
    } else {
        (*s)->ch = *(str + *indexer);
        (*s)->node = (struct linkedList *) malloc(sizeof(struct linkedList));
        ++*indexer;
        addTolinkedList(str, &(*s)->node, indexer);
    }
}
char * combineWithNoSpaces(int argc, char *argv[]) {
    int i, j;
    int count = 0;
    int memory = 0;
    char *str;
    for (i = 1; i < argc; i++) {
        for (j = 0; j < strlen(argv[i]); j++) {
            ++memory;
        }
    }
    str = (char *) malloc(memory * sizeof(char));
    for (i = 1; i < argc; i++) {
        for (j = 0; j < strlen(argv[i]); j++) {
            *(str + count) = argv[i][j];
            ++count;
        }
    }
    return str;
}

输出也如下: sample output

最佳答案

在定位要删除的节点的代码中,有以下几行:

else if((*s)->node!=NULL){
    locate(&((*s)->node));
}
return NULL;

在这里你递归地调用了locate,但你实际上并没有返回那个调用的结果,而是你总是返回NULL

你需要把它改成

else if((*s)->node!=NULL){
    return locate(&((*s)->node));  // Return result of recursive call
}
return NULL;

关于无法删除单链表中的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40153005/

相关文章:

c - C 编程中的 Winzip 命令行

c++ - g++ 不会在 Snow Leopard 下编译 std=c++0x

c - 出现段错误。不知道为什么

java - 来自不同类节点的链表

c++ - 红黑树插入代码显示段错误 11

c++ - C 中用 1 替换 0 的宏

c - 在结构中动态分配一个 int 数组

c - 段错误(核心已转储)。堆排序

c - 为什么 typeof 函数在 C 中不起作用

c++ - gcc-4.9 无法编译#include "user defined file"