头文件:
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
#define SUCCESS 1000001
#define FAILURE 1000002
#define TRUE 1000003
#define FALSE 1000004
typedef int ElemType;
struct node
{
ElemType data;
struct node *next;
};
typedef struct node Node;
typedef Node *LinkList;
int ListInit(LinkList *L);
int ListInsert(LinkList *L,int i,ElemType e);
int ListTraverse(LinkList L,void(*print)(ElemType));
int GetElem(LinkList L,int k,ElemType *e);
int ListLength(LinkList L);
int ListEmpty(LinkList L);
int ListDelete(LinkList L,int i,ElemType *e);
#endif
子函数:
#include <stdio.h>
#include "LinkList.h"
#include <stdlib.h>
int ListInit(LinkList *L)
{
(*L) = (LinkList)malloc(sizeof(Node));
if(NULL == (*L))
{
return FAILURE;
}
(*L)->next = NULL;
return SUCCESS;
}
int ListInsert(LinkList *L,int i,ElemType e)
{
LinkList p = *L;
int j = 1;
while( p != NULL && j < i)
{
p = p->next;
j++;
}
if( p == NULL || j > i)
{
return FAILURE;
}
LinkList n = (LinkList)malloc(sizeof(Node));
if(NULL == n)
{
return FAILURE;
}
n->data = e;
n->next = p->next;
p->next = n;
}
int ListTraverse(LinkList L,void(*print)(ElemType))
{
if(NULL == L)
{
return FAILURE;
}
LinkList p = L->next;
while(p)
{
print(p->data);
p = p->next;
}
return SUCCESS;
}
int GetElem(LinkList L,int k,ElemType *e)
{
int length = 0;
LinkList p = L->next;
while(p)
{
length++;
if(k == length)
{
break;
}
p = p->next;
}
if(k > length)
{
return FAILURE;
}
*e = p->data;
return SUCCESS;
}
int ListLength(LinkList L)
{
int length = 0;
LinkList p = L->next;
while(p)
{
length++;
p = p->next;
}
return length;
}
int ListEmpty(LinkList L)
{
return (L->next == NULL) ? TRUE : FALSE;
}
int ListDelete(LinkList L,int i,ElemType *e)
{
int j = 1;
LinkList p = L;
LinkList tmp;
while(p != NULL && j<i)
{
p = p->next;
j++;
}
if(p == NULL || j > i)
{
return FAILURE;
}
tmp = p->next;
p->next = tmp->next;
free(tmp);
return SUCCESS;
}
int ListClean(LinkList L)
{
LinkList p = L->next;
if(!p)
{
return SUCCESS;
}
while(p)
{
L->next = p->next;
free(p);
p = L->next;
}
}
int ListDestroy(LinkList *L)
{
free(*L);
*L = NULL;
return SUCCESS;
}
主函数:
#include <stdio.h>
#include "LinkList.h"
void visit(ElemType e)
{
printf("%d ",e);
}
int main()
{
int ret,i;
LinkList list;
int place;
ElemType e;
ret = ListInit(&list);
if(FAILURE == ret)
{
printf("Init Failure!\n");
}
else
{
printf("Init success!\n");
}
for(i = 0; i < 10; i++)
{
ret = ListInsert(&list,i+1,i+1);
if(FAILURE == ret)
{
printf("Insert %d Failure!\n",i+1);
}
else
{
printf("Insert %d Success!\n",i+1);
}
}
ret = ListTraverse(list,visit);
if(FAILURE == ret)
{
printf("Traverse Failure!\n");
}
else
{
printf("Traverse Success!\n");
}
place = 4;
ret = GetElem(list,place,&e);
if(FAILURE == ret)
{
printf("Get %dth Failure!\n",place);
}
else
{
printf("%dth element is %d \n",place,e);
}
printf ("The length is %d\n",ListLength(list));
ret = ListEmpty(list);
if(TRUE == ret)
{
printf("List is Empty!\n");
}
else
{
printf("List is not Empty!\n");
}
place = 3;
ret = ListDelete(list,place,&e);
if(SUCCESS == ret)
{
printf("Delete %d Success!\n",place);
}
else
{
printf("Delete Failure!\n");
}
ret = ListTraverse(list,visit);
if(FAILURE == ret)
{
printf("Traverse Failure!\n");
}
else
{
printf("Traverse Success!\n");
}
ret = ListClean(list);
{
if(SUCCESS == ret)
{
printf("Clean success!\n");
}
}
printf ("The length is %d\n",ListLength(list));
ret = ListDestroy(&list);
if(SUCCESS == ret)
{
printf("Destroy Success!\n");
}
return 0;
}