数据结构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.hqwc.cn/news/94107.html

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

相关文章

java八股文面试[数据库]——MySQL索引的数据结构

知识点&#xff1a; 【2023年面试】mysql索引的基本原理_哔哩哔哩_bilibili 【2023年面试】mysql索引结构有哪些&#xff0c;各自的优劣是什么_哔哩哔哩_bilibili

SIEM中的安全事件管理

组织经常面临意外和未知的安全威胁&#xff0c;无论威胁的级别、类型或大小如何&#xff0c;它们的存在都会对企业的整体运作产生冲击&#xff0c;事件管理是尽快识别和响应这些中断以最大程度地减少其对日常业务运营的影响的过程。 什么是安全事件 安全事件是指示对组织网络…

Linux中创建文件夹,删除文件夹

Linux中创建目录&#xff1a;mkdir 文件夹&#xff0c; 比如&#xff1a;mkdir test 删除文件夹&#xff1a;rm -rf 文件夹&#xff0c; 比如&#xff1a;rm -rf soft vi强制不保存退出命令&#xff1a;q&#xff01;

python“魂牵”京东商品历史价格数据接口(含代码示例)

要通过京东的API获取商品详情历史价格数据&#xff0c;您可以使用京东开放平台提供的接口来实现。以下是一种使用Java编程语言实现的示例&#xff0c;展示如何通过京东开放平台API获取商品详情历史价格数据&#xff1a; 首先&#xff0c;确保您已注册成为京东开放平台的开发者…

1、Nginx 简介

文章目录 1、Nginx 简介1.1 Nginx 概述1.2 Nginx 作为 web 服务器1.3 正向代理1.4 反向代理1.5 负载均衡1.6 动静分离 【尚硅谷】尚硅谷Nginx教程由浅入深 志不强者智不达&#xff1b;言不信者行不果。 1、Nginx 简介 1.1 Nginx 概述 Nginx (“engine x”) 是一个高性能的 HT…

164到网络安全面试大全(附答案)

最近有不少小伙伴跑来咨询&#xff1a; 想找网络安全工作&#xff0c;应该要怎么进行技术面试准备&#xff1f;工作不到 2 年&#xff0c;想跳槽看下机会&#xff0c;有没有相关的面试题呢&#xff1f; 为了更好地帮助大家高薪就业&#xff0c;今天就给大家分享两份网络安全工…

记1次前端性能优化之CPU使用率

碰到这样的一个问题&#xff0c;用户反馈页面的图表一直加载不出来&#xff0c;页面还卡死 打开链接页面&#xff0c;打开控制台 Network 看到有个请求一直pending&#xff0c;结合用户描述&#xff0c;页面一直loading,似乎验证了我的怀疑&#xff1a;后端迟迟没有相应。 但是…

Git的基本使用笔记——狂神说

版本控制 版本迭代&#xff0c; 版本控制( Revision control)是一种在开发的过程中用于管理我们对文件、目录或工程等内容的修改历史&#xff0c;方便查看更改历史记录&#xff0c;备份以便恢复以前的版本的软件工程技术。 实现跨区域多人协同开发 追踪和记载一个或者多个文件的…

2023年高教社杯数学建模思路 - 案例:异常检测

文章目录 赛题思路一、简介 -- 关于异常检测异常检测监督学习 二、异常检测算法2. 箱线图分析3. 基于距离/密度4. 基于划分思想 建模资料 赛题思路 &#xff08;赛题出来以后第一时间在CSDN分享&#xff09; https://blog.csdn.net/dc_sinor?typeblog 一、简介 – 关于异常…

抖音小程序开发教学系列(1)- 抖音小程序简介

章节一&#xff1a;抖音小程序简介 1.1 抖音小程序的背景和概述 抖音小程序的发展背景和市场趋势&#xff1a; 抖音作为一款热门的短视频社交平台&#xff0c;用户群体庞大&#xff0c;社交共享的特性也为小程序的发展提供了广阔的空间。抖音小程序作为抖音在社交和用户粘性…

C++——shared_ptr:make_shared的用处,与shared_ptr直接构造的区别

shared_ptr shared_ptr继承自__shared_ptr&#xff0c;其中有两个对象&#xff0c;一个是指向资源的指针&#xff0c;一个是控制块&#xff0c;指向一个引用计数对象。控制块中存储了强引用和弱引用的计数&#xff0c;强引用Uses代表shared_ptr对象的引用计数&#xff0c;弱引…

OLED透明屏显示技术:未来显示科技的领航者

OLED透明屏显示技术是一种创新性的显示技术&#xff0c;它的特殊性质使其成为未来显示科技的领航者。 OLED透明屏具有高对比度、快速响应时间、广视角和低功耗等优势&#xff0c;同时&#xff0c;其透明度、柔性和薄型设计使其成为创新设计的理想选择。 本文将深入探讨OLED透…