标题:单向链表为什么会有内存错误。 还有凡是用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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-400316-1-1.html




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

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