C++-21-双向链表

双向链表

//双链表
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

//定义双向链表结点
typedef struct DLNode
{
    int iNum;              //数据域
    struct DLNode *prior;
    struct DLNode *next;
}DLNodes, *PDLNodes;
/*
pphead : 头指针
pptail :尾指针
i      : 结点值
插入结点(头插法,在左边插入结点)
*/
void list_insert_head(PDLNodes *pphead, PDLNodes *pptail, int i)
{
    PDLNodes pnew;
    pnew = (PDLNodes)calloc(1, sizeof(DLNodes));
    pnew->iNum = i;

    if(*pptail == NULL)           //如果链表为空
    {
        *pphead = pnew;
        *pptail = pnew;
    }
    else
    {
        pnew->next = *pphead;      //新结点的next成员指向原有链表
        (*pphead)->prior = pnew;   //新结点的prior指向原有链表头
        *pphead = pnew;            //新结点成为新的链表头
    }
}
//打印输出双向链表结点
void list_print_func(PDLNodes phead,PDLNodes ptail)
{
    PDLNodes pcur = phead;         //将头指针给当前指针pcur
    printf("\n顺序输出双向链表的所有结点:\n");
    while(pcur != NULL)
    {
        printf("%d\t",pcur->iNum);
        pcur = pcur->next;
    }
    printf("\n");

    pcur = ptail;                  //将尾指针
    printf("\n逆序输出双向链表的所有结点:\n");
    while(pcur != NULL)
    {
        printf("%d\t",pcur->iNum);
        pcur = pcur->prior;
    }
    printf("\n");
}

//删除结点
void list_delete_func(PDLNodes *pphead,PDLNodes *pptail,int i)
{
    PDLNodes pcur = *pphead;
    PDLNodes pbefore = *pphead;
    if(NULL == *pptail)          //证明链表为空
    {
        printf("\n双向链表为空.\n");
    }
    else if(i == (*pphead)->iNum)//如果要删除第一个结点(此时可能只剩下一个结点)
    {
        (*pphead)->next->prior = (*pphead)->prior;//下一个结点的prior指向原有头结点的prior
        *pphead = (*pphead)->next;//头指针要指向下一个结点
        free(pcur);             //释放原有头结点的内存
        if(*pphead == NULL)     //如果删除之后链表为空
        {
            *pptail = NULL;
        }
    }
    else                       //如果不等于第一个结点
    {
        while(NULL != pcur)    //如果pcur没有走出最后一个结点
        {
            if(i == pcur->iNum)
            {
                pcur = pcur->next;
                pbefore->next = pcur;
                pcur->prior = pbefore;
                return;
            }
            else
            {
                pbefore = pcur;
                pcur = pcur->next;
            }
        }
        if(NULL == pcur)        //如果执行到最后
        {
            printf("\n证明你要删除的数据结点不存在");
        }
    }
}

//查找结点
void list_find_func(PDLNodes *pphead,PDLNodes *pptail,int i)
{
    PDLNodes pcur = *pphead;
    PDLNodes pbefore = *pptail;
    if(NULL == *pptail)        //证明链表为空
    {
        printf("\n双向链表为空.\n");
    }
    else
    {
        while(NULL != pcur)    //只要不是最后
        {
            if(i == pcur->iNum)
            {
                printf("\n查询成功,你要查找的数据存在.\n");
                return;
            }
            else
            {
                pbefore = pcur;
                pcur = pcur->next;
            }
        }
        if(NULL == pcur)       //如果执行到最后
        {
            printf("\n证明你要查找的数据结点不存在.\n\n");
        }
    }
}
int main(int argc,char* argv[])
{
    int i;
    PDLNodes phead = NULL;
    PDLNodes ptail = NULL;

    // 插入结点
    printf("\n\n请输入你要插入的结点:\n");
    while(scanf("%d",&i) != EOF)
    {
        //调用插入结点函数
        list_insert_head(&phead, &ptail, i);
    }
    //输出操作
    list_print_func(phead,ptail);
    
    //删除结点
    printf("\n\n请输入你要删除结点的数据:");
    while (scanf("%d",&i) != EOF)
    {
        //调用删除结点函数
        list_delete_func(&phead,&ptail,i);
        printf("\n删除之后链表的结果为:");
        list_print_func(phead,ptail);
    }

    //查找结点
    printf("\n请你输入要查找的结点:");
    while(scanf("%d",&i) != EOF)
    {
        //调用查找函数
        list_find_func(&phead,&ptail,i);
    }
    return 0;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值