链表的创建与释放
//程序名称: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);
}
}