数据结构---双向链表

在单向链表的基础上的扩展

(1)初始化

复制代码
typedef struct Node
{
    int data;
    struct Node *pre;//指向前一个结点
    struct Node *next;//指向后一个结点
}Node,*DoubleLinkList;
//初始化,产出一个头结点
DoubleLinkList InitList(){
    //分配内存
    Node *head = (Node*)malloc(sizeof(Node));
    if(head == NULL)
    {
        printf("内存申请失败\n");
        return head;
    }
    head->pre = NULL;
    head->next = NULL;
    //没有对data赋值,不代表data没有数据
    return head;
}

(2)添加数据

1,头插法

复制代码
//头插法
DoubleLinkList headInsert(DoubleLinkList l,int k){
    //1.新结点
    Node *s = (Node*)malloc(sizeof(Node));
    //判断
    if(s == NULL)
    {
        printf("内存申请失败\n");
        return l;
    }
    //2.初始化新结点
    s->data = k;
    s->next = l->next;
    s->pre = l;
    //添加
    if(l->next != NULL)
    {
        l->next->pre = s;
    }
    l->next = s; 

    return l;
}

2,尾插法

复制代码
//尾插法
DoubleLinkList tailInsert(DoubleLinkList l,int k){
    //1.新结点
    Node *s = (Node*)malloc(sizeof(Node));
    //判断
    if(s == NULL)
    {
        printf("内存申请失败\n");
        return l;
    }
    //2.初始化新结点
    s->data = k;
    s->next = NULL;
    //查找并添加
    Node *temp = l;
    while(temp->next!=NULL)
    {
        temp = temp->next;
    }
    s->pre = temp;
    temp->next = s;

    return l;

}

3,中间插入

复制代码
//中间插入
DoubleLinkList middleInsert(DoubleLinkList l,int x,int k){
    //新结点
    Node *s = (Node*)malloc(sizeof(Node));
    s->data = k;

    Node*p = Find(l,x);
    if(p == NULL)
    {
        printf("查无此数据\n");
        return l;
    }
    s->next = p->next;
    if(p->next!=NULL)
    {
        p->next->pre = s;
    }
    p->next = s;
    s->pre = p;
    
    return l;

}

(3)查找

复制代码
//查找
Node *Find(DoubleLinkList l,int x){
    Node *temp = l->next;
    while(temp!=NULL&&temp->data!=x)
    {
        temp = temp->next;
    }
    return temp;
}

(4)删除

复制代码
//删除
DoubleLinkList delete(DoubleLinkList l,int x){
    Node *temp = Find(l,x);
    if(temp == NULL)
    {
        printf("查无此数据\n");
        return l;
    }
    temp->pre->next = temp->next;
    if(temp->next != NULL)
    {
        temp->next->pre = temp->pre;
    }
    free(temp);
    temp = NULL;
    return l;
}

(5)修改

复制代码
//修改
DoubleLinkList changeList(DoubleLinkList l,int x,int k){
    Node* temp = Find(l,x);
    if(temp == NULL)
    {
        printf("查无此数据\n");
        return l;
    }
    temp->data = k;
    return l;
}

(6)输出

复制代码
//输出
void printff(DoubleLinkList l){
    Node *temp = l->next;
    if(temp == NULL)
    {
        printf("空链表,数据为空\n");
    }
    while(temp!=NULL)
    {
        printf("%d ",temp->data);
        temp = temp->next;
    }
}
相关推荐
韧竹、12 分钟前
数据结构之单链表
数据结构·链表
And_Ii13 分钟前
LeetCode 3350. 检测相邻递增子数组 II
数据结构·算法·leetcode
胖咕噜的稞达鸭17 分钟前
C++中的父继子承(2)多继承菱形继承问题,多继承指针偏移,继承组合分析+高质量习题扫尾继承多态
c语言·开发语言·数据结构·c++·算法·链表·c#
冷月葬花~1 小时前
day24
数据结构·算法·leetcode
仰泳的熊猫2 小时前
LeetCode:538. 把二叉搜索树转换为累加树/1038. 从二叉搜索树到更大和树
数据结构·c++·算法·leetcode
在繁华处3 小时前
C语言初步学习:数组的增删查改
c语言·数据结构·学习
月夜的风吹雨3 小时前
【数据结构】:C 语言常见排序算法的实现与特性解析
c语言·数据结构·排序算法
Cx330❀3 小时前
《C++ 手搓list容器底层》:从结构原理深度解析到功能实现(附源码版)
开发语言·数据结构·c++·经验分享·算法·list
仰泳的熊猫3 小时前
LeetCode:98. 验证二叉搜索树
数据结构·c++·算法·leetcode
杨福瑞3 小时前
C语言数据结构:算法复杂度(1)
c语言·开发语言·数据结构