数据结构

数据结构——循环链表

//循环单链表

typedef struct LNode{

    ElemType data;

    struct LNode *next;

}LNode,*LinkList;

 

//初始化一个循环单链表

bool InitList(LinkList &L){

    L=(Node*)malloc(sizeof(LNode));  //分配一个头节点

    if(L==NULL)

        return false;

    L->next=L;      //头节点next指向头节点

    return true;

}

 

//判空

bool Empty(LinkList L){

    if(L->next==L)

        return true;

    else

        return false;;

}

 

//判断p是否为表尾节点

bool isTail(LinkList L,LNode *p){

    if(p->next==L)

        return true;

    else

        return false;

}

//循环双链表

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 = L;     //头节点prior指向头节点

    L->next = L;      //头节点的next指向头节点

    return true;

}

 

//判空

bool Empty(DLinklist L){

    if(L->next==L)

        return true;

    else

        return false;

}

 

//判断节点p是否是循环链表的尾节点

bool isTail(DLinklist L,DNode *p){

    if(p->next==L)

        return true;

    else

        return false;

}

//插入

bool InsertNextDNode(DNode *p,Dnode *s){

    s->next=p->next//将节点s插入节点p之后

    p->next->prior=s;

    s->prior=p;

    p->next=s;

}


//删除p的后继节点

    p->next=q->next;

    q->next->prior=p;

    free(q);


留言

您的电子邮箱地址不会被公开。