关于C#:无法删除单链列表中的节点

cannot delete a node in singly linked list

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

当我尝试使用命令行参数" mango"删除字符" a"时,它可以正常工作。即,它成功删除了第二个节点。当我尝试对" orange"执行相同的操作时,程序不会将其删除...表示程序无法与第三个节点和更远的节点一起使用。.

该程序一定不能使用任何全局变量,所以我已经使用了双指针。
所有功能均正常运行,可能由于locate()和deleteChar()函数中的某些错误而出现了此问题,但我无法弄清错误是什么。

可以使用双指针解决此问题吗?
我无法弄清楚该程序有什么问题。我是c编程的新手,请帮助我。
提前谢谢..

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#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 \
"
);
    displayWithNoSpaces(&s);
    printf("\
"
);
    /* Delete specified character */
    printf("Now Deleting the node with specified character : \
"
);
    deleteChar(&s);
    /* Display the data after deleting */
    printf("Displaying after deleting..\
"
);
    displayWithNoSpaces(&s);
    printf("\
"
);
    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


在用于查找要删除的节点的代码中,具有以下几行:

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

在这里您递归调用locate,但实际上并没有返回该调用的结果,而是总会返回NULL

您需要将其更改为

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


双指针链接列表通常意味着您具有一组链接列表。在代码中,围绕locate(...)的递归存在一些问题。这是已编辑的代码,因此只有一个指向带有" head"元素的链表的指针。干杯!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#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 \
"
);
displayWithNoSpaces(head);
printf("\
"
);
/* Delete specified character */
printf("Now Deleting the node with specified character : \
"
);
deleteChar(head);
/* Display the data after deleting */
printf("Displaying after deleting..\
"
);
displayWithNoSpaces(head);
printf("\
"
);
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;
 }