无法删除单链表中的节点

时间:2016-10-20 11:20:58

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

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

当我尝试用命令行参数“mango”删除字符'a'时,它工作正常..即它成功删除了第二个节点。当我尝试用“橙色”做同样的事情时,程序不会删除它...意味着程序无法与第三个和更远的节点一起工作..

程序不能使用任何全局变量,所以我使用了双指针。 所有功能都正常工作这个问题可能是由于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;
}

输出如下: sample output

2 个答案:

答案 0 :(得分:2)

在找到要删除的节点的代码中,您有以下几行:

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

在这里,您递归调用locate,但实际上并未返回该调用的结果,而是始终返回NULL

您需要将其更改为

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

答案 1 :(得分:0)

A double pointer linked list would typically mean that you have an array of linked lists. There were a few problems in your code around the recursion in locate(...). Here is your code, redacted so that you only have a single pointer to the linked list with a "head" element. Cheers!

#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;
 }