【问题标题】:Stack in linked list using C. but my push method has an error使用 C 在链表中堆叠。但我的推送方法有错误
【发布时间】:2018-06-16 06:22:07
【问题描述】:
#include <stdio.h>
#include <stdlib.h>

typedef struct node{
    int data;
    struct node *next;
}node;

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

void push(node* head, int data){

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

    if(temp == NULL){
        printf("%s", "Out Of Memory");
    }else{
        temp->data = data;
        temp->next = head->next;

在下一行,有 printf 方法打印数字 7。 是用来调试的。但是这个 printf 方法没有用。所以我注意到了

temp->next = head->next; 

此代码有错误。但我找不到原因。。 我在考虑内存分配问题。但还是没明白..!

        printf("%d", 7);
        head->next = temp;
        printf("push (%d)", data);
    }
}

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

【问题讨论】:

  • 拥有init 函数并没有错,但要明白,当将指针传递给函数时,函数会收到指针的副本(带有自己的和非常不同的指针地址)。因此,如果您需要传递和分配指针而不返回和分配新的内存地址,则需要将指针的地址作为参数传递(例如node**,并调用使用init(&amp;head, &amp;tail);),以便您在init 函数中对同一指针进行操作。否则,您将新地址分配并分配给副本,并且在 main() 中永远不会看到更改。
  • printf("%s", "Out Of Memory") -> printf("Out Of Memory") 不需要"%s"
  • temp->next = head;

标签: c data-structures linked-list


【解决方案1】:
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);
}

你应该通过引用传递head和tail来反映调用函数main()中head和tail的变化值。

有关通过参考传递的更多信息,请检查this

【讨论】:

  • 你的答案是正确的,但是注意,malloc的返回不需要强制转换,没有必要。请参阅:Do I cast the result of malloc? 并且...从技术上讲,您不是通过引用传递——C 中没有这样的东西,您传递的是 headtail 指针的 address价值:)
  • @DavidC.Rankin 是的,确实如此。
【解决方案2】:

1) 您不需要也不想要初始化函数。当head 为NULL 时,堆栈被认为是空的。所以head = NULL; 已经足够初始化了。

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

3) 您的push 函数必须允许更新head 一种方法是返回新的头部。另一种是传递一个指向head的指针

4) 链表没有tail-&gt;next = tail;next 为NULL 时到达链表的末尾。所以如果你有一个tail,那就是tail-&gt;next = NULL;

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

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 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;
}

【讨论】:

    【解决方案3】:

    您可以从您的代码中一起删除 init 函数,并且不需要尾部。 如果你想用它来调试,你的 main 可以是这样的。

    node* head = NULL;
    node* tail = NULL;
    head = (node*)malloc(sizeof(node));
    tail = (node*)malloc(sizeof(node));
    
    head->next = tail;
    tail->next = tail;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      相关资源
      最近更新 更多