【问题标题】:Removing a value of a linked struct删除链接结构的值
【发布时间】:2016-04-30 10:55:39
【问题描述】:

函数必须在删除链表的请求值后返回链表的地址,否则返回NULL,函数 search(int input, CellPtr list) 效果很好... 程序的实际输出是正确的,直到它到达第二个函数实际上它什么都不打印

#include<stdio.h>
#include<stdlib.h>
typedef struct cell *CellPtr;
typedef struct cell {
    int contents;
    CellPtr next;
}   Cell;
int search(int input, CellPtr list);
CellPtr delete_cell(int input, CellPtr list);
int main()
{
    CellPtr list;
    list = malloc(sizeof(Cell));
    list->contents = -2;                                /*ONLY FOR TESTING*/
    list->next = malloc(sizeof(Cell));
    list->next->contents = 4;
    list->next->next = malloc(sizeof(Cell));
    list->next->next->contents = -6;
    list->next->next->next = malloc(sizeof(Cell));
    list->next->next->next->contents = 100;
    printf("search(100, list) = %d\n", search(-6, list));
    if (list = delete_cell(-5, list) == NULL)
        printf("not found");
    return 0;
}

CellPtr delete_cell(int input, CellPtr list)
{
    int i, j;
    CellPtr p, q, tmp;
    if (i = search(input, list) == 0)   {
        return NULL;
    }
    p = list;
    if (i == 1) {
        list = p->next;
        free(p);
        return list;
    }
    for (j=1; j<i-1; j++)   {
        p = p->next;                            /*to get the address of the cell pointing to wanted cell*/
    }
    if (p->next->next == NULL)  {               /*wanted cell is the last one in the list*/
        free(p->next);
        p->next = NULL;
        return list;
    }
    q = p->next;
    while (q->next->next != NULL)   {           /*q is the address of the cell one before the last cell*/
        q = q->next;
    }
    if ((input * list->contents > 0) && (input * q->next->contents < 0))  {
        tmp = list;
        list = tmp->next;
        tmp->next = p->next->next;
        free(p->next);
        p->next = tmp;
        return list;
    }
    if ((input * list->contents <0) && (input * q->next->contents > 0)) {
        q->next->next = p->next->next;
        free(p->next);
        p->next = q->next;
        q->next = NULL;
        return list;
    }
    if ((input * list->contents >0) && (input * q->next->contents > 0)) {
        q->next->next = p->next->next;
        free(p->next);
        p->next = q->next;
        q->next = NULL;
        return list;
    }
    if ((input * list->contents <0) && (input * q->next->contents < 0)) {
        return NULL;
    }
}

【问题讨论】:

  • 代码很难理解。它是否查找值“输入”并删除 Cell 节点?
  • 一个可能的问题是list = delete_cell(-5, list) == NULL,是(list=delete_cell(-5,list) == NULL 还是list = (delete_cell(-5,list)==NULL)?没有括号,表示后面的。

标签: c list struct linked-list


【解决方案1】:

这里,当你初始化最后一个元素时:

list->next->next->next = malloc(sizeof(Cell));

您错过了将其next-指针初始化为NULL。您必须添加:

list->next->next->next->next = NULL;

否则,搜索不存在元素的函数(如delete_cell(-5, list) 中的函数将取消引用未初始化的指针,并且程序可能会崩溃(未定义的行为)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2020-09-05
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多