数据结构day05(单链表)

今日任务:

 思维导图:

实现 代码:(多文件)

head.h

#ifndef __HEAD_H__
#define __HEAD_H__#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int datatype;typedef struct Linklist
{union {int len;datatype data;};struct Linklist* next;
}Node,*NodeP;
NodeP head_create();
NodeP create();
int output(NodeP head);
int tail_insert(NodeP head,datatype data);
int head_insert(NodeP head,datatype data);
int tail_delete(NodeP head);
int head_delete(NodeP head);
int pos_insert(NodeP head,datatype data,int pos);
int pos_delete(NodeP head,int pos);
int pos_update(NodeP head,datatype data,int pos);
int value_index(NodeP head,datatype data);
int value_delete(NodeP head,datatype data);
int inversion(NodeP head);
int free_linklist(NodeP head);
#endif

fun.c

#include "head.h"
/** function:   传参 空指针判定* @param [ in] * @param [out] * @return      */
int void_point(NodeP p){if(NULL==p){puts("how dare you give me null point.");return -1;}return 0;
}
/** function:    头结点创建* @param [ in] * @param [out] * @return      */
NodeP head_create(){NodeP head=(NodeP)malloc(sizeof(Node));if(head==NULL){puts("how dare you give me null place.");return NULL;}head->len=0;head->next=NULL;return head;
}
/** function:    节点创建* @param [ in] * @param [out] * @return      */
NodeP create(datatype data){NodeP new=(NodeP)malloc(sizeof(Node));if(new==NULL){puts("how dare you give me null place.");return NULL;}new->data=data;new->next=NULL;return new;
}
/** function:    输出* @param [ in] * @param [out] * @return      */
int output(NodeP head){if(void_point(head))return -1;while(head->next!=NULL){printf("%d\t",head->next->data);head=head->next;}puts("output done.");
}
/** function:    节点尾插* @param [ in] * @param [out] * @return      */
int tail_insert(NodeP head,datatype data){if(void_point(head))return -1;//找到尾部节点NodeP p=head;while(p->next!=NULL)p=p->next;p->next=create(data);head->len++;puts("tail insert success.");return 0;
}
/** function:    节点头插* @param [ in] * @param [out] * @return      */
int head_insert(NodeP head,datatype data){if(void_point(head))return -1;NodeP new=create(data);new->next=head->next;head->next=new;head->len++;puts("head insert success");
}
/** function:    尾删* @param [ in] * @param [out] * @return      */
int tail_delete(NodeP head){if(void_point(head))return -1;if(head->next==NULL){puts("there is no assigment to delete.");return -1;}//找到最后一个节点,free并len--,前一节点指向nullNodeP p=head;while(p->next->next!=NULL)p=p->next;free(p->next);p->next=NULL;head->len--;puts("tail delete success");return 0;
}
/** function:    头删* @param [ in] * @param [out] * @return      */
int head_delete(NodeP head){if(void_point(head))return -1;if(head->next==NULL){puts("there is no assigment to delete.");return -1;}NodeP p=head->next;free(head->next);head->next=p->next;p=NULL;head->len--;puts("head delete success");return 0;
}
/** function:    指定位置添加* @param [ in] * @param [out] * @return      */
int pos_insert(NodeP head,datatype data,int pos){if(void_point(head))return -1;if(pos>head->len+1||pos<1){puts("your position is illegal.");return -1;}NodeP p=head;while(pos--!=1)p=p->next;NodeP new=create(data);new->next=p->next;p->next=new;head->len++;puts("pos insert success");
}
/** function:    指定位置删除* @param [ in] * @param [out] * @return      */
int pos_delete(NodeP head,int pos){if(void_point(head))return -1;if(pos<1||pos>head->len){puts("your position is illegal.");return -1;}NodeP p=head;while(pos--!=1)p=p->next;NodeP x=p->next;p->next=p->next->next;free(x);x=NULL;head->len--;puts("pos delete success");return 0;
}
/** function:    指定位置修改* @param [ in] * @param [out] * @return      */
int pos_update(NodeP head,datatype data,int pos){if(void_point(head))return -1;if(pos<1||pos>head->len){puts("your position is illegal.");return -1;}NodeP p=head;while(pos--)p=p->next;p->data=data;puts("pos update success");return 0;
}
/** function:    按值查找下表* @param [ in] * @param [out] * @return      */
int value_index(NodeP head,datatype data){if(void_point(head))return -1;if(head->len==0){puts("linklist is null");return -1;}int index=0;NodeP p=head;while(p->next!=NULL){p=p->next;index++;if(p->data==data){printf("the index your want to find is%d\n",index);return index;}}puts("can't find your value");return 0;
}
/** function:    按值删除* @param [ in] * @param [out] * @return      */
int value_delete(NodeP head,datatype data){if(void_point(head))return -1;if(head->len==0){puts("linklist is null");return -1;}NodeP p=head;while(p->next!=NULL){if(p->next->data==data){pos_delete(head,value_index(head,data));puts("value delete success");return 0;}p=p->next;}puts("no value your want to delete");
}
/** function:    循环逆置* @param [ in] * @param [out] * @return      */
int inversion(NodeP head){if(void_point(head))return -1;if(head->len==0){puts("linklist is NULL");return -1;}if(head->len==1){puts("there is no deed");return 0;}//逆置//将第二个节点作为尾节点,但是得先记录一下,还得用于每次循环的头插的第一个元素NodeP p=head->next->next;head->next->next=NULL;//定义一个节点,用于方便循环调用后面的元素头插,NodeP k=p;puts("debug..");output(p);while(p!=NULL){p=p->next;k->next=head->next;head->next=k;k=p;}puts("inversion success.");return 0;
}
/** function:    释放链表* @param [ in] * @param [out] * @return      */
int free_linklist(NodeP head){if(void_point(head))return -1;NodeP p=NULL;while(head!=NULL){p=head;head=head->next;free(p);p=NULL;}puts("free success.");return 0;
}

main.c

#include "head.h"
int main(int argc, const char *argv[])
{NodeP p=head_create();tail_insert(p,10);tail_insert(p,20);tail_insert(p,30);tail_insert(p,40);tail_insert(p,50);head_insert(p,99);head_insert(p,88);output(p);//pos_insert(p,66,0);//output(p);//pos_delete(p,8);//output(p);//pos_update(p,66,8);//output(p);//tail_delete(p);//output(p);//head_delete(p);//output(p);//value_delete(p,77);//output(p);//inversion(p);//output(p);free_linklist(p);p=NULL;return 0;
}

不好,眼花了,没看到实现单项循环链表,ji

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

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

相关文章

win10+QT5.15+cryptopp562 完整配置开发

1、准备如下几项内容&#xff1a; a、WIN10环境下的QT5.15.2安装包&#xff0c;QTCreator对应版本安装。&#xff08;自行安装&#xff09; b、cryptopp562安装包下载&#xff0c;官网&#xff1a;https://www.cryptopp.com/&#xff0c;这里没选择最新的8.7是因为mingw-32编译…

Viva Workplace Analytics Employee Feedback SU Viva Glint部署方案

目录 一、Viva Workplace Analytics & Employee Feedback SU Viva Glint介绍 二、Viva Glint和Viva Pulse特点和优势 1. 简单易用

在R中安装TensorFlow、TensorFlow_Probability、numpy(R与Python系列第二篇)

目录 前言&#xff1a; 1-安装tensorflow库 Step1: 下载R包tensorflow Step2&#xff1a;安装TensorFlow库 Step3&#xff1a;导入R中 2-安装tensorflow_probability库 Step1&#xff1a;下载R包&#xff1a;tfprobability Step2&#xff1a;安装TensorFlow Probability …

【CSS】em单位的理解

1、em单位的定义 MDN的解释&#xff1a;它是相对于父元素的字体大小的一个单位。 例如&#xff1a;父元素font-size&#xff1a;16px&#xff1b;子元素的font-size&#xff1a;2em&#xff08;也就是32px&#xff09; 注&#xff1a;有一个误区&#xff0c;虽然他是一个相对…

代码随想录day25

216.组合总和III ● 力扣题目链接 ● 找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数&#xff0c;并且每种组合中不存在重复的数字。 思路 ● 和上题差不多&#xff0c;只不过多了一个条件限制 ● 时间复杂度&#xff1a;O(n * 2^n) 代码 class …

HTTP Get 和 Post 的区别

分析&回答 使用规范 根据HTTP规范&#xff0c;GET用于信息获取&#xff0c;而且应该是安全的和幂等的。 根据HTTP规范&#xff0c;POST表示可能修改变服务器上的资源的请求。 传递参数 GET请求的数据会附在URL之后&#xff08;就是把数据放置在HTTP协议头中&#xff09;。…

基于Kohonen网络的聚类算法

1.案例背景 1.1 Kohonen网络 Kohonen网络是自组织竞争型神经网络的一种,该网络为无监督学习网络,能够识别环境特征并自动聚类。Kohonen神经网络是芬兰赫尔辛基大学教授Teuvo Kohonen 提出的,该网络通过自组织特征映射调整网络权值,使神经网络收敛于一种表示形态。在这一形态中…

EG1164大功率同步整流升压模块开源,最高效率97%

EG1164大功率同步整流Boost升压电源模块&#xff0c;最高效率97%&#xff0c;输入电压8~50V&#xff0c;输出电压8~60V可调&#xff0c;最大功率300瓦以上&#xff0c;开关频率219kHz。 白嫖了张嘉立创的彩色丝印券就随便画了个板试试&#xff0c;第一次打彩色丝印。 因为我测…

Mongodb常见操作命令

一、登录相关以及启动 启动服务mongodb&#xff1a; cd /usr/local/mongodb/bin ./mongod -f /data/mongodb/mongodb1.conf./mongod -f /data/mongodb/mongodb2.conf./mongod -f /data/mongodb/mongodb3.conf 登录mongodb数据库&#xff08;mongodb默认端口:27017&#xff0…

ORA-00600之数据库内部BUG 22114696

ORA-00600之数据库内部BUG 22114696 错误信息Bug信息应对办法 错误信息 应用包运行时收到报错信息如下&#xff1a; ORA-00600: internal error code, arguments: [4450],[kpotx.c],[2866],[],… ORA-02063: preceding line from DW_JOB_PROD line ORA-06512: line at "…

如何飞速成为开源贡献者(Contributor)

如何飞速成为开源贡献者Contributor 一、环境信息1.1 硬件信息1.2 软件信息 二、Git安装2.1 Git介绍2.2 Git下载安装 三、开源项目选定四、GitHub参与开源流程4.1 Fork项目4.2 SSH配置4.2.1 为什么要配置SSH4.2.2 如何配置SSH 4.3 Clone项目4.4 IDEA关联4.5 PR生成4.6 PR提交 一…

Navicat16安装教程

注&#xff1a;因版权原因&#xff0c;本文已去除破解相关的文件和内容 1、在本站下载解压后即可获得Navicat16安装包和破解补丁&#xff0c;如图所示 2、双击“navicat160_premium_cs_x64.exe”程序&#xff0c;即可进入安装界面&#xff0c; 3、点击下一步 4、如图所示勾选“…

Java 中数据结构HashMap的用法

Java HashMap HashMap 是一个散列表&#xff0c;它存储的内容是键值对(key-value)映射。 HashMap 实现了 Map 接口&#xff0c;根据键的 HashCode 值存储数据&#xff0c;具有很快的访问速度&#xff0c;最多允许一条记录的键为 null&#xff0c;不支持线程同步。 HashMap 是…

防雷接地+防雷检测综合应用解决方案

防雷接地和防雷检测是防雷工程中的重要内容&#xff0c;它们旨在保护建筑物和设备免受雷电的危害。地凯科技将介绍防雷接地和防雷检测的基本原理、施工案例方案和国标措施。 防雷接地是指将建筑物的金属结构、防雷装置和电气设备与地面连接&#xff0c;形成一个接地系统&#…

一些指针的练习题

一、指针练习 1.1 声明一个整型变量和一个指向整型的指针变量&#xff0c;将指针指向该整型变量&#xff0c;然后通过指针修改变量的值。 #include <stdio.h> int main() {int a 0; int* p &a;// *p 20; // 修改指针变量的值 // printf("a 1 %d", …

安装Ubuntu系统,将U盘当作启动盘后写保护怎么回复?

下载ChipGenius 插入写保护的U盘&#xff0c;打开ChipGenius.exe后可以扫描到U盘&#xff0c;如下图中的E:盘就是我插入的U盘&#xff08;我的PC上只有C、D两个分区&#xff09;&#xff1b; ChipGenius的作用 下载ChipGenius是为了获取U盘的设备信息&#xff1a;重点是主控…

数据集学习笔记(七):不同任务数据集的标签介绍(包含目标检测、图像分割、行为分析)

文章目录 一、目标检测1.1 TXT1.2 COCO1.3 XML 二、图像分割2.1 json2.1 TXT2.1.1 json转txt 三、行为分析3.1 TXT3.2 JSON 一、目标检测 1.1 TXT 每行表示&#xff08;类别&#xff0c;中心x相对坐标&#xff0c;中心y相对坐标&#xff0c;相对宽度、相对高度&#xff09; 1…

LeetCode494. 目标和

494. 目标和 文章目录 [494. 目标和](https://leetcode.cn/problems/target-sum/)一、题目二、题解方法一&#xff1a;目标和路径计数算法方法二&#xff1a;01背包方法三&#xff1a;01背包一维数组 一、题目 给你一个非负整数数组 nums 和一个整数 target 。 向数组中的每个…

语言基础篇7——Python运算符

运算符 算数运算符 # 算术运算符 # # - # * # ** # / 浮点除 # // 整数除 # %赋值运算符 # 赋值运算符 # # # - # / 浮点除等 # // 整数除等 # * # **关系运算符 # 关系运算符 # > # # < # > # < # ! # is # is not逻辑运算符 # 逻辑运算符 # 返回表达式…

Golang网络编程

Golang网络编程 网络编程简介网络编程协议网络分层模型TCP/IP协议什么是DNS套接字&#xff08;Socket&#xff09;客户端服务器模型TCP/UDP的区别HTTP协议会话sessionCookiehttpsHTTP请求格式HTTP响应格式http头信息http请求头信息http响应头信息HTTP状态码http内容类型和内容…