数据结构.循环链表

1.为什么引用循环链表?

单链表中我们是把结点遍历一遍后就结束了,使表处理更加灵活,通过任意一个结点出发都可以找到链表中的其它结点,因此引用循环链表

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;
}

//判断循环单链表是否为空
b00l 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;
}


/* 循环双链表  头prior指向null 尾next指向头 */
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;     //头结点的前驱指向头结点
    L->next=L;      //头结点的后继指向头结点
}

//判断循环双链表是否为空
b00l 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;
}

//插入p节点之后s
bool Insertnext(dnode *p,dnode *s)
{
    s->next=p->next;
    p->next->prior=s;
    s->prior=p;
    p->next=s;
}

//删除p节点的后继节点 q;
bool delete(dnode *p,dnode *q)
{
    p->next=q->next;
    q->next->prior=p;
    free(q);
}

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值