注册 登录
编程论坛 数据结构与算法

链表的创建与释放

唯佳 发布于 2012-01-12 08:48, 964 次点击
//程序名称:create
//程序目的:设计一个将输入的数据建立成链表的程序
#include <stdio.h>
#include <stdlib.h>
#define Max 10

struct List
{
    int Number;
    char Name[Max];
    struct List *Next;
};
typedef struct List Node;
typedef Node *Link;
//释放链表
void Free_List(Link Head)
{
    Link Pointer;

    while( Head != NULL)
    {
        Pointer=Head;
        Head=Head->Next;
        free(Pointer);
    }

}

//输出链表数据
void Print_List(Link Head)
{
    Link Pointer;
    Pointer = Head;
    while( Pointer != NULL )
    {
        printf("##Input Data##\n");
        printf("Data Number :%d \n",Pointer->Number);
        printf("Data Name :%s \n",Pointer->Name);
        Pointer=Pointer->Next;
    }
}

//建立链表
Link Create_List(Link Head)
{
    int DataNum;
    char DataName[Max];
    Link New;
    Link Pointer;
    int i;

    Head = (Link)malloc(sizeof(Node));

    if ( Head == NULL )
        printf("Memory allocate Fauilure!!\n");
    else
    {
        DataNum = 1;
        printf("Please input data name : \n");
        scanf("%s",DataName);
        Head->Number = DataNum;

        for (i=0;i<=Max;i++)
            Head->Name[i] = DataName[i];

            Head->Next = NULL;

            Pointer = Head;

            while(1)
            {
                DataNum++;
                New = (Link)malloc(sizeof(Node));
                    printf("Please input the data name : ");
                    scanf("%s",DataName);
                if(DataName[0] == '0')
                    break;
                New->Number = DataNum;
            for (i=0 ; i <=Max; i++)
            {
                New->Name[i] = DataName[i];
            }
            New->Next = NULL;

            Pointer->Next = New;

              Pointer = New;
            }
    }
    return Head;
}

void main()
{
    Link Head;

    Head=Create_List(Head);//调用建立链表

    if ( Head != NULL)
    {
        Print_List(Head);
        Free_List(Head);
    }

}
7 回复
#2
唯佳2012-01-12 08:50
看过没错的源码
但我写的时候 老是不成功
编辑器codeblocks
问题何在
#3
唯佳2012-01-12 08:52
还有、
typedef struct List Node;
typedef Node *Link;
什么作用啊
我初学
看到其他的链表 没这样的声明啊
照样能用
#4
silent_world2012-01-12 09:30
修改源码如下,可以正常使用:
#include <stdio.h>
#include <stdlib.h>
#define Max 10

struct List
{
    int Number;
    char Name[Max];
    struct List *Next;
};

typedef struct List Node;
typedef Node *Link;

//释放链表
void Free_List(Link Head)
{
    Link Pointer;

    while( Head != NULL)
    {
        Pointer=Head;
        Head=Head->Next;
        free(Pointer);
    }

}

//输出链表数据
void Print_List(Link Head)
{
    Link Pointer;
    Pointer = Head;
    while( Pointer != NULL )
    {
        printf("##Input Data##\n");
        printf("Data Number :%d \n",Pointer->Number);
        printf("Data Name :%s \n",Pointer->Name);
        Pointer=Pointer->Next;
    }
}

//建立链表
Link Create_List()
{
     int DataNum = 0;
     char DataName[Max];
     Link New = 0, Head = 0;
     Link Pointer = 0;
     int i;

     Head = (Link)malloc(sizeof(Node));

     if ( Head == NULL )
          printf("Memory allocate Fauilure!!\n");
     else
     {
          memset(Head, 0, sizeof(Node));
          memset(DataName, 0, Max);

          DataNum = 1;
          printf("Please input data name : \n");
          scanf("%s",DataName);
          Head->Number = DataNum;

          for(i = 0; i <= Max; i++)
               Head->Name[i] = DataName[i];

          Head->Next = NULL;

          Pointer = Head;

          while(1)
          {
               DataNum++;

               memset(DataName, 0, Max);
               printf("Please input the data name : ");
               scanf("%s", DataName);
               if(DataName[0] == '0')
               {
                    break;
               }

               New = (Link)malloc(sizeof(Node));
               if( New == NULL )
               {
                    printf("Memory allocate Fauilure!!\n");
                    return Head;
               }
               memset(New, 0, sizeof(Node));

               New->Number = DataNum;
               for (i=0 ; i <=Max; i++)
               {
                    New->Name[i] = DataName[i];
               }
               New->Next = NULL;

               Pointer->Next = New;

               Pointer = New;
          }
     }

     return Head;
}

void main()
{
    Link Head;

    Head=Create_List();//调用建立链表

    if( Head != NULL)
    {
        Print_List(Head);
        Free_List(Head);
    }

}
#5
silent_world2012-01-12 09:32
源码整体还不错,有一些小的问题:
1、会出现内存泄漏;
2、有些资源没有reset,会出现信息错误。
#6
唯佳2012-01-12 10:13
回复 5楼 silent_world
我 试了下
但是 输不出来
还是在 一直让输名字
#7
唯佳2012-01-12 10:25
懂了
#8
yuanspring2012-01-12 15:34
职位信息,某国最大互联网公司成都研发中心急寻高级测试工程师-测试leader(BASE地点:成都,因此,成都本地的GGJJ们看过来,如果您在北京,想要回成都工作,或是家乡为重庆、云南、贵州、湖北、湖南的GGJJ们,也可以到成都来工作。)如想要了解详细情况,QQ30683890 .也有其他研发类职位,windows C/C++ Linux C/C++ java类职位,欢迎为哪些想回成都的朋友引荐。
1