#define OK 1
#define ERROR 0
#include "stdio.h"
#include "iostream.h"
typedef int Status;
typedef int ElemType;
typedef struct LNode {
ElemType data; // 数据域
struct LNode *next; // 指针域
} LNode, *LinkList;
LinkList L; // L 为单链表的头指针
Status CreateList(LinkList &L, int n)
{// 逆序输入 n 个数据元素,建立带头结点的单链表
if(n==0){cout<<"fail to create a List"<<endl;
return ERROR;}
L = new LNode;
if(!L)return ERROR;
L->next = NULL; // 先建立一个带头结点的单链表
printf("\nplease input the data:\n");
for (int i = n; i > 0; --i)
{ LNode* p = new LNode;
scanf("%d",&p->data); // 输入元素值
p->next = L->next;
L->next = p; // 插入
}
cout<<endl<<"success to create a List……(with a head LNode)"<<endl;
return OK;
}
//end of the CreateList_L() function
Status PrintList(LinkList L)
{//显示单链表
LNode*p=L->next;
while(p)
{printf("%d ",p->data);p=p->next;}
printf("\n");
return OK;
}
void ClearList()
{ // 将单链表重新置为一个空表
LNode* p;
while(L)
{p=L->next;
L->next=p->next;
delete (p);
}
cout<<endl<<"success to clear the List……"<<endl;
}
//end of the ClearList() function
void main()
{LinkList La;
int n;
printf("to create the list,input the number of your data:");
scanf("%d",&n);
CreateList(La,n);
printf("\noutput the data:\n");
PrintList(La);
ClearList();
}