#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED
#include "ds.h"
#ifndef ElemType
#define ElemType int
#define ELEMTYPE_TAG
#endif
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, *LinkList;
Status DestroyList(LinkList &L);
Status ClearList(LinkList &L);
bool ListEmpty(LinkList L);
int ListLength(LinkList L);
Status GetElem(LinkList L, int i, ElemType &e);
template <typename T> bool equal(T a, T b)
{
return a==b;
}
int LocateElem(LinkList L, ElemType e,
bool (*compare)(ElemType,ElemType)=equal<ElemType>);
Status ListInsert(LinkList &L, int i, ElemType e);
Status ListDelete(LinkList &L, int i, ElemType &e);
Status ListTraverse(LinkList L, Status (*visit)(ElemType));
Status InitList(LinkList &L)
{
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
return OK;
}
Status DestroyList(LinkList &L)
{
return ERROR;
}
Status ClearList(LinkList &L)
{
L->next=NULL;
return OK;
}
bool ListEmpty(LinkList L)
{
if(!L->next)
return OK;
}
int ListLength(LinkList L)
{
LinkList P=L;
int length=0;
printf("开\n");
while(P){
length++;
P=P->next;
}
return length;
}
Status GetElem(LinkList L, int i, ElemType &e)
{
return ERROR;
}
int LocateElem(LinkList L, ElemType e, bool (*compare)(ElemType,ElemType))
{
LinkList p;
int j=0;
p = L->next; j = 1;
while(p!=NULL) {
if( compare(p->data,e) ) return j;
j++;
p=p->next;
}
return 0;
}
Status ListInsert(LinkList &L, int i, ElemType e)
{
if(i<1&&i>ListLength(L))return ERROR;
LinkList P=L;
int length=1;
while(length!=i){
length++;
P=P->next;
}
LinkList temp=(LNode*)malloc(sizeof(LNode));
temp->next=P->next;
temp->data=e;
P->next=temp;
return OK;
}
Status ListDelete(LinkList &L, int i, ElemType &e)
{
if(i<1&&i>ListLength(L))return ERROR;
LinkList P=L;
int length=1;
while(length!=i){
length++;
P=P->next;
}
LinkList temp=(LNode*)malloc(sizeof(LNode));
temp=P->next;
e=temp->data;
P->next=temp->next;
return OK;
}
Status ListTraverse(LinkList L, Status (*visit)(ElemType))
{
LinkList p = L->next;
while (p) {
if ( visit(p->data)==ERROR ) return ERROR;
p = p->next;
}
return OK;
}
#ifdef ELEMTYPE_TAG
#undef ElemType
#undef ELEMTYPE_TAG
#endif
#endif // LINKLIST_H_INCLUDED