1.存储结构
typedef int Static;
typedef int ElemType;
struct DuLNode{
ElemType date;
struct DuLNode *prior;
struct DuLNode *next;
};
typedef struct DuLNode *DuLinkList;
2.创建一个空的循环链表
void InitList(DuLinkList &L)
{
L = (DuLinkList)malloc(sizeof(DuLNode));
if(!L)
exit(OVERFLOW);
L->prior = L->next = L;
}
3.将链表设置为空表
void ClearList(DuLinkList &L)
{
DuLinkList q,p = L->next;
while(p!=L)
{
q = p->next;
free(p);
p = q;
}
}
4.销毁链表
void DestoryList(DuLinkList &L)
{
ClearList(L);
free(L);
L = NULL;
}
5.判断表 L 是否为空
Static isEmpty(DuLinkList L)
{
if(L->prior == L->next)
return TURE;
return FALSE;
}
6.求表 L 的长度
int getLength(DuLinkList L)
{
int len = 0;
DuLinkList p = L->next;
while(p!=L)
{
p = p->next;
len++;
}
return len;
}
7.取出第 i 个元素
ElemType getElem(DuLinkList L, int i,ElemType &e)
{
int count = 1;
DuLinkList p = L->next;
while(count < i && p!=L)
{
p = p->next;
count++;
}
if(count>i || p==L)
return ERROR;
e = p->date;
return OK;
}
8.找出元素 e 在表中的位置
int getLocation(DuLinkList L,ElemType e)
{
int count = 1;
DuLinkList p = L->next;
while(p!=L && p->date!=e)
{
p = p->next;
count++;
}
if(p==L)
return 0;
return count;
}
9.找出元素 e 在表中前一个元素
Static getPrior(DuLinkList L,ElemType e,ElemType &pre_e)
{
DuLinkList p = L->next->next;
while(p!=L && p->date!=e)
p = p->next;
if (p == L)
return FALSE;
pre_e = p->prior->date;
return OK;
}
10.找出元素 e 在表中后一个元素
Static getNext(DuLinkList L,ElemType e, ElemType &next_e)
{
DuLinkList p = L->next;
while(p!=L && p->date!=e)
p = p->next;
if(p==L)
return ERROR;
next_e = p->next->date;
return OK;
}
11.在表中第 i 个位置插入元素 e
Static ListInsert(DuLinkList &L,int i,ElemType e)
{
if(i<=0 || i>getLength(L)+1)
return ERROR;
DuLinkList q,p = L;
int count = 0;
while(count<i-1)
{
p = p->next;
count++;
}
q = (DuLinkList)malloc(sizeof(DuLNode));
q->date = e;
q->next = p->next;
q->prior = p;
p->next->prior = q;
p->next = q;
return OK;
}
12.在表中第 i 个位置删除元素 e
Static ListDelete(DuLinkList &L,int i,ElemType &e)
{
if(i<=0 || i>getLength(L))
return ERROR;
DuLinkList q,p = L;
int count = 0;
while(count<i-1)
{
p = p->next;
count++;
}
q = p->next;
e = q->date;
p->next = q->next;
q->next->prior = p;
free(q);
return OK;
}
13.打印链表
void visitList(DuLinkList L)
{
DuLinkList p = L->next;
while(p!=L)
{
printf("%d\n",p->date);
p = p->next;
}
putchar('\n');
}