使用C在链表中堆栈。但我的push方法有错误

使用C在链表中堆栈。但我的push方法有错误,c,data-structures,linked-list,C,Data Structures,Linked List,此代码有一个错误。但是我找不到原因。。 我在考虑内存分配问题。但还是没有得到它 temp->next = head->next; 您应该将head和tail作为passby引用传递,以反映调用函数main()时head和tail的更改值 有关通过参考检查的更多信息 您应该将head和tail作为passby引用传递,以反映调用函数main()时head和tail的更改值 有关通过引用检查的详细信息,请参见,您不需要也不需要初始化功能。当head为空时,堆栈被视为空。Sohead=

此代码有一个错误。但是我找不到原因。。 我在考虑内存分配问题。但还是没有得到它

temp->next = head->next; 
您应该将head和tail作为passby引用传递,以反映调用函数main()时head和tail的更改值

有关通过参考检查的更多信息

您应该将head和tail作为passby引用传递,以反映调用函数main()时head和tail的更改值

有关通过引用检查的详细信息,请参见,您不需要也不需要初始化功能。当
head
为空时,堆栈被视为空。So
head=NULL已足够初始化

2) 堆栈不需要
尾部
指针,因为元素总是在前端插入/删除

3) 您的
push
功能必须允许更新
head
一种方法是返回新的head。另一种方法是将指针传递到
head

4) 链表没有
tail->next=tailnext
为空时,将到达列表的末尾。因此,如果你有一个
tail
,它将是
tail->next=NULL

使用“返回新头部”的堆栈更像:

void init(node** head, node** tail){
    *head = malloc(sizeof(node));
    *tail = malloc(sizeof(node));
    (*head)->next = *tail;
    (*tail)->next = *tail;
}

void main(){
    node* head = NULL;
    node* tail = NULL;
    init(&head, &tail);
    push(head, 10);
}
node* push(node* head, int data){

    node *temp = malloc(sizeof(node));

    if(temp == NULL){
        printf("%s", "Out Of Memory");
        return head;
    }

    temp->data = data;
    temp->next = head;   // notice this
    return temp;
}

int main(){
    node* head = NULL;
    head = push(head, 10);
    return 0;
}
使用“将指针传递到头部”的堆栈更像:

void init(node** head, node** tail){
    *head = malloc(sizeof(node));
    *tail = malloc(sizeof(node));
    (*head)->next = *tail;
    (*tail)->next = *tail;
}

void main(){
    node* head = NULL;
    node* tail = NULL;
    init(&head, &tail);
    push(head, 10);
}
node* push(node* head, int data){

    node *temp = malloc(sizeof(node));

    if(temp == NULL){
        printf("%s", "Out Of Memory");
        return head;
    }

    temp->data = data;
    temp->next = head;   // notice this
    return temp;
}

int main(){
    node* head = NULL;
    head = push(head, 10);
    return 0;
}
1) 您不需要也不想要初始化函数。当
head
为空时,堆栈被视为空。So
head=NULL已足够初始化

2) 堆栈不需要
尾部
指针,因为元素总是在前端插入/删除

3) 您的
push
功能必须允许更新
head
一种方法是返回新的head。另一种方法是将指针传递到
head

4) 链表没有
tail->next=tailnext
为空时,将到达列表的末尾。因此,如果你有一个
tail
,它将是
tail->next=NULL

使用“返回新头部”的堆栈更像:

void init(node** head, node** tail){
    *head = malloc(sizeof(node));
    *tail = malloc(sizeof(node));
    (*head)->next = *tail;
    (*tail)->next = *tail;
}

void main(){
    node* head = NULL;
    node* tail = NULL;
    init(&head, &tail);
    push(head, 10);
}
node* push(node* head, int data){

    node *temp = malloc(sizeof(node));

    if(temp == NULL){
        printf("%s", "Out Of Memory");
        return head;
    }

    temp->data = data;
    temp->next = head;   // notice this
    return temp;
}

int main(){
    node* head = NULL;
    head = push(head, 10);
    return 0;
}
使用“将指针传递到头部”的堆栈更像:

void init(node** head, node** tail){
    *head = malloc(sizeof(node));
    *tail = malloc(sizeof(node));
    (*head)->next = *tail;
    (*tail)->next = *tail;
}

void main(){
    node* head = NULL;
    node* tail = NULL;
    init(&head, &tail);
    push(head, 10);
}
node* push(node* head, int data){

    node *temp = malloc(sizeof(node));

    if(temp == NULL){
        printf("%s", "Out Of Memory");
        return head;
    }

    temp->data = data;
    temp->next = head;   // notice this
    return temp;
}

int main(){
    node* head = NULL;
    head = push(head, 10);
    return 0;
}

您可以从代码中删除init函数,而不需要尾部。 如果您想使用它进行调试,您的main可以如下所示

void push(node** headPtr, int data){

    node *temp = malloc(sizeof(node));

    if(temp == NULL){
        printf("%s", "Out Of Memory");
        return;
    }

    temp->data = data;
    temp->next = *headPtr;   // notice the *
    *headPtr = temp;
}

int main(){
    node* head = NULL;
    push(&head, 10); // notice the &
    return 0;
}

您可以从代码中删除init函数,而不需要尾部。 如果您想使用它进行调试,您的main可以如下所示

void push(node** headPtr, int data){

    node *temp = malloc(sizeof(node));

    if(temp == NULL){
        printf("%s", "Out Of Memory");
        return;
    }

    temp->data = data;
    temp->next = *headPtr;   // notice the *
    *headPtr = temp;
}

int main(){
    node* head = NULL;
    push(&head, 10); // notice the &
    return 0;
}

您的答案是正确的,但是请注意,没有必要强制执行malloc的返回,这是不必要的。看:还有。。。从技术上讲,你不是通过引用传递的——在C语言中没有这样的东西,你是通过值传递
指针的地址
:)
@DavidC.Rankin是的,的确如此。你的答案是正确的,但是请注意,没有必要抛出
malloc
,这是不必要的。看:还有。。。从技术上讲,你不是通过引用传递的——在C中没有这样的事情,你是通过值传递
指针的地址
:)
@DavidC.Rankin是的,的确如此。拥有
init
函数没有错,但是在传递指向函数的指针时要理解,该函数接收指针的一个副本(具有它自己的、非常不同的指针地址)。因此,如果需要在不返回和分配新内存地址的情况下传递和分配指针,则需要将指针的地址作为参数传递(例如,
节点**
,并使用
init(&head,&tail);
)调用,以便在
init
函数中对同一指针进行操作。否则,您会将新地址分配给一个副本,并且在
main()
printf(“%s”,“内存不足”)
->
printf(“内存不足”)
无需
“%s”
temp->next=head;拥有
init
函数没有什么错,但是要理解,当向函数传递指针时,函数会收到指针的副本(具有它自己的、非常不同的指针地址)。因此,如果需要在不返回和分配新内存地址的情况下传递和分配指针,则需要将指针的地址作为参数传递(例如,
节点**
,并使用
init(&head,&tail);
)调用,以便在
init
函数中对同一指针进行操作。否则,您会将新地址分配给一个副本,并且在
main()
printf(“%s”,“内存不足”)
->
printf(“内存不足”)
无需
“%s”
temp->next=head;