【数据结构算法】线性表(五):双向链表

双向链表

这里写图片描述
将单链表中终端结点的指针端由空指针改为指向头结点,就使整个单链表形成一个环,这种头尾相接的单链表称为单循环链表,简称循环链表。
其实循环链表和单链表的主要差异就在于循环链表判断空的条件,原来判断head->next是否为空,而现在则是head->next是否等于head。

代码

初始化循环链表
//链表存储结构的定义
typedef struct CLinkList
{
    int data;
    struct CLinkList *next;
}node;


//初始化循环链表
void init(node **pNode)
{
    int item;
    node *temp;
    node *target;

    printf("输入结点的值,输入0完成初始化\n");

    while(1)
    {
        scanf("%d",&item);

        if (item == 0)
            return;

        if ((*pNode) == NULL)
        {//循环链表中只有一个结点
            *pNode = (node*)malloc(sizeof(struct CLinkList));
            if (!(*pNode))
                exit(0);
            (*pNode)->data = item;
            (*pNode)->next = *pNode;
        }
        else
        {
            //找到next指向第一个结点的结点
            for(target = (*pNode); target->next != (*pNode); target = target->next)
                ;

            //生成一个新的结点
            if(!temp)
                exit(0);

            temp->data = item;
            temp->next = *pNode;
            target->next = temp;
        }
    }
}
插入结点
//链表存储结构的定义
typedef struct CLinkList
{
    int data;
    struct CLinkList *next;
}node;

//插入结点
//参数:插入的位置
void insert(node **pNode, int i)
{
    node *temp;
    node *target;
    node *p;
    int item;
    int j = 1;

    printf("输入要插入结点的值");
    scanf("%d", &item);

    if(i == 1)
    {
        //新插入的结点作为第一个结点
        temp = (node *)malloc(sizeof(struct CLinkList));

        if(!temp)
            exit(0);

        temp->data = item;

        //寻找最后一个结点
        for(target = (*pNode); target->next != (*pNode); target = target->next)
                ;

        temp->next = (*pNode);
        target->next = temp;
        *pNode = temp;
    }
    else
    {
        target = *pNode;
        for(; j<(i-1); ++j)
        {
            target = target->next;
        }

        temp = (node *)malloc(sizeof(struct CLinkList));

        if(!temp)
            exit(0);

        temp->data = item;

        p = target->next;
        target->next = temp;
        temp->next = p;
    }
}
删除结点
void delete(node **pNode, int i)
{
    node *target;
    node *temp;
    int j - 1;

    if(i == 1)
    {
        //删除第一个结点

        //找到最后的结点
        for(target = (*pNode); target->next != (*pNode); target = target->next)
                ;

        temp = *pNode;
        *pNode = (*pNode)->next;
        target->next = *pNode;
        free(temp);
    }
    else
    {
        target = *pNode;

        for(; j<(i-1); ++j)
        {
            target = target->next;
        }

        temp = target->next;
        target->next = temp->next;
        free(temp);
    }
}
返回结点所在位置
search(node *pNode, int elem)
{

    node *target;
    int i = 1;

    for(target = pNode; target->data != elem && target->next != pNode; ++i)
    {
        target = target->next;
    }

    if(target->next == pNode)
        return 0;
    else
        return i;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuanCruise

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值