Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C链表,丢失元素_C_Linked List - Fatal编程技术网

C链表,丢失元素

C链表,丢失元素,c,linked-list,C,Linked List,我正在尝试编写代码来添加链表元素的编号。但是加上之后,我失去了一些元素。我找不到错误。这是我的密码: void push(struct node** head_ref, int new_data){ struct node* new_node = (struct node*)malloc(sizeof(struct node*)); new_node->data = new_data; new_node->next = (*head_ref); (*

我正在尝试编写代码来添加链表元素的编号。但是加上之后,我失去了一些元素。我找不到错误。这是我的密码:

void push(struct node** head_ref, int new_data){
    struct node* new_node = (struct node*)malloc(sizeof(struct node*));
    new_node->data  = new_data;
    new_node->next = (*head_ref);
    (*head_ref)    = new_node;
}
void reverse(struct node** head_ref){
    struct node* prev   = NULL;
    struct node* current = *head_ref;
    struct node* next;
    while (current != NULL){
        next  = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    *head_ref = prev;
}
struct node *shiftter(struct node *a, int index){
    struct node *temp = a;
    while(index > 0){
        append(&temp, 0);
        --index;
    }
    return temp;
}
struct node *add(struct node *fist, struct node *second){
    struct node *gudu = second;
    struct node *c = fist;
    struct node *hudu = NULL;
    reverse(&gudu);
    reverse(&c);
    while(c != NULL){
        push(&hudu, c->data + gudu->data);
        c = c->next;
        gudu = gudu->next;
    }
    while(gudu != NULL){
        push(&hudu, gudu->data);
        gudu = gudu->next;
    }
    return hudu;
}
int main(int argc, const char * argv[]) {
    struct node *a = NULL;
    struct node *b = NULL;
    push(&a , 1);
    push(&a , 2);
    push(&a , 3);
    push(&a , 4);
    push(&b , 5);
    push(&b , 1);
    push(&b , 2);
    push(&b , 4);
    printList(a);
    printf("\n");
    printList(b);
    printf("\n");
    b = shiftter(b,1);
    printList(b);
    printf("\n");
    printList(add(a, b));
    printf("\n");
    printList(a);
    printf("\n");
    printList(b);
    return 0;
}
我的输出是:

4 3 2 1 

4 2 1 5 

4 2 1 5 0  

4 6 4 7 1 

4

4 

我的程序以退出代码结束:0

首先,名为
reverse
的函数不会执行任何与列表中的
reverse
相关的远程操作。它将最后一个元素(
prev
)放在列表的最前面,但这是最接近的。。。逻辑的其余部分充其量是模糊的。它不应该修改最后一个元素的
->next
成员,以指向倒数第二个元素吗?这就是我认为你们缺失元素的来源所在


另外,我们还没有完整的计划来最终回答这个问题。一旦你用一个完整的/可编译的测试用例更新了问题,请点击我。

即使
reverse
中的算法是正确的,函数
add
中的问题很简单:你反向列表,并并行迭代生成的列表。但是您不保留新的头,因此除了最后一个节点之外,所有节点都不再被任何对象引用

您应该保留新的磁头,并在完成计算后将其反转回去以恢复原始列表


更好的方法是:将列表按从低到高的顺序排列。

函数
shifter()
应该做什么?它也适合,还有大约114个字符。@chux你能把我链接到一个元,告诉我为什么这应该是一个注释,而不是一个答案吗?因为到目前为止,这表明这是一个候选答案。这似乎是我答案的一个很好的延续。请随意拿我写的东西,做你需要做的事情,形成更完整的东西。不管引用与否,我都不在乎。。。作为一个社区,为了更大的利益,我们提出了一个统一的答案,帮助OP学习。还有一件事:如果剩余的代码片段碰巧出现,使这个问题变得完整,请随时打电话给我,以便我可以帮助分析。