#include <iostream>
#define MAX_SIZE 20
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int ElemType;
typedef int Status;
typedef struct Node
{
ElemType data;
struct Node* next;
};
typedef struct Node* linkList;
Status InitList(linkList* L)
{
(*L) = (linkList)malloc(sizeof(Node));
if ((*L) == NULL)
return ERROR;
(*L)->next = NULL;
return OK;
}
Status ClearList(linkList* L)
{
linkList p, q;
p = (*L)->next;
while (p != NULL)
{
q = p->next;
free(p);
p = q;
}
(*L)->next = NULL;
return OK;
}
Status isListEmpty(linkList L)
{
if (L->next != NULL)
return FALSE;
return TRUE;
}
int ListLength(linkList L)
{
int i = 0;
linkList p = L->next;
while (p)
{
i++;
p = p->next;
}
return i;
}
Status GetElem(linkList L, int i, ElemType *e)
{
linkList p = L->next;
int j = 1;
while ((p != NULL) && (j < i))
{
j++;
p = p->next;
}
if (p == NULL || j > i)
{
return ERROR;
}
*e = p->data;
return OK;
}
int LocateElem(linkList L, ElemType e)
{
int i = 1;
linkList p = L->next;
while (p)
{
if (p->data == e)
return i;
i++;
p = p->next;
}
return ERROR;
}
Status ListInsert(linkList* L, int i, ElemType e)
{
linkList p, q;
p = *L;
int j = 1;
while ((p != NULL) && j < i)
{
j++;
p = p->next;
}
if (!p || j > i)return ERROR;
q = (linkList)malloc(sizeof(Node));
if (q == NULL)return ERROR;
q->data = e;
q->next = p->next;
p->next = q;
return OK;
}
Status ListDelete(linkList* L, int i)
{
int j = 1;
linkList p = *L, q;
while (p && j < i)
{
j++;
p = p->next;
}
if (!p || j > i)return ERROR;
if (p->next == NULL)return ERROR;
q = p->next;
p->next = p->next->next;
free(q);
return OK;
}
Status ListTraverse(linkList L)
{
linkList p = L->next;
while (p)
{
printf("%d-->", p->data);
p = p->next;
}
printf("\n");
return OK;
}
Status CreatListHead(linkList* L, int n)
{
srand((unsigned)time(NULL));
linkList p;
*L = (linkList)malloc(sizeof(Node));
if((*L) == NULL)return ERROR;
(*L)->next = NULL;
while (n--)
{
p = (linkList)malloc(sizeof(Node));
if((p) == NULL)return ERROR;
p->data = rand() % 100 + 1;
p->next = (*L)->next;
(*L)->next = p;
}
return OK;
}
Status CreatListTail(linkList* L,int n)
{
srand((unsigned)time(NULL));
linkList p, q;
*L = (linkList)malloc(sizeof(Node));
if ((*L) == NULL)return ERROR;
q = *L;
while (n--)
{
p = (linkList)malloc(sizeof(Node));
if ((p) == NULL)return ERROR;
p->data = rand() % 100 + 1;
q->next = p;
q = p;
}
q->next = NULL;
return OK;
}
int main()
{
linkList L;
ElemType e = 0;
Status res;
int i, j;
res = InitList(&L);
printf("初始化后的长度:%d\n", ListLength(L));
for (i = 0; i < 5; i++)
{
res = ListInsert(&L, 1, i);
if (!res)printf("插入元素失败!\n");
}
printf("插入5个元素后:\n");
ListTraverse(L);
printf("插入5个元素后是否为空:%d(1:是 0:否)\n", res);
printf("插入5个元素后的长度:%d\n", ListLength(L));
res = isListEmpty(L);
printf("是否为空:%d(1:是 0:否)\n", res);
res = ClearList(&L);
printf("清空后后的长度:%d\n", ListLength(L));
printf("清空后是否为空:%d(1:是 0:否)\n", res);
for (i = 0; i < 10; i++)
{
res = ListInsert(&L, 1, i);
if (!res)printf("插入元素失败!\n");
}
printf("插入10个元素后:\n");
ListTraverse(L);
printf("插入10个元素后的长度:%d\n", ListLength(L));
GetElem(L, 5, &e);
printf("第5个元素为:%d\n",e);
j = ListLength(L);
res = ListDelete(&L, j + 1);
if (res == ERROR)
printf("删除第%d元素失败!\n", j + 1);
else
printf("删除第%d元素成功!\n", j);
res = ListDelete(&L, j);
if (res == ERROR)
printf("删除第%d元素失败!\n", j);
else
printf("删除第%d元素成功!\n", j);
printf("删除后:\n");
ListTraverse(L);
if (ListDelete(&L, 5))
{
printf("删除第五个元素成功!\n");
}
ListTraverse(L);
ClearList(&L);
CreatListHead(&L, 20);
printf("头部创建:\n");
ListTraverse(L);
ClearList(&L);
printf("尾部创建:\n");
CreatListTail(&L, 20);
ListTraverse(L);
ClearList(&L);
free(L);
return 0;
}
