| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2966 人关注过本帖
标题:单向链表为什么会有内存错误。 还有凡是用malloc开辟的空间 一定要用free函 ...
取消只看楼主 加入收藏
waja
Rank: 2
等 级:论坛游民
帖 子:53
专家分:52
注 册:2012-7-27
结帖率:81.82%
收藏
已结贴  问题点数:10 回复次数:0 
单向链表为什么会有内存错误。 还有凡是用malloc开辟的空间 一定要用free函数释放吗?
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
struct LNode
{
    int data;
    struct LNode *next;
};


struct LNode *create(int n)
{
    int i;
    struct LNode *head, *p1, *p2;
    int a;
    head = NULL;
    printf("Input the integers:\n");
    for(i = n; i > 0; i--)
    {
        p1 = (struct LNode*)malloc(sizeof(struct LNode));
        scanf("%d", &a);
        p1->data = a;
        if (head = NULL)
        {
            head = p1;
            p2 = p1;
        }
        else
        {
            p2->next = p1;
            p2  = p1;
        }

    }
    p2->next = NULL;
    return head;
}

void main()
{
    int n;
    struct LNode *q;
    printf("Inout the count of the nodes you want to create:\n");
    scanf("%d", &n);
    q = create(n);
    printf("the result is :\n");
    while(q)
    {
        printf("%d    ", q->data);
        q = q->next;
    }
    getch();
}

[ 本帖最后由 waja 于 2013-3-10 09:19 编辑 ]
搜索更多相关主题的帖子: free include create 
2013-03-10 09:18
快速回复:单向链表为什么会有内存错误。 还有凡是用malloc开辟的空间 一定要用 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024365 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved