首页 > 用户发贴区 > 编程问题提问区 > free函数的疑惑?
2008
02-18

free函数的疑惑?

对于链表来说,怎样free 呢?SAMPLE:


#include “Stdio.h”
#include “stdlib.h”
#define LEN sizeof(struct student)
struct student
{
    long num;
    char name[10];
    float scores[2];
    struct student *next;
}stu;


struct student * creat_list(void);
void print(struct student *head);
int main(void)
{
 /* 此处添加你自己的代码 */
struct student *pa=NULL,*pb=NULL;
pa=creat_list();
print(pa);
getch();
}


struct student * creat_list(void)
{


    struct student * head=NULL,*ppre=NULL,*pafter=NULL;
    ppre=pafter=(struct student *)malloc(sizeof(struct student));
    printf(“请输入学号:”);
    scanf(“%ld”,&ppre->num);
    if(ppre->num!=0)
    {
        printf(“请输入姓名:”);
        scanf(“%s”,ppre->name);
        printf(“请输入数学成绩:”);
        scanf(“%f”,&ppre->scores[0]);
        printf(“请输入语文成绩:”);
        scanf(“%f”,&ppre->scores[1]);
        head=ppre;
        while(pafter->num!=0)
        {
            pafter=(struct student *)malloc(sizeof(struct student));
            printf(“请输入学号:”);
            scanf(“%ld”,&pafter->num);
            if(pafter->num==0)
                ppre->next=NULL;
            else
            {
                printf(“请输入姓名:”);
                scanf(“%s”,pafter->name);
                printf(“请输入数学成绩:”);
                scanf(“%f”,&pafter->scores[0]);
                printf(“请输入语文成绩:”);
                scanf(“%f”,&pafter->scores[1]);
                ppre->next=pafter;
                ppre=pafter;
            }
        }
       
    }
   
    free(pafter);
    free(ppre);
   
    return (head);
}


void print(struct student *head)
{
    struct student *q=NULL;
   
    printf(“\nnow print:\n”);
    q=head;
    if(head!=NULL)
    do
    {  
        printf(“%ld  %s  %5.1f  %5.1f\n”,q->num,q->name,q->scores[0],q->scores[1]);
        q=q->next;
    }while(q!=NULL);
    else printf(“None\n”);
}


结果好怪!GCC编译的!


free函数的疑惑?》有 2 条评论

  1. wfnh 说:

    没人啊!?

     

     

  2. wfnh 说:

    不会吧!!?没人!

     

留下一个回复