已排序插入关联列表

时间:2015-07-25 16:16:05

标签: c linked-list sorted

的AoA,

我一直试图在我的循环链表中调试问题12小时。该函数接收一个具有开始和光标字段的ADT。最初的虚拟单元指向自身。插入元素。不允许重复元素。

    int setInsertElementSorted(setADT buffer, setElementT E)
    {
        bool isUnique = true;
        cellT *previous;

        previous = buffer->start;
        buffer->cursor = buffer->start->next;

        while(buffer->cursor != buffer->start){
            if(buffer->cursor->value == E){
                isUnique = false;
            } else if(E < buffer->cursor->value)
                break;
              else {   
                previous = buffer->cursor;
                buffer->cursor = buffer->cursor->next;
            }
        }

        if(isUnique != false){
            cellT *newNode = malloc(sizeof(cellT));
            newNode->value = E;
            previous->next = newNode;
            newNode->next = buffer->cursor;

            buffer->count++;
            return (buffer->count);   
            }
    }

代码接受一系列整数,然后将它们分类到LL参数中。假设用于一组(因此没有重复输入)。

输出:9,8,7,6,5,4,3,2,1

是.. 3,4,5,6,7,8,9(前两个值发生了什么变化?)

输入类似的东西时:7,3,5,1,9,2

out只有7,9(所以它不能处理多于一个的值... o.O)

其他信息:

typedef struct cellT {
    int value;
    struct cellT *next;
} cellT;

struct setCDT{
    int count;
    cellT *start;
    cellT *cursor;   
};

setADT setNew()
{
    setADT newNode = malloc(sizeof(struct setCDT));
    newNode->start = newNode->cursor = malloc(sizeof(cellT));
    newNode->start->next = newNode->cursor->next = newNode->start;
    newNode->count = 0;
    return (newNode);
}

setADT是setCDT的指针类型。但是,setElementT只是一个简单明了的 int 。抱歉模棱两可。

1 个答案:

答案 0 :(得分:4)

一些观察结果:

while(buffer->cursor != buffer->start && buffer->cursor->value < E){
    if(buffer->cursor->value == E) // never true

第一个循环中的value == E永远不会为真,因为循环条件具有value < E,因此遇到等于E的值将停止迭代。如果找到重复项而不是使用<= E,则将循环条件更改为return,只需flag

flag == false也没有返回值的路径(虽然由于上面的错误,目前无法访问),并且如果newNode的错误导致flag分配的内存泄漏{1}}已修复且列表中已存在E

以下if似乎毫无意义,并且由于{之后没有else缩进非常误导:

if(buffer->cursor != buffer->start){
    newNode->next = buffer->cursor; // would be harmless in both branches
    previous->next = newNode;       // done in both branches
} else                              // always using { } would make this clear
    previous->next = newNode;
    buffer->count++;
    return (buffer->count);

另外,不要将setADT作为指针类型进行输入,只是误导并与New(setADT)这样的结构相结合几乎肯定会导致错误。

同时在setNew中,由于只有一个节点,请将newNode->start->next = newNode->cursor->next = newNode->start;替换为newNode->start->next = newNode->start;

变更摘要:

int setInsertElementSorted(struct setCDT * const buffer, const int E) {
    cellT *newNode;
    cellT *previous = buffer->start;
    buffer->cursor = previous->next;

    while (buffer->cursor != buffer->start && buffer->cursor->value <= E) {
        if (buffer->cursor->value == E) {
            return buffer->count; // duplicate value
        }
        previous = buffer->cursor;
        buffer->cursor = buffer->cursor->next;
    }
    if ((newNode = malloc(sizeof(*newNode)))) {
        newNode->value = E;
        newNode->next = buffer->cursor;
        previous->next = newNode;
        buffer->count++;
    }
    return buffer->count;
}

如果错误仍然存​​在,则错误可能在其他地方。

要测试的代码:

int main (int argc, char **argv) {
    struct setCDT *list = setNew();
    for (int i = 1; i < argc; ++i) {
        setInsertElementSorted(list, atoi(argv[i]));
    }
    list->cursor = list->start;
    while ((list->cursor = list->cursor->next) != list->start) {
        (void) printf("%d\n", list->cursor->value);
    }
    return EXIT_SUCCESS;
}