#include <stdlib.h>
#include <stdio.h>
#define LEN sizeof(DAT)
#define TRUE 1
#define FALSE -1
#define NULL 0
typedef struct LNode{
int data;
struct LNode *next;
}DAT;
DAT *InitList(DAT *head)//创建空表
{
head = (DAT *)malloc(LEN);
if(!head)
{printf("\n--链表创建失败--\n");exit(FALSE);}
head ->next = NULL;
return head;
}
DAT *creatList(DAT *head)//数据录入
{
DAT *p1,*p2;
char ch;
int n = 1;
head = (DAT *)malloc(LEN);
p1 = p2 = (DAT *)malloc(LEN);
if(p1 != NULL)
{
printf("\n请输入数据:\n");
scanf("%d",&p1->data);//p1需要存储数据,每一次存储都需要申请空间
head -> next = p1;//注意:本程序的头结点不存储数据,因为ClearList函数和destroyList函数无法区分实现
//所以ListTraverse输出函数也是从头结点的下一个才开始输出的!
//p2 = p1;为什么这步可以省略呢?就是因为p1,p2在申请空间的时候被赋予的地址是一致的!
printf("\n继续录入(y/any other keys to exit)?\n");
getchar();
scanf("%c",&ch);
while(ch == 'Y'||ch == 'y')
{
printf("\n请输入数据:\n");
p1 = (DAT *)malloc(LEN);
if(p1 != NULL)
scanf("%d",&p1->data);
p2 -> next = p1;
p2 = p1;
printf("\n继续录入(y/any other keys to exit)?\n");
getchar();
scanf("%c",&ch);
}
p2 -> next = NULL;//p2作为连接新建节点的指针,如果next为空,那自然是结束了录入
printf("\n--录入结束--\n");
}