有关单链表free失败的问题,在不加free程序是可以正常运行的

hg460713171 2017-03-19 08:19:48
下面是代码和出现的错误
# include<stdio.h>
# include<stdlib.h>
typedef struct Node
{
int data;
Node * pNext;
} Node, *list;
void Creat_list(list*l, int n);
void shuchu(list l);
void merge(list la, list *lb, list *lc);
void main()
{
list la;
list lb;
list lc;
Creat_list(&la, 3);
Creat_list(&lb, 3);
merge(la, &lb, &lc);
shuchu(lc);
}
void Creat_list(list *l, int n)//创建一个链表 头插法逆序输入
{
int i;
list q;
int a;
*l = (list)malloc(sizeof(Node));
(*l)->pNext = NULL;
for (i = n; i > 0; i--)
{
q = (list)malloc(sizeof(Node));
scanf_s("%d", &a);
q->pNext = (*l)->pNext;
(*l)->pNext = q;
q->data = a;
}
}
void shuchu(list l)//输出链表里的值
{
list p = l->pNext;
while (p)
{
printf("%d", p->data);
p = p->pNext;
}
}
void merge(list la, list *lb, list *lc) //讲两个递增链表合并为一个非递减链表
{
list pa;
list pb;
list pc;
pa = la->pNext;
pb = (*lb)->pNext;
*lc = pc = la;
while (pa&&pb)
{
if (pa->data <= pb->data)
{
pc->pNext = pa;
pc = pa;
pa = pa->pNext;
}
else
{
pc->pNext = pb;
pc = pb;
pb = pb->pNext;
}
pc->pNext = pa ? pa : pb;
//*lb= NULL;
free(*lb);
}
}
输入为 3 2 1 和 3 2 1
...全文
205 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Pingo520 2017-03-20
  • 打赏
  • 举报
回复
你的malloc和free都不对应。你的malloc分配的单个的node节点,别指望通过free(list)来释放所有的节点。且你的那个list并没有通过malloc分配
赵4老师 2017-03-20
  • 打赏
  • 举报
回复
free Deallocates or frees a memory block. void free( void *memblock ); Function Required Header Compatibility free <stdlib.h> and <malloc.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value None Parameter memblock Previously allocated memory block to be freed Remarks The free function deallocates a memory block (memblock) that was previously allocated by a call to calloc, malloc, or realloc. The number of freed bytes is equivalent to the number of bytes requested when the block was allocated (or reallocated, in the case of realloc). If memblock is NULL, the pointer is ignored and free immediately returns. Attempting to free an invalid pointer (a pointer to a memory block that was not allocated by calloc, malloc, or realloc) may affect subsequent allocation requests and cause errors. After a memory block has been freed, _heapmin minimizes the amount of free memory on the heap by coalescing the unused regions and releasing them back to the operating system. Freed memory that is not released to the operating system is restored to the free pool and is available for allocation again. When the application is linked with a debug version of the C run-time libraries, free resolves to _free_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support. Example /* MALLOC.C: This program allocates memory with * malloc, then frees the memory with free. */ #include <stdlib.h> /* For _MAX_PATH definition */ #include <stdio.h> #include <malloc.h> void main( void ) { char *string; /* Allocate space for a path name */ string = malloc( _MAX_PATH ); if( string == NULL ) printf( "Insufficient memory available\n" ); else { printf( "Memory space allocated for path name\n" ); free( string ); printf( "Memory freed\n" ); } } Output Memory space allocated for path name Memory freed Memory Allocation Routines See Also _alloca, calloc, malloc, realloc, _free_dbg, _heapmin
自信男孩 2017-03-20
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    int data;
    struct Node *pNext;
} Node, *list;

void Creat_list(list*l, int n);
void shuchu(list l);
void merge(list la, list lb, list *lc);

int  main(void)
{
    list la;
    list lb;
    list lc;

    Creat_list(&la, 3);
    shuchu(la);
    Creat_list(&lb, 3);
    shuchu(lb);

    merge(la, lb, &lc);

    shuchu(lc);

    return 0;
}
void Creat_list(list *l, int n)
{
    int a;
    int i;
    list q;

    *l = (list)malloc(sizeof(Node));
    (*l)->pNext = NULL;
    for (i = n; i > 0; i--)
    {
        q = (list)malloc(sizeof(Node));
        scanf("%d", &a);
        q->pNext = (*l)->pNext;
        (*l)->pNext = q;
        q->data = a;
    }
}

void shuchu(list l)
{
    list p = l->pNext;

    while (p) {
        printf("%d\t", p->data);
        p = p->pNext;
    }
    putchar(10);
}

void merge(list la, list lb, list *lc)
{
    list pa;
    list pb;
    list pc;

    pa = la->pNext;
    pb = lb->pNext;
    *lc = pc = la;

    while (pa && pb) {
        if (pa->data <= pb->data) {
            pc->pNext = pa;
            pc = pa;
            pa = pa->pNext;
        } else {
            pc->pNext = pb;
            pc = pb;
            pb = pb->pNext;
        }
    }
    while (pa) {
        pc->pNext = pa;
        pc = pa;
        pa = pa->pNext;
    }
    while (pb) {
        pc->pNext = pb;
        pc = pb;
        pb = pb->pNext;
    }
    free(lb);
}
合并算法有点问题,现已改好了,你可以跑一下试试; 1. 合并排序的参数,lb不需要传递二重指针,因为不要将lb设置为传入传出参数,只需要作为传入参数即可,并在merge函数里将其释放。或者在main函数里释放,都是可以的。 2. 合并排序时,有可能两个链表的长度不一样,那么就需要将链表较长的最后加到链表lc中。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧