循环链表和双向链表

下面是 C 语言中循环链表和双链表的链式表示和实现示例:

 

**循环链接表:**

 

'''c

 

#include <stdio.h>

#include <stdlib.h>

 

struct Node {

    int data;

    struct Node* next;

};

 

// Function to create a new node

struct Node* createNode(int data) {

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->data = data;

    newNode->next = NULL;

    return newNode;

}

 

// Function to insert an element at the beginning of the circular linked list

struct Node* insertAtBeginning(struct Node* head, int data) {

    struct Node* newNode = createNode(data);

 

    if (head == NULL) {

        newNode->next = newNode;

        return newNode;

    }

 

    newNode->next = head->next;

    head->next = newNode;

    return head;

}

 

// Function to insert an element at the end of the circular linked list

struct Node* insertAtEnd(struct Node* head, int data) {

    struct Node* newNode = createNode(data);

 

    if (head == NULL) {

        newNode->next = newNode;

        return newNode;

    }

 

    newNode->next = head->next;

    head->next = newNode;

    return newNode;

}

 

// Function to display the elements of the circular linked list

void displayList(struct Node* head) {

    if (head == NULL) {

        return;

    }

 

    struct Node* current = head->next;

 

    do {

        printf("%d ", current->data);

        current = current->next;

    } while (current != head->next);

 

    printf("\n");

}

 

int main() {

    struct Node* head = NULL;

 

    head = insertAtBeginning(head, 3);

    head = insertAtEnd(head, 5);

    head = insertAtEnd(head, 7);

 

    displayList(head);

 

    return 0;

}

 

'''

 

在此实现中,通过将最后一个节点的“下一个”指针设置为指向头节点来创建循环链表。“createNode”函数使用给定数据创建一个新节点。“insertAtBeginning”函数通过更新新节点的“next”指针以指向当前头节点,并将头节点的“next”指针更新为指向新节点,在列表的开头插入新节点。“insertAtEnd”函数通过将新节点的“next”指针更新为指向头节点的“next”指针,并将头节点的“next”指针更新为指向新节点,在列表末尾插入新节点。'displayList' 函数遍历循环链表并打印每个节点的数据,直到它再次到达起点。

 

**双链表:**

 

'''c

 

#include <stdio.h>

#include <stdlib.h>

 

struct Node {

    int data;

    struct Node* prev;

    struct Node* next;

};

 

// Function to create a new node

struct Node* createNode(int data) {

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->data = data;

    newNode->prev = NULL;

    newNode->next = NULL;

    return newNode;

}

 

// Function to insert an element at the beginning of the doubly linked list

struct Node* insertAtBeginning(struct Node* head, int data) {

    struct Node* newNode = createNode(data);

 

    if (head == NULL) {

        return newNode;

    }

 

    newNode->next = head;

    head->prev = newNode;

    return newNode;

}

 

// Function to insert an element at the end of the doubly linked list

struct Node* insertAtEnd(struct Node* head, int data) {

    struct Node* newNode = createNode(data);

 

    if (head == NULL) {

        return newNode;

    }

 

    struct Node* current = head;

    while (current->next != NULL){

        current = current->next;

    }

 

    current->next = newNode;

    newNode->prev = current;

    return head;

}

 

// Function to display the elements of the doubly linked list in forward direction

void displayForward(struct Node* head) {

    struct Node* current = head;

 

    while (current != NULL) {

        printf("%d ", current->data);

        current = current->next;

    }

 

    printf("\n");

}

 

// Function to display the elements of the doubly linked list in reverse direction

void displayReverse(struct Node* head) {

    struct Node* current = head;

 

    while (current->next != NULL) {

        current = current->next;

    }

 

    while (current != NULL) {

        printf("%d ", current->data);

        current = current->prev;

    }

 

    printf("\n");

}

 

int main() {

    struct Node* head = NULL;

 

    head = insertAt this point, the response was cut off. Apologies for the incomplete answer. Let me continue with the remaining code for the doubly linked list implementation:

 

```c

    head = insertAtBeginning(head, 3);

    head = insertAtEnd(head, 5);

    head = insertAtEnd(head, 7);

 

    displayForward(head);

    displayReverse(head);

 

    return 0;

}

 

'''

 

在此实现中,双向链表的每个节点都包含一个“prev”指针和一个“next”指针,它们分别指向上一个和下一个节点。“createNode”函数使用给定数据创建一个新节点,并初始化指向“NULL”的“prev”和“next”指针。“insertAtBeginning”函数通过更新新节点的“next”指针以指向当前头节点,并将头节点的“prev”指针更新为指向新节点,在列表的开头插入新节点。“insertAtEnd”函数通过遍历列表直到到达最后一个节点,然后更新最后一个节点的“next”指针以指向新节点,并将新节点的“prev”指针更新为指向最后一个节点,从而在列表末尾插入一个新节点。'displayForward' 函数向前(从头到尾)遍历双向链表并打印每个节点的数据。'displayReverse' 函数以相反的方向(从尾部到正面)遍历双向链表并打印每个节点的数据。

 

在“main”函数中,我们创建一个空的双向链表,然后使用“insertAtBeginning”和“insertAtEnd”函数在列表的开头和结尾插入元素。最后,我们使用 'displayForward' 和 'displayReverse' 函数以正向和反向打印列表的元素。可以根据对循环链表和双向链表实现的要求修改和扩展此代码。

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

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

相关文章

Mysql与Redis如何保证数据一致性问题

目录 一、Mysql与Redis同步数据是否存在延迟呢&#xff1f; 二、如何保证一致性&#xff1f; 2.1、第一种方式&#xff1a;手动编码 2.2、第二种方式&#xff1a;MQ异步更新 2.3、第三种方式&#xff1a;binlog同步数据 2.4、第四种方式&#xff1a;双写一致性 2.5、第五…

ASP.NET可视化流程设计器源码

源码介绍: ASP.NET可视化流程设计器源码已应用于众多大型企事业单位。拥有全浏览器兼容的可视化流程设计器、表单设计器、基于角色的权限管理等系统开发必须功能&#xff0c;大大为您节省开发时间&#xff0c;是您开发OA.CRM、HR等企事业各种应用管理系统和工作流系统的最佳基…

Python爬虫获取百度的图片

一. 爬虫的方式&#xff1a; 主要有2种方式: ①ScrapyXpath (API 静态 爬取-直接post get) ②seleniumXpath (点击 动态 爬取-模拟) ScrapyXpath XPath 是 Scrapy 中常用的一种解析器&#xff0c;可以帮助爬虫定位和提取 HTML 或 XML 文档中的数据。 Scrapy 中使用 …

Intel x86架构之I/O APIC

全文来自Intel手册&#xff08;见参考1&#xff09;&#xff1a;Intel? 82093AA I/O Advanced Programmable Interrupt Controller (I/O APIC) Datasheet 注意&#xff1a;下文中已经指出手册中的对应页面和章节&#xff0c;请对照手册原文看&#xff0c;任何个人理解错误&…

期货日数据维护与使用_日数据维护_模块整体代码

目录 写在前面 setting.py sqlite_tool.py future_widget.py 写在前面 本文默认已经创建了项目&#xff0c;如果不知道如何创建一个空项目的&#xff0c;请参看以下两篇博文 PyQt5将项目搬到一个新的虚拟环境中 https://blog.csdn.net/m0_37967652/article/details/122…

tcp和udp的区别(附java实现)

TCP和UDP的区别 TCP&#xff08;Transmission Control Protocol&#xff09;和UDP&#xff08;User Datagram Protocol&#xff09;是两种不同的网络传输协议&#xff0c;它们在数据传输时有一些重要的区别。 TCP TCP是面向连接的协议&#xff0c;它在通信之前需要建立连接&…

InternLM第2节课笔记

轻松玩转书生浦语大模型趣味Demo InternLM模型全链条开源 InternLM-7B和InternLM-20B Lagent&#xff1a;智能体&#xff08;agent&#xff09;框架 浦语灵笔&#xff1a;InternLM-Xcomposer-7B 视觉-语言大模型 模型下载 Hugging Face huggingface-cli OpenXLab python…

【C++】- 类和对象(!!C++类基本概念!this指针详解)

类和对象 引入类类的定义类的访问限定操作符类的作用域类的实例化类对象模型this指针 引入类 在 C中&#xff0c;引入了一个新的定义----------类。类是一种用户自定义的数据类型&#xff0c;用于封装数据和行为。类可以看作是一个模板或蓝图&#xff0c;描述了一组相关的数据和…

第15课 利用openCV实现人脸识别

这节课&#xff0c;我们再来看一个简单且实用的例子&#xff1a;人脸识别。这个小例子可以让你进一步领略openCV的强悍。 1.复制demo14并改名为demo15。 2.修改capImg函数&#xff1a; int fmle::capImg() {// 加载人脸检测分类器cv::CascadeClassifier faceCascade;faceCas…

UEditor在编辑对齐方式时产生额外空行问题

一、问题描述 一个关于UEditor富文本编辑器的问题&#xff1a;在编辑内容对齐方式后保存后浏览器显示的段落上下会比原先多出一些间距。 下面是对齐编辑后&#xff0c;未保存前的的HTML&#xff1a; 保存后&#xff0c;实际会多出一个段落空行&#xff1a; 二、问题调查 经…

基于B/S架构的数字孪生智慧监所可视化监管系统

1 前言 物联网技术的发展使云计算技术得到了迅猛的发展及广泛的应用&#xff0c;智能体系的创建已经成为监狱发展的必然趋势。 智慧监狱的创建、智能化管理的推行是监狱管理的创新&#xff0c;也是监狱整体工作水平提升的具体体现。 1.1 建设背景 近年来&#xff0c;司法部不…

CISSP 第7章:PKI和密码学应用

第七章 PKI和密码学应用 7.1 非对称密码学 对称密码系统具有共享的秘钥系统&#xff0c;从而产生了安全秘钥分发的问题 非对称密码学使用公钥和私钥对&#xff0c;无需支出复杂密码分发系统 7.1.1 公钥与私钥 7.1.2 RSA&#xff08;兼具加密和数字签名&#xff09; RSA算法依赖…

ctrl + v获取图片和文字

1、效果实现 1.1、做法 容器监听paste事件。原生js则document.addEventListener(paste)&#xff0c;vue则paste 监听paste事件的回调函数有个参数e&#xff0c;获取e.clipboardData粘贴的文字信息 e.clipboardData.getData("text/plain")粘贴的图片信息 e.clipboard…

委托QAbstractItemDelegate

参考&#xff1a;QT(7)-初识委托_qt 委托-CSDN博客 一、 1、 模型&#xff1a;负责“组织”数据&#xff1b; 视图&#xff1a;负责“显示”数据&#xff1b; 委托&#xff1a;负责“修改”数据&#xff1b; 2、委托&#xff1a;在QT的MV模型中&#xff0c;处理特定类型的…

c++拷贝控制

文章目录 拷贝构造函数的基本概念定义语法何时使用拷贝构造函数示例代码运行结果注意事项 拷贝赋值运算符的基本概念定义语法何时使用拷贝赋值运算符示例代码运行结果注意事项 析构函数的基本概念定义语法何时调用析构函数示例代码运行结果注意事项 三/五法则三法则 (Rule of T…

【SpringCloud】之配置中心(进阶使用)

&#x1f389;&#x1f389;欢迎来到我的CSDN主页&#xff01;&#x1f389;&#x1f389; &#x1f3c5;我是君易--鑨&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f31f;推荐给大家我的博客专栏《SpringCloud开发之远程消费》。&#x1f3af;&a…

精彩推荐 |【Java技术专题】「重塑技术功底」攻破Java技术盲点之剖析动态代理的实现原理和开发指南(上)

攻破Java技术盲点之剖析动态代理的实现原理和开发指南 背景介绍静态代理和动态代理动态代理与静态代理的区别 进入正题重温&#xff1a;静态代理实现静态代理案例静态代理的弊端 重温&#xff1a;动态代理Java动态代理InvocationHandlerJava动态代理的实现下面看具体的代码实例…

Blazor项目如何调用js文件

以下是来自千问的回答并加以整理&#xff1a;&#xff08;说一句&#xff0c;文心3.5所给的回答不完善&#xff0c;根本运行不起来&#xff0c;4.0等有钱了试试&#xff09; 在Blazor项目中引用JavaScript文件&#xff08;.js&#xff09;以实现与JavaScript的互操作&#xff…

基于JAVA的服装店库存管理系统 开源项目

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 数据中心模块2.2 角色管理模块2.3 服装档案模块2.4 服装入库模块2.5 服装出库模块 三、系统设计3.1 用例设计3.2 数据库设计3.2.1 角色表3.2.2 服装档案表3.2.3 服装入库表3.2.4 服装出库表 四、系统展示五、核心代码5.…

Java 11中的新字符串APIs详解

第1章 引言 大家好&#xff0c;我是小黑&#xff0c;咱们都知道&#xff0c;Java作为一种广泛使用的编程语言&#xff0c;每一次更新都会带来不少新鲜事物。而Java 11&#xff0c;作为长期支持&#xff08;LTS&#xff09;版本之一&#xff0c;更是引起了广大开发者的关注。好…