gpt4 book ai didi

c - 如果删除链表的最后一个节点,程序将崩溃

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

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

当使用结束字符不是元音的命令行参数执行程序时,程序可以正常运行。但是当参数以元音结尾时,程序崩溃并显示消息段错误(核心转储)..我不知道如何处理这个问题..

程序不得创建任何全局变量,因此我使用了双指针。程序不得使用除 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(),进一步执行locateVowels()。输出的屏幕截图是:
结尾字符不是元音的参数 argument with no ending vowel结尾字符为元音的参数 argument ending with vowel

最佳答案

removeVowels处,struct linkedList *temporary = s->node;
s 是最后一个元素时,temporary 将为 NULL
所以temporary->ch as NULL->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;
}

关于c - 如果删除链表的最后一个节点,程序将崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40167728/

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