软件152笪世海
#include "stdio.h"
#include <stdlib.h>
#include <time.h>
enum Status{OK,ERROR};
typedef int ElemType;
typedef struct LNode
{
ElemType data;///链表结点的元素,类型为int
LNode *next;//指向下一结点指针
}LNode,*LinkList;
Status CreatLink(LinkList &L,int n)///建立链表
{
LinkList p;
LinkList Head;
int i;
L = (LinkList)malloc(sizeof(LNode));///链表头结点
L->next = NULL;
Head = L;
if (L == NULL)
{
return ERROR;
}
for (i = 0; i < n; ++i)
{
p = (LinkList)malloc(sizeof(LNode));///产生新结点
if (p == NULL)
{
return ERROR;
}
printf("please input a number:\n");
scanf("%d",&p->data);
p->next = NULL;
Head->next = p;//尾插法插入新结点
Head = p;
}
return OK;
}
Status DestroyLink(LinkList &L)///销毁链表
{
LinkList p = L->next;
LinkList q;
while (p != NULL)///判断下一结点是不是空
{
q = p->next;
free(p);///释放当前结点
p = q;
}
free(L);///释放链表头结点
return OK;
}
Status PrintLink(LinkList L)////打印链表
{
LinkList P = L->next;
while (P != NULL)
{
printf("%d ",P->data);
P = P->next;
}
printf("\n");
return OK;
}
int main()
{
LinkList L;
CreatLink(L,5);
PrintLink(L);
DestroyLink(L);
return 0;
}