如果链表的最后一个节点被删除,程序会崩溃
The program crashes if linked last node of linked list is deleted
我在从链接列表中删除元音时遇到问题。该程序接受命令行参数,将它们组合成一个字符串并将每个字符作为节点添加到链表中。
当使用结尾字符不是元音的命令行参数执行程序时,程序运行良好。但是当参数以元音结尾时,程序崩溃并显示消息 Segmentation fault(core dumped)..我不知道如何处理这个..
程序不能创建任何全局变量所以我使用了双 pointer.The 程序不能使用除 stdio.h string.h stdlib.h
所有功能都正常工作这个问题可能是由于 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) = '[=10=]';
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()。
输出的屏幕截图是:
结尾字符不是元音的参数
结尾字符是元音的参数
在您的 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);
}
}
在 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 = '[=10=]';
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 != '[=10=]') {
printf("%c", (*s)->ch);
printLinkedList(&(*s)->node);
}
return;
}
小心。最后一个 link 通常指向 NULL,它可能不会被取消引用总是 NULL 检查!!
+------+ +------+ +------+
| data | | data | | data |
+------+ +------+ +------+
| next |---->| next |---->| next |----> NULL
+------+ +------+ +------+
^
|
START (Keep track of the whole list.)
希望这有助于澄清。
我在从链接列表中删除元音时遇到问题。该程序接受命令行参数,将它们组合成一个字符串并将每个字符作为节点添加到链表中。
当使用结尾字符不是元音的命令行参数执行程序时,程序运行良好。但是当参数以元音结尾时,程序崩溃并显示消息 Segmentation fault(core dumped)..我不知道如何处理这个..
程序不能创建任何全局变量所以我使用了双 pointer.The 程序不能使用除 stdio.h string.h stdlib.h
所有功能都正常工作这个问题可能是由于 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) = '[=10=]';
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()。
输出的屏幕截图是:
结尾字符不是元音的参数
在您的 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);
}
}
在 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 = '[=10=]';
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 != '[=10=]') {
printf("%c", (*s)->ch);
printLinkedList(&(*s)->node);
}
return;
}
小心。最后一个 link 通常指向 NULL,它可能不会被取消引用总是 NULL 检查!!
+------+ +------+ +------+
| data | | data | | data |
+------+ +------+ +------+
| next |---->| next |---->| next |----> NULL
+------+ +------+ +------+
^
|
START (Keep track of the whole list.)
希望这有助于澄清。