如果链接的链接的最后一个节点被删除,程序将崩溃

时间:2016-10-21 03:08:45

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

从链接列表中删除元音时遇到问题。程序接受命令行参数,将它们组合在一个字符串中,并将每个字符作为节点添加到链接列表中。

当程序使用命令行参数执行时,其结尾字符不是元音,程序运行正常。但是当参数以元音结束时,程序崩溃并出现消息Segmentation fault(core dumped)..我不知道如何处理这个..

程序不能创建任何全局变量,所以我使用了双指针。程序不能使用任何其他头文件而不是stdio.h string.h stdlib.h

< p> 所有功能都正常工作这个问题可能是由于locateVowels()和removeVowels()函数中的一些错误而发生的,但我无法弄清楚错误是什么。

可以用双指针解决这个问题吗? 我无法弄清楚这个程序有什么问题。我是c编程新手,请帮帮我..请纠正我.. 提前致谢。

完整的代码如下:

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

struct linkedList {
    char ch;
    struct linkedList *node;
};
void printMenu(void);
char* combineWithNoSpaces(int, char *[]);
void addTolinkedList(char *, struct linkedList **, int *);
void printLinkedList(struct linkedList **);
struct linkedList *locateVowel(struct linkedList *s);
int delHead(struct linkedList **);
void removeVowels(struct linkedList *);
int isEmpty(struct linkedList **);
int isVowel(char);
void freeLinkedList(struct linkedList *);

int main(int argc, char *argv[]) {
    int choice, indexer = 0;
    struct linkedList *s;
    char *string;
    if (argc == 1) {
        printf("Parse a sentence");
    } else {
        s = (struct linkedList *) malloc(sizeof(struct linkedList));
        string = combineWithNoSpaces(argc, argv);
        addTolinkedList(string, &s, &indexer);
        while (1) {
            printMenu();
            scanf("%d", &choice);
            if (choice == 1) {
                printLinkedList(&s);
            } else if (choice == 2) {
                if (!delHead(&s))
                    printf("Failed.Empty linked list");
            } else if (choice == 3) {
                removeVowels(s);

            } else if (choice == 4) {
                if (isEmpty(&s)) {
                    printf("Empty LinkedList");
                } else
                    printf("Not Empty");
            } else if (choice == 5) {
                freeLinkedList(s);
                break;
            } else
                printf("Invalic choice");
            printf("\n");
        }
    }
    return 0;
}
int isVowel(char ch) {
    return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'
            || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');
}
void removeVowels(struct linkedList *s) {

    s = locateVowel(s);

    while (s != NULL) {
        struct linkedList *temporary = s->node;

        s->ch = temporary->ch;
        s->node = temporary->node;

        free(temporary);

        s = locateVowel(s);
    }
}
struct linkedList *locateVowel(struct linkedList *s) {

    if (s == NULL) {
        return s;
    }

    char ch = s->ch;

    if (isVowel(ch)) {
        return s;
    }

    if (s->node == NULL) {
        return NULL;
    }

    return locateVowel(s->node);
}

int isEmpty(struct linkedList **s) {
    if (*s == NULL)
        return 1;
    else
        return 0;
}


int delHead(struct linkedList **s) {
    struct linkedList *temp;
    if ((*s) == NULL) {
        return 0;
    } else {
        temp = (*s)->node;
        free(*s);
        *s = temp;
        return 1;
    }
}
void printLinkedList(struct linkedList **s) {
    if ((*s) != NULL) {
        printf("%c", (*s)->ch);
        printLinkedList(&(*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) + 1);
    for (i = 1; i < argc; i++) {
        for (j = 0; j < strlen(argv[i]); j++) {
            *(str + count) = argv[i][j];
            ++count;
        }
    }
    *(str + memory + 1) = '\0';
    return str;
}
void freeLinkedList(struct linkedList *s) {
    while (s != NULL) {

        struct linkedList *temporary = s;

        s = s->node;

        free(temporary);
    }
}
void printMenu(void) {
    printf("\n\n"
            "1. print input arguments (no spaces)\n"
            "2. remove first character\n"
            "3. remove vowels\n"
            "4. is the linked list empty?\n"
            "5. exit program\n"
            "Enter your choice>");
}

程序显示菜单。整数选择3用于删除执行removeVowels()的元音,removeVowels()进一步执行locateVowels()。 输出的屏幕截图是:
结尾字符不是元音的参数 argument with no ending vowel 结尾字符为元音的参数 argument ending with vowel

3 个答案:

答案 0 :(得分:1)

removeVowelsstruct linkedList *temporary = s->node;
temporary是最后一个元素时,NULL将为s 因此temporary->chNULL->ch会出现细分错误。

修复方法之一,在以下代码中。(覆盖而不是删除节点的策略。)

void removeVowels(struct linkedList *s) {
    s = locateVowel(s);

    while (s != NULL) {
        struct linkedList *temporary = s->node;
        if(temporary == NULL){
            s->ch = '\0';
            s->node = NULL;
            break;
        } else {
            s->ch = temporary->ch;
            s->node = temporary->node;
        }

        free(temporary);

        s = locateVowel(s);
    }
}

void printLinkedList(struct linkedList **s) {
    if ((*s) != NULL &&  (*s)->ch != '\0') {
        printf("%c", (*s)->ch);
        printLinkedList(&(*s)->node);
    }
    return;
}

答案 1 :(得分:0)

removeVolwel()功能中,您需要检查temporary不是NULL

将功能更新为

void removeVowels(struct linkedList *s) {

    s = locateVowel(s);

    while (s != NULL) {
        struct linkedList *temporary = s->node;
        /**********************/
        if (temporary == NULL) {
            break
        }
        /******************/
        s->ch = temporary->ch;
        s->node = temporary->node;

        free(temporary);

        s = locateVowel(s);
    }
}

答案 2 :(得分:0)

小心。最后一个链接通常指向NULL,可能不会解除引用始终为NULL check !!

+------+     +------+     +------+
| data |     | data |     | data |
+------+     +------+     +------+
| next |---->| next |---->| next |----> NULL
+------+     +------+     +------+
   ^
   |
 START (Keep track of the whole list.)

希望有助于澄清。