【数据结构】双向链表

news2024/10/5 12:52:34

1.双向链表的结构

2.双向链表的实现

首先在VS里面的源文件建立test.cList.c,在头文件里面建立List.h

List.h:

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int LTDateType;
typedef struct ListNode
{
    LTDateType data;
    struct ListNode* next;
    struct ListNode* prev;
}LTNode;

2.1 初始化

List.h:

LTNode* ListInit(LTNode* phead);//初始化

List.c:

#include "List.h"
LTNode* ListInit()
{
    //哨兵位头结点
    LTNode* phead = (LTNode*)malloc(sizeof(LTNode));
    phead->next = phead;
    phead->prev = phead;
    return phead;
}

test.c

#include "List.h"
void TestList1()
{
    LTNode* plist = ListInit();
}
int main()
{
    TestList1();
    return 0;
}

2.2 打印

List.h:

void ListPrint(LTNode* phead);//打印

List.c:

void ListPrint(LTNode* phead)
{
    assert(phead);
    LTNode* cur = phead->next;
    while (cur != phead)
    {
        printf("%d ", cur->data);
        cur = cur->next;
    }
    printf("\n");
}

2.3 申请结点

List.h:

LTNode* BuyListNode(LTDateType x);//申请结点 

List.c:

LTNode* BuyListNode(LTDateType x)
{
    LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
    newnode->data = x;
    newnode->next = NULL;
    newnode->prev = NULL;
    return newnode;
}

2.4 尾插

List.h:

void ListPushBack(LTNode* phead, LTDateType x);//尾插 

List.c:

void ListPushBack(LTNode* phead, LTDateType x)
{
    assert(phead);
    LTNode* tail = phead->prev;
    LTNode* newnode = BuyListNode(x);
    tail->next = newnode;
    newnode->prev = tail;
    phead->prev = newnode;
    newnode->next = phead;
}

test.c

#include "List.h"
void TestList1()
{
    LTNode* plist = ListInit();
    ListPushBack(plist, 1);
    ListPushBack(plist, 2);
    ListPushBack(plist, 3);
    ListPrint(plist);
}
int main()
{
    TestList1();
    return 0;
}

运行结果:

2.5 尾删

List.h:

void ListPopBack(LTNode* phead);//尾删

List.c:

void ListPopBack(LTNode* phead)
{
    assert(phead);
    assert(phead->next != phead);//排除为空的情况
    LTNode* tail = phead->prev;
    phead->prev = tail->prev;
    tail->prev->next = phead;
    free(tail);
}

test.c

#include "List.h"
void TestList1()
{
    LTNode* plist = ListInit();
    ListPushBack(plist, 1);
    ListPushBack(plist, 2);
    ListPushBack(plist, 3);
    ListPrint(plist);
    ListPopBack(plist);
    ListPrint(plist);

}
int main()
{
    TestList1();
    return 0;
}

运行结果:

2.6 头插

List.h:

void ListPushFront(LTNode* phead, LTDateType x);//头插

List.c:

void ListPushFront(LTNode* phead, LTDateType x)
{
    assert(phead);
    LTNode* newnode = BuyListNode(x);
    LTNode* next = phead->next;
    phead->next = newnode;
    newnode->prev = phead;
    newnode->next = next;
    next->prev = newnode;
}

test.c

#include "List.h"
void TestList1()
{
    LTNode* plist = ListInit();
    ListPushBack(plist, 1);
    ListPushBack(plist, 2);
    ListPushBack(plist, 3);
    ListPrint(plist);
    ListPopBack(plist);
    ListPrint(plist);
    ListPushFront(plist, 10);
    ListPrint(plist);

}
int main()
{
    TestList1();
    return 0;
}

运行结果:

2.7 头删

List.h:

void ListPopFront(LTNode* phead);//头删

List.c:

void ListPopFront(LTNode* phead)
{
    assert(phead);
    assert(phead->next != phead);
    LTNode* next = phead->next;
    LTNode* nextNext = next->next;
    phead->next = nextNext;
    nextNext->prev = phead;
    free(next);
}

test.c

#include "List.h"
void TestList1()
{
    LTNode* plist = ListInit();
    ListPushBack(plist, 1);
    ListPushBack(plist, 2);
    ListPushBack(plist, 3);
    ListPrint(plist);
    ListPopBack(plist);
    ListPrint(plist);
    ListPushFront(plist, 10);
    ListPrint(plist);
    ListPopFront(plist);
    ListPrint(plist);
}
int main()
{
    TestList1();
    return 0;
}

运行结果:

2.8 查找

List.h:

LTNode* ListFind(LTNode* phead, LTDateType x);//查找

List.c:

LTNode* ListFind(LTNode* phead, LTDateType x)
{
    assert(phead);
    LTNode* cur = phead->next;
    while (cur != phead)
    {
        if (cur->data == x)
            return cur;
        cur = cur->next;
    }
    return NULL;
}

2.9 前插

List.h:

void ListInsert(LTNode* pos, LTDateType x);//前插

List.c:

void ListInsert(LTNode* pos, LTDateType x)
{
    assert(pos);
    LTNode* posPrev = pos->prev;
    LTNode* newnode = BuyListNode(x);
    posPrev->next = newnode;
    newnode->prev = posPrev;
    newnode->next = pos;
    pos->prev = newnode;
}

test.c

#include "List.h"
void TestList1()
{
    LTNode* plist = ListInit();
    ListPushBack(plist, 1);
    ListPushBack(plist, 2);
    ListPushBack(plist, 3);
    ListPrint(plist);
    ListPopBack(plist);
    ListPrint(plist);
    ListPushFront(plist, 10);
    ListPrint(plist);
    ListPopFront(plist);
    ListPrint(plist);
    LTNode* list = ListFind(plist, 2);
    ListInsert(plist->next->next, 20);
    ListPrint(plist);
}
int main()
{
    TestList1();
    return 0;
}

运行结果:

2.10 删除

List.h:

void ListErase(LTNode* pos);//删除

List.c:

void ListErase(LTNode* pos)
{
    assert(pos);
    LTNode* posPrev = pos->prev;
    LTNode* posNext = pos->next;
    posPrev->next = posNext;
    posNext->prev = posPrev;
    free(pos);
    pos = NULL;
}

test.c

#include "List.h"
void TestList1()
{
    LTNode* plist = ListInit();
    ListPushBack(plist, 1);
    ListPushBack(plist, 2);
    ListPushBack(plist, 3);
    ListPrint(plist);
    ListPopBack(plist);
    ListPrint(plist);
    ListPushFront(plist, 10);
    ListPrint(plist);
    ListPopFront(plist);
    ListPrint(plist);
    LTNode* list = ListFind(plist, 2);
    ListInsert(plist->next->next, 20);
    ListPrint(plist);
    ListErase(plist->next->next);
    ListPrint(plist);
}
int main()
{
    TestList1();
    return 0;
}

运行结果:

2.11 销毁

List.h:

void ListDestroy(LTNode* phead);//销毁

List.c:

void ListDestroy(LTNode* phead)
{
    assert(phead);
    LTNode* cur = phead->next;
    while (cur != phead);
    {
        LTNode* next = cur->next;
        free(cur);
        cur = next;
    }
    free(phead);//置空放在test.c里面,因为形参是实参的临时拷贝
}

3.完整代码展示

List.h:

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int LTDateType;
typedef struct ListNode
{
    LTDateType data;
    struct ListNode* next;
    struct ListNode* prev;
}LTNode;
LTNode* ListInit();//初始化
void ListPrint(LTNode* phead);//打印
LTNode* BuyListNode(LTDateType x);//申请结点 
void ListPushBack(LTNode* phead, LTDateType x);//尾插 
void ListPopBack(LTNode* phead);//尾删
void ListPushFront(LTNode* phead, LTDateType x);//头插
void ListPopFront(LTNode* phead);//头删
LTNode* ListFind(LTNode* phead, LTDateType x);//查找
void ListInsert(LTNode* pos, LTDateType x);//前插
void ListErase(LTNode* pos);//删除
void ListDestroy(LTNode* phead);//销毁

List.c:

#include "List.h"
LTNode* ListInit()
{
    //哨兵位头结点
    LTNode* phead = (LTNode*)malloc(sizeof(LTNode));
    phead->next = phead;
    phead->prev = phead;
    return phead;
}

void ListPrint(LTNode* phead)
{
    assert(phead);
    LTNode* cur = phead->next;
    while (cur != phead)
    {
        printf("%d ", cur->data);
        cur = cur->next;
    }
    printf("\n");
}

LTNode* BuyListNode(LTDateType x)
{
    LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
    newnode->data = x;
    newnode->next = NULL;
    newnode->prev = NULL;
    return newnode;
}

void ListPushBack(LTNode* phead, LTDateType x)
{
    assert(phead);
    LTNode* tail = phead->prev;
    LTNode* newnode = BuyListNode(x);
    tail->next = newnode;
    newnode->prev = tail;
    phead->prev = newnode;
    newnode->next = phead;
}

void ListPopBack(LTNode* phead)
{
    assert(phead);
    assert(phead->next != phead);//排除为空的情况
    LTNode* tail = phead->prev;
    phead->prev = tail->prev;
    tail->prev->next = phead;
    free(tail);
}

void ListPushFront(LTNode* phead, LTDateType x)
{
    assert(phead);
    LTNode* newnode = BuyListNode(x);
    LTNode* next = phead->next;
    phead->next = newnode;
    newnode->prev = phead;
    newnode->next = next;
    next->prev = newnode;
}

void ListPopFront(LTNode* phead)
{
    assert(phead);
    assert(phead->next != phead);
    LTNode* next = phead->next;
    LTNode* nextNext = next->next;
    phead->next = nextNext;
    nextNext->prev = phead;
    free(next);
}

LTNode* ListFind(LTNode* phead, LTDateType x)
{
    assert(phead);
    LTNode* cur = phead->next;
    while (cur != phead)
    {
        if (cur->data == x)
            return cur;
        cur = cur->next;
    }
    return NULL;
}

void ListInsert(LTNode* pos, LTDateType x)
{
    assert(pos);
    LTNode* posPrev = pos->prev;
    LTNode* newnode = BuyListNode(x);
    posPrev->next = newnode;
    newnode->prev = posPrev;
    newnode->next = pos;
    pos->prev = newnode;
}

void ListErase(LTNode* pos)
{
    assert(pos);
    LTNode* posPrev = pos->prev;
    LTNode* posNext = pos->next;
    posPrev->next = posNext;
    posNext->prev = posPrev;
    free(pos);
    pos = NULL;
}

void ListDestroy(LTNode* phead)
{
    assert(phead);
    LTNode* cur = phead->next;
    while (cur != phead);
    {
        LTNode* next = cur->next;
        free(cur);
        cur = next;
    }
    free(phead);//置空放在test.c里面,因为形参是实参的临时拷贝
}

test.c

#include "List.h"
void TestList1()
{
    LTNode* plist = ListInit();
    ListPushBack(plist, 1);
    ListPushBack(plist, 2);
    ListPushBack(plist, 3);
    ListPrint(plist);
    ListPopBack(plist);
    ListPrint(plist);
    ListPushFront(plist, 10);
    ListPrint(plist);
    ListPopFront(plist);
    ListPrint(plist);
    LTNode* list = ListFind(plist, 2);
    ListInsert(plist->next->next, 20);
    ListPrint(plist);
    ListErase(plist->next->next);
    ListPrint(plist);
}
int main()
{
    TestList1();
    return 0;
}

4.顺序表和链表的区别

不同点

顺序表

链表

存储空间上

物理上一定连续

逻辑上连续,但物理上不一定连

随机访问

支持O(1)

不支持:O(N)

任意位置插入或者删除元

可能需要搬移元素,效率低O(N)

只需修改指针指向

插入

动态顺序表,空间不够时需要扩

没有容量的概念

应用场景

元素高效存储+频繁访问

任意位置插入和删除频繁

缓存利用率

备注:缓存利用率参考存储体系结构以及局部原理性。

5.链表OJ题

5.1 环形链表(1)

给你一个链表的头节点 head ,判断链表中是否有环。如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。如果链表中存在环 ,则返回 true 。 否则,返回 false 。

链接:https://leetcode.cn/problems/linked-list-cycle/

bool hasCycle(struct ListNode *head) {
    struct ListNode* slow=head,*fast=head;
    while(fast&&fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
        if(slow==fast)
        {
            return true;
        }
    }
    return false;
}

5.2 环形链表(2)

给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。不允许修改链表。

链接:https://leetcode.cn/problems/linked-list-cycle-ii/description/

struct ListNode *detectCycle(struct ListNode *head) {
    struct ListNode* slow=head,*fast=head;
    while(fast&&fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
        if(slow==fast)
        {
            //相遇
            struct ListNode* meet=slow;
            //公式
            while(meet!=head)
            {
                meet=meet->next;
                head=head->next;
            }
            return meet;
        }
    }
    return NULL;
}

5.3 复杂链表的复制

给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。
构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。
例如,如果原链表中有 X 和 Y 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 x 和 y ,同样有 x.random --> y 。返回复制链表的头节点。用一个由 n 个节点组成的链表来表示输入/输出中的链表。
每个节点用一个 [val, random_index] 表示:
val:一个表示 Node.val 的整数。
random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为 null 。
你的代码 只 接受原链表的头节点 head 作为传入参数。

链接:https://leetcode.cn/problems/copy-list-with-random-pointer/description/

struct Node* copyRandomList(struct Node* head) {
    //1.拷贝结点插入原结点的后面
    struct Node* cur=head;
    while(cur)
    {
        struct Node* copy=(struct Node*)malloc(sizeof(struct Node));
        copy->val=cur->val;
        //插入copy结点
        copy->next=cur->next;
        cur->next=copy;
        cur=copy->next;
    }
    //2.根据原结点,处理copy结点的random
    cur=head;
    while(cur)
    {
        struct Node* copy=cur->next;
        if(cur->random==NULL)
        {
            copy->random=NULL;
        }
        else
        {
            copy->random=cur->random->next;
        }
        cur=copy->next;
    }
    struct Node* copyHead=NULL,*copyTail=NULL;
    cur=head;
    while(cur)
    {
        struct Node* copy=cur->next;
        struct Node* next=copy->next;
        if(copyTail==NULL)
        {
            copyHead=copyTail=copy;
        }
        else
        {
            copyTail->next=copy;
            copyTail=copy;
        }
        cur->next=next;
        cur=next;
    }
    return copyHead;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/165982.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

LeetCode 329. 矩阵中的最长递增路径(C++)*

思路&#xff1a; 1.用动态规划&#xff0c;但是时间复杂度太高&#xff0c;效率太低 2.使用常规的DFS&#xff0c;时间复杂度高&#xff0c;包含了太多重复无效遍历&#xff0c;会超时 3.在DFS的基础上使用记忆化搜索&#xff0c;帮助消去重复的遍历&#xff0c;提高效率 原题…

解决: 您目前无法访问 因为此网站使用了 HSTS。网络错误和攻击通常是暂时的,因此,此网页稍后可能会恢复正常

目录 问题描述 报错信息 问题原因 如何解决 参考资料 问题描述 您目前无法访问 因为此网站使用了 HSTS。网络错误和攻击通常是暂时的&#xff0c;因此&#xff0c;此网页稍后可能会恢复正常。 报错信息 今天使用Edge浏览器在访问一个平时常用的emoji网站时&#xff0c;…

springboot整合spring-security

在web开发中&#xff0c;安全性问题比较重要&#xff0c;一般会使用过滤器或者拦截器的方式对权限等进行验证过滤。此博客根据b站up主&#xff0c;使用demo示例进行展示spring-security的一些功能作用。 目 录 1、创建项目 2、编写controller 3、添加spring-security依赖 …

Spring Cloud OpenFeign 配置

最少的配置&#xff08;使用默认配置&#xff09; 最少/默认配置示例如下&#xff08;使用Nacos作为服务的注册与发现中心&#xff09;&#xff1a; application.properties server.port8082 spring.application.namenacos-consumer spring.cloud.nacos.discovery.server-ad…

[拆轮子] PaddleDetection中__shared__、__inject__ 和 from_config 三者分别做了什么

在上一篇中&#xff0c;PaddleDetection Register装饰器到底做了什么 https://blog.csdn.net/HaoZiHuang/article/details/128668393 已经介绍了 __shared__ 和 __inject__ 的作用: __inject__ 表示引入全局字典中已经封装好的模块。如loss等。__shared__为了实现一些参数的配…

excel函数技巧:函数TEXT七助数据大变身

如果函数有职业&#xff0c;那各函数的职业会是什么呢&#xff1f;别的先不说&#xff0c;就拿TEXT而言&#xff0c;它可以让日期变数字、数字变日期、阿拉伯数字变大写中文数字、金额元变万元&#xff0c;连IF的条件判断它也可以变出来…这简直就是当之无愧的变装女皇啊&#…

从0到1完成一个Node后端(express)项目(三、写接口、发起请求)

往期 从0到1完成一个Node后端&#xff08;express&#xff09;项目&#xff08;一、初始化项目、安装nodemon&#xff09; 从0到1完成一个Node后端&#xff08;express&#xff09;项目&#xff08;二、下载数据库、navicat、express连接数据库&#xff09; 写接口 我们看ex…

关于Linux部署Tomcat的访问问题

文章目录1.问题2.排除问题2.1检查Tomcat是否启动2.2检查防火墙&端口3.其他可能的问题3.1java的配置问题3.2可能出现了端口占用问题1.问题 在CentOS7系统的主机中配置好了Tomcat后发现通过默认端口无法访问到&#xff08;http://xx:xx:xx:xx:8080&#xff09; 2.排除问题 …

C语言在杨氏矩阵中找一个数

这道题大家都会做&#xff0c;使用暴力算法遍历整个数组。但是题目要求时间复杂度小于O&#xff08;n&#xff09;&#xff0c;这样做显然不合题意&#xff0c;所以&#xff0c;通过分析杨氏矩阵的特点&#xff0c;我们发现矩阵右上角的那个数为一行中最大的&#xff0c;一列中…

SAP MM 新建移动类型(Movement Type)

一、概念 物料的移动类型&#xff08;Movement Type&#xff09;代表了货物的移动&#xff0c;当一个物料做某种移动时&#xff0c;便开始了如下一系列事件&#xff1a; 1、一个物料凭证会被创建&#xff0c;可以被用来作为移动的证明及作为其它任何相关应用的一个信息来源&am…

Jetson nano 入手系列之6—使用qt creator 开发c++ opencv+CSI摄像头人脸检测

Jetson nano 入手系列之6—使用qt creator 开发c opencvCSI摄像头人脸检测1.创建摄像头人脸检测项目1.1 创建并配置项目1.2 编辑文件1.2.1 main.cpp1.2.2 CMakeLists.txt2.构建及编译2.1 直接使用qt creator完成2.2 使用命令行参考文献本系列针对亚博科技jetson nano开发板。 …

一篇文章带你学会MySQL数据库的基本管理

目录 前言 一、数据库的介绍 二、mariadb的安装 三、数据库的开启及安全初始化 四、数据库的基本管理 五、数据库密码更改及破解 六、用户授权 七、数据库的备份 八、phpmyadmin的安装 总结 前言 什么是数据库&#xff1f; 每个人家里都会有衣柜&#xff0c;衣柜是…

前端效果积累 | 酷炫、实用3D地球路径飞行效果实现

&#x1f4cc;个人主页&#xff1a;个人主页 ​&#x1f9c0; 推荐专栏&#xff1a;前端开发成神之路 --【这是一个为想要入门和进阶前端开发专门开启的精品专栏&#xff01;从个人到商业的全套开发教程&#xff0c;实打实的干货分享&#xff0c;确定不来看看&#xff1f; &…

【C语言进阶】自定义类型之结构体

目录一&#xff1a;结构体1.1&#xff1a;结构的基础知识&#xff1a; 1.2&#xff1a;结构的声明&#xff1a; 1.3&#xff1a;特殊声明&#xff08;匿名结构体&#xff09;&#xff1a; 1.4&#xff1a;结构的自引用&#xff1a; 1.5&#xff1a;结构体变量的定义和初始化&am…

springboot 项目自定义log日志文件提示系统找不到指定的文件

自己尝试搭建了一个springboot项目&#xff0c;自定义了log日志文件&#xff0c;启动后报错 Logging system failed to initialize using configuration from logback-spring.xml java.io.FileNotFoundException: E:\code_demo\xxxx\logback-spring.xml (系统找不到指定的文件…

Elasticsearch(二)--Elasticsearch客户端讲解

一、前言 在上一章我们大致了解了下elasticsearch,虽说上次的内容全是八股文&#xff0c;但是很多东西还是非常有用的&#xff0c;这些哪怕往小说作为面试&#xff0c;往大说是可以帮你很快的理解es是个什么玩意儿&#xff0c;所以还是非常推荐大家去看一下上一章内容。 这一章…

【C++】map和set的使用

​&#x1f320; 作者&#xff1a;阿亮joy. &#x1f386;专栏&#xff1a;《吃透西嘎嘎》 &#x1f387; 座右铭&#xff1a;每个优秀的人都有一段沉默的时光&#xff0c;那段时光是付出了很多努力却得不到结果的日子&#xff0c;我们把它叫做扎根 目录&#x1f449;关联式容…

码二哥的技术专栏 总入口

已发表的技术专栏&#xff08;订阅即可观看所有专栏&#xff09; 0  grpc-go、protobuf、multus-cni 技术专栏 总入口 1  grpc-go 源码剖析与实战  文章目录 2  Protobuf介绍与实战 图文专栏  文章目录 3  multus-cni   文章目录(k8s多网络实现方案) 4  gr…

JVM整理笔记之测试工具JCStress的使用及其注解的应用

文章目录前言如何使用JCStress测试代码JCStress 注解说明前言 如果要研究高并发&#xff0c;一般会借助高并发工具来进行测试。JCStress&#xff08;Java Concurrency Stress&#xff09;它是OpenJDK中的一个高并发测试工具&#xff0c;它可以帮助我们研究在高并发场景下JVM&a…

RecyclerView 倒计时和正计时方案

本章内容一.方案制定二.设计三.编码相信不少同学都会在这里栽跟头&#xff0c;在思考这个问题设计了两套方案&#xff0c;而我的项目需求中需要根据业务是否反馈来进行倒计时和正计时的操作。一.方案制定 1.在Adapter中使用CountDownTimer 2.修改数据源更新数据 3.只修改页面展…