c - 需要帮助 : Unable to delete string from doubly linked list: C

标签 c string duplicates

我正在搞清楚C 中双链表的实现。我的列表按排序顺序添加。 我坚持删除这些值。下面的函数不会从双向链表中删除节点。 我认为我的逻辑是正确的,但我正在努力分配节点值。

a表示添加,d表示删除

struct Node
{
    char name[42];
    struct Node *next;
    struct Node *prev;
};

struct Node* getNewNode(char *name)
{
    struct Node *newNode = malloc(sizeof (struct Node));

    strcpy(newNode->name, name);
    newNode->prev = NULL;
    newNode->next = NULL;

    //printf("GetNewNode Name: %s", newNode->name);

    return newNode;
}

void addSorted(struct Node **head, struct Node *newNode)
{
    struct Node *current;
    // Special case for the head end
    if ((*head == NULL) || strcmp((*head)->name, newNode->name) >= 0)
    {
        newNode->next = *head;
        *head = newNode;
    }
    else
    {
        // Locate the Node before the point of insertion
        current = *head;
        while (current->next != NULL &&
                strcmp(current->next->name, newNode->name) <= 0)
        {
            current = current->next;
        }
        newNode->next = current->next;
        current->next = newNode;
    }
}

void deleteValue(struct Node *head, char *name)
{
    struct Node* current = head;

    /* Find name */
    while (current != NULL)
    {
        if (strcmp(current->name, name) == 0)
        {
            printf("Current At Break: %s \n", current->name);
            break;
        }
        else if (strcmp(current->name, name) < 0)
        {
            printf("While Error: %s is not in the list\n", name);
            return;
        }
        else
        {
            current = current->next;
            printf("Current Is: %s \n", current->name);
        }
    }

    if (current == NULL)
    {
        printf("If Error: %s is not in the list\n", name);
    }
    else
    { /* current != NULL */
        if (current->prev == NULL && current->next == NULL)
        {
            /* Only node in head */
            head = NULL;
            printf("Deleting At 1st\n");
        }
        else if (current->prev == NULL)
        {
            /* First node in head */
            head = current->next;
            head->prev = NULL;
            printf("Deleting At 2nd\n");
        }
        else if (current->next == NULL)
        {
            /* Last node in head */
            head = current->prev;
            head->next = NULL;
            printf("Deleting At 3rd\n");
        }
        else
        {
            /* Node in middle of head */
            current->prev->next = current->next;
            current->next->prev = current->prev;
            printf("Deleting At 4th\n");
        }
    }
    free(current);
}

int main(void)
{
    struct Node *head = NULL, *temp = NULL;
    FILE * fp;
    char * line = NULL, *name = NULL, *operator = NULL;
    size_t len = 0;
    ssize_t read;

    fp = fopen("data.txt", "r");
    if (fp == NULL)
    {
        exit(EXIT_FAILURE);
    }
    while ((read = getline(&line, &len, fp)) != -1)
    {
        name = strtok(line, " ");
        operator = strtok(NULL, "\n");

        //printf("%s %s\n", name, operator);

        if (strncmp(operator, "a", 1) == 0)
        {
            temp = getNewNode(name);
            addSorted(&head, temp);
        }
        else if (strncmp(operator, "d", 1) == 0)
        {
            printf("****Try Deleting: %s\n", name);
            deleteValue(&head, name);
        }
    }

    fclose(fp);
    if (line)
    {
        free(line);
    }
}

文件

Beverly a
Kathy a
Radell a
Gary a
Chuck a
David a
kari a
Tom a
Tanya a
Scott a
Beverly d
Brenda a
Kathy a
Gary a
WenChen a
Chuck a
Mike a
Emanuel a
Linda a
Bernie a
Hassan a
Brian a
Gary d
Kathy d
Gary a
Eunjin a
Kathy a
Brenda a
Jun a
Peanut a
Travis a

最佳答案

改变了一些功能。您可以将头部地址作为参数传递。希望这会有所帮助。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

struct Node
{
    char name[42];
    struct Node *next;
    struct Node *prev;
};

struct Node* getNewNode(char *name)
{
    struct Node *newNode = malloc(sizeof (struct Node));

    strcpy(newNode->name, name);
    newNode->prev = NULL;
    newNode->next = NULL;

    return newNode;
}

char *trimwhitespace(char *str)
{
    char *end;

    // Trim leading space
    while (isspace(*str)) str++;

    if (*str == 0) // All spaces?
        return str;

    // Trim trailing space
    end = str + strlen(str) - 1;
    while (end > str && isspace(*end)) end--;

    // Write new null terminator
    *(end + 1) = 0;

    return str;
}

void deleteValue(struct Node **head, char *name)
{
    struct Node* current = *head;

    /* Find name */
    while (current != NULL)
    {
        if (strcmp(current->name, name) == 0)
        {
            printf("Current At Break: %s \n", current->name);
            break;
        }
        else if (strcmp(current->name, name) > 0)
        {
            printf("While Error: %s is not in the list\n", name);
            return;
        }
        else
        {
            current = current->next;
            printf("Current Is: %s \n", current->name);
        }
    }

    if (current == NULL)
    {
        printf("If Error: %s is not in the list\n", name);
    }
    else
    { /* current != NULL */
        if (current->prev == NULL && current->next == NULL)
        {
            /* Only node in head */
            *head = NULL;
            printf("Deleted At 1st\n");
        }
        else if (current->prev == NULL)
        {
            /* First node in head */
            *head = current->next;
            (*head)->prev = NULL;
            printf("Deleted At 2nd\n");
        }
        else if (current->next == NULL)
        {
            /* Last node in head */
            current->prev->next = NULL;
            printf("Deleted At 3rd\n");
        }
        else
        {
            /* Node in middle of head */
            current->prev->next = current->next;
            current->next->prev = current->prev;
            printf("Deleted At 4th\n");
        }
    }
    free(current);
    current = NULL;
}
void addSorted(struct Node **head, struct Node *newNode)
{
    struct Node *current;
    // Special case for the head end
    if ((*head == NULL) || strcmp((*head)->name, newNode->name) >= 0)
    {
        newNode->next = *head;
        if (*head != NULL) (*head)->prev = newNode;
        *head = newNode;
    }
    else
    {
        // Locate the Node before the point of insertion
        current = *head;
        while (current->next != NULL &&
                strcmp(current->next->name, newNode->name) <= 0)
        {
            current = current->next;
        }
        newNode->next = current->next;
        if (current->next != NULL)current->next->prev = newNode;
        current->next = newNode;
        newNode->prev = current;
    }
}

int main(void)
{
    struct Node *head = NULL, *temp = NULL;
    FILE * fp;
    char * line = NULL, *name = NULL, *operator = NULL;
    size_t len = 0;
    ssize_t read;

    fp = fopen("data.txt", "r");
    if (fp == NULL)
    {
        exit(EXIT_FAILURE);
    }
    while ((read = getline(&line, &len, fp)) != -1)
    {
        name = strtok(line, " ");
        operator = strtok(NULL, "\n");
        name = trimwhitespace(name);

        //printf("%s %s\n", name, operator);

        if (strncmp(operator, "a", 1) == 0)
        {
            temp = getNewNode(name);
            //printf("mainTemp Name: %s\n", temp->name);
            addSorted(&head, temp);
        }
        else if (strncmp(operator, "d", 1) == 0)
        {
            printf("****Try Deleting: %s\n", name);
            deleteValue(&head, name);
        }
    }

    fclose(fp);
    if (line)
    {
        free(line);
    }


    temp = head;
    int i = 0;
    printf("\n\n~~~~~~~~~\n");
    while (temp != NULL)
    {
        printf("%s \n", temp->name);
        temp = temp->next;
        i++;
    }
    printf("%i\n", i);
}

关于c - 需要帮助 : Unable to delete string from doubly linked list: C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31129768/

相关文章:

c - 避免重复/循环取消切换

c++ - 多线程环境中的信号处理函数

c - glibC 和 bioniC 的区别

delphi - 从列表中删除重复项

c - Strtok 段错误

c - 将命令行参数保存为 C 中的全局变量

(s)printf 的 C++ 标准替代品

string - 如何在 Swift 中显示单词之间有延迟的字符串?

node.js - MongoDB 和 NodeJS 如果不存在则插入否则抛出错误

php - 从 PHP 中的逗号分隔字符串中删除重复的子字符串