带有头结点的单链表的12个基本操作
#define DestroyList ClearList//DestroyList()和ClearList()的操作是一样的
void InitList(LinkList &L){
L = NULL;
}
void ClearList(LinkList &L){
LinkList p;
while (L){
p = L;
L = L->next;
free(p);
}
}
Status ListEmpty(LinkList L){
if (L)return FALSE;
else return TRUE;
}
int ListLength(LinkList L){
LinkList p = L;
int i = 0;
while (p)
{
i++;
p = p->next;
}
return i;
}
Status GetElem(LinkList L, int i, ElemType &e){
int j = 1;
LinkList p = L;
if (i < 1)return ERROR;
while (j < i && p){
j++;
p = p->next;
}
if (j == i && p){
e = p->data;
return OK;
}
return ERROR;
}
int LocateElem(LinkList L, ElemType e, Status(*compare)(ElemType, ElemType)){
int i = 1;
LinkList p = L;
while (p){
i++;
if (compare(p->data, e))
return i;
p = p->next;
}
return 0;
}
Status PriorElem(LinkList L, ElemType cur_e, ElemType &pre_e){
LinkList q, p = L;
while (p->next){
q = p->next;
if (q->data == cur_e){
pre_e = p->data;
return OK;
}
p = q;
}
}
Status NextElem(LinkList L, ElemType cur_e, ElemType &next_e){
LinkList p = L;
while (p->next){
if (p->data == cur_e){
next_e = p->next->data;
return OK;
}
p = p->next;
}
return ERROR;
}
Status ListInsert(LinkList L, int i, ElemType e){
int j = 1;
LinkList s, p = L;
if (i < 1)return ERROR;
s = (LinkList)malloc(sizeof(LNode));
s->data = e;
if (i == 1){
s->next = L;
L = s;
}
else
{
while (p && j < i - 1){
j++;
p = p->next;
}
if (!p)return ERROR;
s->next = p->next;
p->next = s;
}
return OK;
}
Status ListDelete(LinkList L, int i, ElemType &e){
int j = 1;
LinkList q, p = L;
if (!L)return ERROR;
else if (i == 1){
L = p->next;
e = p->data;
free(p);
}
else
{
while (p->next && j < i - 1){
j++;
p = p->next;
}
}
if (!p->next || j > i - 1){
return ERROR;
q = p->next;
p->next = q->next;
e = q->data;
free(q);
}
return OK;
}
void ListTraverse(LinkList L, void(*visit)(ElemType&)){
LinkList p =L;
while (p)
{
visit(p->data);
p = p->next;
}
printf("\n");
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/zhou554291911/article/details/46957579