开发者社区> 问答> 正文

链表堆空间分配问题

#include <stdio.h>
#include <malloc.h>
typedef struct Node {
        int value;        //4
        struct Node* next;        //4
}Node;

Node *create();
void add();
void del();
void search();

Node *create(int v) {
    Node *first;
    first = (Node *)(calloc(1,sizeof(*first)));
    first->value = v;
    first->next = NULL;
    return first;
}
void add(Node **head,int v) {
    Node *p;
    p = (Node *)(calloc(1,sizeof(*p)));
    p->value = v;
    p->next = *head;
    *head = p;
    
}
void search(Node *head) {
    Node *p;
    p=head;
    while(p != NULL) {
        printf("address is %d;value address is %d;next address is %d;next content is %d\n",p,&(p->value),&(p->next),p->next);
        p = p->next;
    }
}



int main() {
    Node *head;
    head = create(0);
    add(&head,1);
    add(&head,2);
    add(&head,3);
    search(head);
    

}

展开
收起
a123456678 2016-06-08 21:41:09 1837 0
1 条回答
写回答
取消 提交回答
  • 动态分配的内存不一定是连续的。要连续的自己申请数组,这么小的内存用数组更好。
    Node all[NR_NODES];
    动态分配浪费内存,又容易出错。

    2019-07-17 19:32:48
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载