无法删除单链表中的节点
cannot delete a node in singly linked list
我在删除链表中具有指定字符的节点时遇到问题。该程序接受命令行参数,将它们组合成一个字符串,并将每个字符作为节点添加到链表中。
当我尝试使用命令行参数 "mango" 删除字符 'a' 时,它工作正常.. 即它成功删除了第二个节点。当我尝试用 "orange" 做同样的事情时,程序不会删除它...意味着程序无法使用第三个和更远的节点..
该程序不得使用任何全局变量,因此我使用了双指针。
所有功能都正常工作这个问题可能是由于 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;
}
输出也如下:
在定位要删除的节点的代码中,有这些行:
else if((*s)->node!=NULL){
locate(&((*s)->node));
}
return NULL;
在这里你递归地调用了 locate
,但你实际上并没有 return 那个调用的结果,相反你总是 return NULL
.
您需要将其更改为
else if((*s)->node!=NULL){
return locate(&((*s)->node)); // Return result of recursive call
}
return NULL;
双指针链表通常意味着您有一个链表数组。您的代码中围绕 locate(...) 中的递归存在一些问题。这是您的代码,经过编辑以便您只有一个指向带有 "head" 元素的链表的指针。干杯!
#include <iostream>
#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*);
struct linkedList *createlist();
struct linkedList* createlist()
{
struct linkedList* head = (struct linkedList*) malloc(sizeof(struct linkedList));
head->node = NULL;
return head;
}
int main(int argc, char *argv[]) {
/*some variables*/
char *str;
int indexer = 0;
/*add data from arguments to linked list combine arguments with no spaces
* as a single string
*/
struct linkedList* head = createlist();
str = combineWithNoSpaces(argc, argv);
addTolinkedList(str, head, &indexer);
/*diaplay the added data to linked list with no spaces */
printf("your combined argument is \n");
displayWithNoSpaces(head);
printf("\n");
/* Delete specified character */
printf("Now Deleting the node with specified character : \n");
deleteChar(head);
/* Display the data after deleting */
printf("Displaying after deleting..\n");
displayWithNoSpaces(head);
printf("\n");
return 0;
}
struct linkedList *locate(struct linkedList *head){
if (head->node == NULL) return NULL;
if(head->node->ch == 'a'){
return head;
}
return locate(head->node);
}
void deleteChar(struct linkedList *head){
struct linkedList *temp, *tag;
tag = locate(head);
if(tag!= NULL){
temp = tag->node->node;
free(tag->node);
tag->node = temp;
}
}
void displayWithNoSpaces(struct linkedList *head) {
if (head->node != NULL) {
printf("%c", head->node->ch);
displayWithNoSpaces(head->node);
}
return;
}
void addTolinkedList(char *str, struct linkedList *head, int *indexer) {
if (*indexer == strlen(str)) {
head->node = NULL;
return;
} else {
head->node = (struct linkedList *) malloc(sizeof(struct linkedList));
head->node->ch = *(str + *indexer);
++*indexer;
addTolinkedList(str,head->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;
}
我在删除链表中具有指定字符的节点时遇到问题。该程序接受命令行参数,将它们组合成一个字符串,并将每个字符作为节点添加到链表中。
当我尝试使用命令行参数 "mango" 删除字符 'a' 时,它工作正常.. 即它成功删除了第二个节点。当我尝试用 "orange" 做同样的事情时,程序不会删除它...意味着程序无法使用第三个和更远的节点..
该程序不得使用任何全局变量,因此我使用了双指针。 所有功能都正常工作这个问题可能是由于 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;
}
输出也如下:
在定位要删除的节点的代码中,有这些行:
else if((*s)->node!=NULL){
locate(&((*s)->node));
}
return NULL;
在这里你递归地调用了 locate
,但你实际上并没有 return 那个调用的结果,相反你总是 return NULL
.
您需要将其更改为
else if((*s)->node!=NULL){
return locate(&((*s)->node)); // Return result of recursive call
}
return NULL;
双指针链表通常意味着您有一个链表数组。您的代码中围绕 locate(...) 中的递归存在一些问题。这是您的代码,经过编辑以便您只有一个指向带有 "head" 元素的链表的指针。干杯!
#include <iostream>
#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*);
struct linkedList *createlist();
struct linkedList* createlist()
{
struct linkedList* head = (struct linkedList*) malloc(sizeof(struct linkedList));
head->node = NULL;
return head;
}
int main(int argc, char *argv[]) {
/*some variables*/
char *str;
int indexer = 0;
/*add data from arguments to linked list combine arguments with no spaces
* as a single string
*/
struct linkedList* head = createlist();
str = combineWithNoSpaces(argc, argv);
addTolinkedList(str, head, &indexer);
/*diaplay the added data to linked list with no spaces */
printf("your combined argument is \n");
displayWithNoSpaces(head);
printf("\n");
/* Delete specified character */
printf("Now Deleting the node with specified character : \n");
deleteChar(head);
/* Display the data after deleting */
printf("Displaying after deleting..\n");
displayWithNoSpaces(head);
printf("\n");
return 0;
}
struct linkedList *locate(struct linkedList *head){
if (head->node == NULL) return NULL;
if(head->node->ch == 'a'){
return head;
}
return locate(head->node);
}
void deleteChar(struct linkedList *head){
struct linkedList *temp, *tag;
tag = locate(head);
if(tag!= NULL){
temp = tag->node->node;
free(tag->node);
tag->node = temp;
}
}
void displayWithNoSpaces(struct linkedList *head) {
if (head->node != NULL) {
printf("%c", head->node->ch);
displayWithNoSpaces(head->node);
}
return;
}
void addTolinkedList(char *str, struct linkedList *head, int *indexer) {
if (*indexer == strlen(str)) {
head->node = NULL;
return;
} else {
head->node = (struct linkedList *) malloc(sizeof(struct linkedList));
head->node->ch = *(str + *indexer);
++*indexer;
addTolinkedList(str,head->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;
}