当前位置: 首页 > news >正文

数据结构 / day04 作业

1. 单链表任意位置删除, 单链表任意位置修改, 单链表任意位置查找, 单链表任意元素查找, 单链表任意元素修改, 单链表任意元素删除, 单链表逆置

// main.c#include "head.h"int main(int argc, const char *argv[])
{Linklist head=NULL; //head 是头指针// printf("head=%p\n", head);// printf("head->data=%d\n", head->data);// printf("head->next=%p\n", head->next);int n;int pos;int key;data_type element;printf("please input n:");scanf("%d", &n);for(int i=0; i<n; i++){printf("please input No.%d element: ", i+1);scanf("%d", &element);head=insert_head(element, head);}puts("---ouput Linklist---");output(head);/*puts("---get tail node---");Linklist node_tail = get_tail_node(head);printf("tail->data=%d\n", node_tail->data);puts("---append element---");printf("please input element to append:");scanf("%d", &element);head=append(head, element);output(head);puts("---delete first node---");head=delete_first(head);head=delete_first(head);output(head);puts("---delete tail node---");head=delete_tail(head);head=delete_tail(head);output(head);puts("---get list len---");int len=get_len(head);printf("len=%d\n", len);puts("---insert_by_pos---");int pos;printf("please input pos: ");scanf("%d", &pos);printf("please input element: ");scanf("%d", &element);head=insert_by_pos(head, pos, element);output(head);puts("---get node by pos---");printf("please input pos:");scanf("%d", &pos);Linklist node  = get_node_by_pos(head, pos);printf("node->data=%d\n", node->data);*/puts("---delete node by pos---");printf("please input pos:");scanf("%d", &pos);head = delete_by_pos(head, pos);output(head);puts("---update node by pos---");printf("please input pos:");scanf("%d", &pos);printf("please element:");scanf("%d", &element);int ret = update_by_pos(head, pos, element);output(head);puts("---get node by element---");printf("please element:");scanf("%d", &element);pos = get_node_by_element(head, element);printf("pos=%d\n",pos);
//puts("---update node by element---");printf("please input a key:");scanf("%d", &key);printf("please input an element:");scanf("%d", &element);ret = update_node_by_element(head, key, element);output(head);
//puts("---delete by element---");printf("please input element:");scanf("%d", &element);head = delete_node_by_element(head, element);output(head);//puts("---reverse list---");head = revser_list(head);output(head);
//return 0;
}
//head.h#ifndef __HEAD_H__
#define __HEAD_H__#include <string.h>
#include <stdlib.h>
#include <stdio.h>typedef int data_type;typedef struct Node
{int data;struct Node *next;}*Linklist;Linklist create();typedef struct Node node;Linklist create_node();
Linklist insert_head(data_type data, Linklist head);
int output(Linklist head);
Linklist append(Linklist head, data_type element);
Linklist get_tail_node(Linklist head);
Linklist delete_first(Linklist head);
Linklist free_node(Linklist node);
Linklist delete_tail(Linklist head);
int get_len(Linklist head);
Linklist insert_by_pos(Linklist head, int pos, data_type element);
Linklist delete_by_pos(Linklist head, int pos);
Linklist get_node_by_pos(Linklist head, int pos);
int update_by_pos(Linklist head, int pos, data_type element);
int get_node_by_element(Linklist head, data_type key);
Linklist delete_node_by_element(Linklist head, data_type key);
Linklist revser_list(Linklist head);
int update_node_by_element(Linklist head, data_type key, data_type element);
#endif

//test.c#include "head.h"Linklist create_node()
{Linklist node=(Linklist)malloc(sizeof(struct Node));if(NULL==node){return NULL;}//successnode->data=0;node->next=NULL;return node;
}Linklist insert_head(data_type element, Linklist head)
{//创建新节点Linklist node = create_node();if(NULL==node){return head;}node->data=element;//判断链表是否位空if(NULL==head){head=node;}else //链表不为空{node->next=head;head=node;		}return head;// 这里的head是形参,所以需要返回用以改变主函数的head
}int output(Linklist head)
{if(NULL==head){return -1;}Linklist node = head;while(node!=NULL){//printf("data=%d, next=%p\t", node->data, node->next);printf("%d\t", node->data);node=node->next;}putchar(10);}Linklist get_tail_node(Linklist head)
{Linklist node=head;while(node->next!=NULL && node!=NULL){node=node->next;}return node;}Linklist append(Linklist head, data_type element)
{//Linklist node = create_node();if(NULL==node){return head;}node->data=element;node->next=NULL;Linklist node_tail=get_tail_node(head);if(NULL==node_tail){head = node;}node_tail->next = node;	return	head;}Linklist free_node(Linklist node)
{free(node);node=NULL;return node;}Linklist delete_first(Linklist head)
{if(NULL==head){return head;}Linklist node_deleted = head;head=head->next;node_deleted = free_node(node_deleted);return head;}Linklist delete_tail(Linklist head)
{if(NULL==head){return head;}//找到 tail 之前的nodeLinklist node = head;if(node->next==NULL){head=free_node(node);return head;}while(NULL!=node->next->next){node=node->next;}node->next=free_node(node->next);node->next=NULL;return head;
}int get_len(Linklist head)
{//puts("in get_len");int count=0;Linklist node=head;while(node!=NULL){//printf("len=%d\n", len);count++;node=node->next;}return count;}Linklist insert_by_pos(Linklist head, int pos, data_type element)
{//pos, Linklist pos start from 1 hearint len=get_len(head);if(pos<1 || pos>len+1){return head;}if(NULL==head || pos==1)head=insert_head(element, head);else{//find node at posLinklist node=head;for(int i=1; i<pos-1; i++){node=node->next;}printf("node(pos-1)->data=%d", node->data);//create new nodeLinklist node_new= create_node();if(NULL==node_new){return head;}node_new->data=element;//insert node_new->next=node->next;node->next=node_new;}return head;
}Linklist get_node_by_pos(Linklist head,int pos)
{if(NULL==head){return head;}int len=get_len(head);if(pos<1||pos>len){return NULL;}Linklist node=head;int count=1;while(count<pos){count++;node=node->next;}return node;}Linklist delete_by_pos(Linklist head,int pos)
{//note: list pos start from 1if(NULL==head){return head;}//判断pos合法int len=get_len(head);if(pos<1 && pos>len) {return head;}if(1==len || 1==pos){head=delete_first(head);}else{Linklist node = get_node_by_pos(head, pos-1);//deleteLinklist node_tbd = node->next;node->next=node->next->next;free_node(node_tbd);node_tbd=NULL;}return head;}int update_by_pos(Linklist head, int pos, data_type element)
{if(NULL==head){return -1;}Linklist node = get_node_by_pos(head, pos);if(NULL==node)return -1;node->data=element;return 0;}int get_node_by_element(Linklist head, data_type key)
{//找到返回pos信息if(NULL==head){return -1;}int count=0;int is_found=0;Linklist node=head;while(NULL!=node){//printf("node->data=%d\n", node->data);count++;if(node->data==key){is_found=1;break;}node=node->next;}if(is_found==1){return count;}else{return -1;}
}Linklist delete_node_by_element(Linklist head, data_type key)
{if(NULL==head){return head;}int pos = get_node_by_element(head, key);if(-1==pos){return head;}head=delete_by_pos(head, pos);return head;}Linklist revser_list(Linklist head)
{if(NULL==head||NULL==head->next){return head;}Linklist node=head->next;head->next=NULL;while(NULL!=node){Linklist node_t=node;node=node->next;node_t->next=head;head=node_t;}return head;
}int update_node_by_element(Linklist head, data_type key, data_type element)
{if(NULL==head){return -1;}int pos=get_node_by_element(head, key);if(-1==pos){return -1;}int ret=update_by_pos(head, pos, element);return ret;}

运行结果

please input n:7
please input No.1 element: 1
please input No.2 element: 2
please input No.3 element: 3
please input No.4 element: 4
please input No.5 element: 5
please input No.6 element: 6
please input No.7 element: 7
---ouput Linklist---
7	6	5	4	3	2	1	
---delete node by pos---
please input pos:1
6	5	4	3	2	1	
---update node by pos---
please input pos:2
please element:500
6	500	4	3	2	1	
---get node by element---
please element:4
pos=3
---update node by element---
please input a key:4
please input an element:400
6	500	400	3	2	1	
---delete by element---
please input element:2
6	500	400	3	1	
---reverse list---
1	3	400	500	6	

2.思维导图

相关文章:

数据结构 / day04 作业

1. 单链表任意位置删除, 单链表任意位置修改, 单链表任意位置查找, 单链表任意元素查找, 单链表任意元素修改, 单链表任意元素删除, 单链表逆置 // main.c#include "head.h"int main(int argc, const char *argv[]) {Linklist headNULL; //head 是头指针// printf(&q…...

Java核心知识点整理大全20-笔记

目录 17. 设计模式 17.1.1. 设计原则 17.1.24. 解释器模式 18. 负载均衡 18.1.1.1. 四层负载均衡&#xff08;目标地址和端口交换&#xff09; 18.1.1.2. 七层负载均衡&#xff08;内容交换&#xff09; 18.1.2. 负载均衡算法/策略 18.1.2.1. 轮循均衡&#xff08;Roun…...

Spark---转换算子、行动算子、持久化算子

一、转换算子和行动算子 1、Transformations转换算子 1&#xff09;、概念 Transformations类算子是一类算子&#xff08;函数&#xff09;叫做转换算子&#xff0c;如map、flatMap、reduceByKey等。Transformations算子是延迟执行&#xff0c;也叫懒加载执行。 2)、Transf…...

什么是关系型数据库?

什么是关系型数据库&#xff1f; 关系型数据库&#xff08;RDBMS&#xff09;是建立在关系模型基础上的数据库系统。关系模型是一种数据模型&#xff0c;它表示数据之间的联系&#xff0c;包括一对一、一对多和多对多的关系。在关系型数据库中&#xff0c;数据以表格的形式存储…...

【LeetCode】挑战100天 Day12(热题+面试经典150题)

【LeetCode】挑战100天 Day12&#xff08;热题面试经典150题&#xff09; 一、LeetCode介绍二、LeetCode 热题 HOT 100-142.1 题目2.2 题解 三、面试经典 150 题-143.1 题目3.2 题解 一、LeetCode介绍 LeetCode是一个在线编程网站&#xff0c;提供各种算法和数据结构的题目&…...

ArcGIS10.x系列 Python工具箱教程

ArcGIS10.x系列 Python工具箱教程 目录 1.前提 2.需要了解的资料 3.Python工具箱制作教程 4. Python工具箱具体样例代码&#xff08;DEM流域分析-河网等级矢量化&#xff09; 1.前提 如果你想自己写Python工具箱&#xff0c;那么假定你已经会ArcPy&#xff0c;如果只是自己…...

【蓝桥杯】刷题

刷题网站 记录总结刷题过程中遇到的一些问题 1、最大公约数与最小公倍数 a,bmap(int,input().split())sa*bwhile a%b:a,bb,a%bprint(b,s//b)2.迭代法求平方根(题号1021) #include<stdio.h> #include<math.h> int main() {double x11.0,x2;int a;scanf("%d&…...

软件产品登记的材料条件

(1&#xff09;申请双软认证前应该要获得信息产业部授权的软件检测机构出具的检测证明&#xff0c;这份检测证明可以到软件行业协会申请&#xff0c;然后协会会派专家到公司进行“检测”&#xff0c;检测通过后出具证明&#xff0c;这份证明的申请与软件著作权等无关&#xff0…...

春节后跟进客户开发信模板?外贸邮件模板?

适合新年的客户开发信模板&#xff1f;年后给客户的邮件怎么写&#xff1f; 在春节这一传统的中国节日结束后&#xff0c;跟进客户对于维持和发展业务至关重要。客户开发信模板是一种有效的工具。蜂邮将介绍一些春节后跟进客户开发信模板的关键技巧&#xff0c;以确保您的业务…...

个人财务管理软件CheckBook Pro mac中文版特点介绍

CheckBook Pro mac是一款Mac平台的个人财务管理软件&#xff0c;主要用于跟踪个人收入、支出和账户余额等信息。 CheckBook Pro mac 软件特点 简单易用&#xff1a;该软件的用户界面非常简洁明了&#xff0c;即使您是初学者也可以轻松上手。 多账户管理&#xff1a;该软件支持…...

rfc4301- IP 安全架构

1. 引言 1.1. 文档内容摘要 本文档规定了符合IPsec标准的系统的基本架构。它描述了如何为IP层的流量提供一组安全服务&#xff0c;同时适用于IPv4 [Pos81a] 和 IPv6 [DH98] 环境。本文档描述了实现IPsec的系统的要求&#xff0c;这些系统的基本元素以及如何将这些元素结合起来…...

【数据结构/C++】线性表_双链表基本操作

#include <iostream> using namespace std; typedef int ElemType; // 3. 双链表 typedef struct DNode {ElemType data;struct DNode *prior, *next; } DNode, *DLinkList; // 初始化带头结点 bool InitDNodeList(DLinkList &L) {L (DNode *)malloc(sizeof(DNode))…...

前端已死?看看我的秋招上岸历程

背景 求职方向&#xff1a;web前端 技术栈&#xff1a;vue2、springboot&#xff08;学校开过课&#xff0c;简单的学习过&#xff09; 实习经历&#xff1a;两段&#xff0c;但都是实训类的&#xff0c;说白了就是类似培训&#xff0c;每次面试官问起时我也会坦诚交代&…...

Flink Flink中的合流

一、Flink中的基本合流操作 在实际应用中&#xff0c;我们经常会遇到来源不同的多条流&#xff0c;需要将它们的数据进行联合处理。所以 Flink 中合流的操作会更加普遍&#xff0c;对应的 API 也更加丰富。 二、联合&#xff08;Union&#xff09; 最简单的合流操作&#xf…...

工业园区重金属废水深度处理工程项目,稳定出水0.1mg/l

随着环保要求不断提高&#xff0c;工业废水处理已成为众多企业的必修课。然而在工业生产中&#xff0c;如何有效处理含有重金属的废水成为了一个关键的挑战。 重金属废水是指含有汞、铅、铜、镉、锌、镍等有毒有害物质的废水&#xff0c;来源于矿山开采、金属冶炼、电镀、印刷线…...

element table滚动条失效

问题描述:给el-table限制高度之后滚动条没了 给看看咋设置的&#xff1a; <el-table:data"tableData"style"width: 100%;"ref"table"max-height"400"sort-change"changeSort">对比了老半天找不出问题&#xff0c;最后…...

代码随想录算法训练营 ---第四十六天

第一题&#xff1a; 简介&#xff1a; 本题的重点在于确定背包容量和物品数量 确定dp数组以及下标的含义 dp[i] : 字符串长度为i的话&#xff0c;dp[i]为true&#xff0c;表示可以拆分为一个或多个在字典中出现的单词。 2.确定递推公式 如果确定dp[j] 是true&#xff0c;且…...

MySQL-02-InnoDB存储引擎

实际的业务系统开发中&#xff0c;使用MySQL数据库&#xff0c;我们使用最多的当然是支持事务并发的InnoDB存储引擎的这种表结构&#xff0c;下面我们介绍下InnoDB存储引擎相关的知识点。 1-Innodb体系架构 InnoDB存储引擎有多个内存块&#xff0c;可以认为这些内存块组成了一…...

Qt路径和Anaconda中QT路径冲突(ubuntu系统)

最近做一个项目需要配置QT库&#xff0c;本项目配置环境如下&#xff1a; Qt version 5 Operating system, version and so on ubuntu 20.04 Description 之前使用过anaconda环境安装过QT5&#xff0c;所以在项目中CMakeLists文件中使用find_package时候&#xff0c;默认使用An…...

vue2.js添加水印

通过canvas生成水印图片 function addWaterMark(str) {let ctx document.createElement("canvas");ctx.width 900;ctx.height 450;ctx.style.display "none";let cans ctx.getContext("2d");cans.rotate((-20 * Math.PI) / 180);cans.font…...

转行AI Agent的真实成本:时间、金钱与精力

建议按照我下面的办法来做&#xff0c;不一定能让你成为LLM专家&#xff0c;但一定能帮你快速入门&#xff0c;少走弯路。 1.先把模型“用顺”一上来别纠结框架、工程化这些&#xff0c;第一件事是能稳定调用一个模型&#xff0c;让它按你想要的格式输出结果。很多人卡在这里&a…...

如何快速构建复杂多资源类型Kubernetes Operator:Kopf实战案例指南

如何快速构建复杂多资源类型Kubernetes Operator&#xff1a;Kopf实战案例指南 【免费下载链接】kopf A Python framework to write Kubernetes operators in just a few lines of code 项目地址: https://gitcode.com/gh_mirrors/ko/kopf Kubernetes Operator是自动化管…...

从实验室到生产线:差动变压器和霍尔传感器在工业自动化中的选型与避坑指南

工业自动化中的位移检测双雄&#xff1a;差动变压器与霍尔传感器的实战选型指南 在机床主轴定位误差超过0.01mm就会导致零件报废的生产线上&#xff0c;在机械臂末端执行器需要实时反馈位置的精密装配场景中&#xff0c;位移传感器的选型直接决定了自动化系统的可靠性与精度。不…...

Leather Dress Collection详细步骤:从SD1.5环境搭建到12个皮装模型调用

Leather Dress Collection详细步骤&#xff1a;从SD1.5环境搭建到12个皮装模型调用 1. 项目介绍 Leather Dress Collection是一个基于Stable Diffusion 1.5的LoRA模型集合&#xff0c;专门用于生成各种皮革服装风格的图像。这个集合包含了12个精心训练的LoRA模型&#xff0c;…...

ComfyUI-MimicMotionWrapper深度解析:如何实现精准AI动作迁移

ComfyUI-MimicMotionWrapper深度解析&#xff1a;如何实现精准AI动作迁移 【免费下载链接】ComfyUI-MimicMotionWrapper 项目地址: https://gitcode.com/gh_mirrors/co/ComfyUI-MimicMotionWrapper ComfyUI-MimicMotionWrapper是一个基于ComfyUI平台的AI动作迁移插件&a…...

AlmaLinux 9.6 基础环境配置全攻略:从yum源优化到SSH安全加固

1. AlmaLinux 9.6 环境初始化实战 刚装好的AlmaLinux 9.6系统就像毛坯房&#xff0c;得先做好基础装修才能住得舒服。作为CentOS的完美替代品&#xff0c;AlmaLinux继承了RHEL系的稳定基因&#xff0c;但默认配置往往需要根据实际需求调整。下面我就用自己趟过坑的经验&#xf…...

北美面试生存指南:如何优雅应对“压力测试”与“强势发问”?

顺利通过了简历筛选和第一轮的基础技术面&#xff0c;你信心满满地进入了 Onsite&#xff08;现场/视频连线&#xff09;轮次。然而&#xff0c;你遇到的面试官却全程板着脸&#xff0c;不仅频繁打断你的发言&#xff0c;还对你简历上最引以为傲的项目提出尖锐质疑&#xff1a;…...

从零到一:OpenSPG Docker化部署全流程实战

1. 环境准备&#xff1a;Docker与Docker Compose安装 第一次接触OpenSPG时&#xff0c;我花了两天时间才把环境折腾明白。现在回头看&#xff0c;其实只要把Docker和Docker Compose装对版本&#xff0c;后面基本不会踩坑。建议直接用官方脚本安装&#xff0c;比手动配置省心得多…...

如何深度移除Windows Defender:高级权限工具配置指南

如何深度移除Windows Defender&#xff1a;高级权限工具配置指南 【免费下载链接】windows-defender-remover A tool which is uses to remove Windows Defender in Windows 8.x, Windows 10 (every version) and Windows 11. 项目地址: https://gitcode.com/gh_mirrors/wi/w…...

直播内容自动化采集系统:如何实现40+平台无人值守录制

直播内容自动化采集系统&#xff1a;如何实现40平台无人值守录制 【免费下载链接】DouyinLiveRecorder 可循环值守和多人录制的直播录制软件&#xff0c;支持抖音、TikTok、Youtube、快手、虎牙、斗鱼、B站、小红书、pandatv、sooplive、flextv、popkontv、twitcasting、winktv…...