1.双链表
初始化
typedef struct Dnode{
ElemType data;
struct Dnode *prior,*next;
}Dnode,*DLinklist;
bool InitDLinklist(DLinklist &L)
{
L=(Dnode *)malloc(sizeof(Dnode));
if(L==null)
return false;
L-prior=null;
L->next=null;
return true;
}
插入
插入需要考虑,如果在尾部插入,则如何处理尾结点,next指向null
//在p结点后插入s结点
bool InsertNextDnode(DNode *p,Dnode *s)
{
if(s==null||p=null)
return false;
//if(p->next==null)//若p无后继结点
if(p->next==null)//尾结点 ????
{
s->next=P->next;
p->prior=s;
}
s-next=p->next;
p->next->prior=s;//如果是尾结点,error
s->prior=p;
p->next=s;
return true;
}
删除
//删除p结点的后继结点q
bool DeleteNextDnode(Dnode *p)
{
Dnode *q;//指向被删除结点
if(p->next==null||p=null)
return false;//尾结点不可以删
q=p->next;
q->next->prior=p;
p->next=q->next;
free(q);
return true;
}
2.循环单链表
typedef struct Lnode{
ElemType data;
struct Lnode *next;
}Lnode,* Linklist;
//初始化
bool InitList(Linklist &L)
{
L=(Lnode *)malloc(sizeof(Lnode));
if(L==null)
return false;
L->next=L;
return true;
}
//判表空
if(L->next==L)
return true;//表空
3.循环双链表
typedef InitDLinklist(DLinklist &L)
{
ElemType data;
struct Dnode *prior,*next;
}Dnode,*DLinklist;
//初始化
bool InitDLinklist(DLinklist &L)
{
L=(Lnode *)malloc(sizeof(Lnode));
if(L==null)
return null;
L->next=L;
L->prior=L;
return ture;
}
//判表空
if(L->next=L)
return true;
//插入;在p后插入q
bool InsertNextDnode(Dnode *p,Dnode *q)
{
//与双链表不同无需考虑边界
}
4.静态链表
分配连续的一整片空间,插入,删除等只需要修改指针,不需要移动元素,以next==-1为结束标志。