数据结构 / 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.思维导图

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

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

相关文章

如何基于gensim和Sklearn实现文本矢量化

大家利用机器学习或深度学习开展文本分类或关联性分析之前&#xff0c;由于计算机只能分析数值型数据&#xff0c;而人类所熟悉的自然语言文字&#xff0c;机器学习算法是一窍不通的&#xff0c;因此需要将大类的文本及前后关系进行设计&#xff0c;并将其转换为数值化表示。一…

【Python 训练营】N_11 模拟进度条

题目 格式化输出进度条&#xff0c;具体格式如下&#xff1a; 分析 需要格式化打印&#xff0c;进度条随时间显示进展&#xff0c;需要用time模块的sleep()函数。 答案 import time # 导入time模块 length 100 # 定义进度长度模块 for i in range(1,length1): # 遍历1&…

96.STL-遍历算法 transform

目录 transform 语法&#xff1a; 功能描述&#xff1a; 函数原型&#xff1a; 代码示例&#xff1a; transform 是 C 标准模板库&#xff08;STL&#xff09;中的一个算法&#xff0c;用于对一个范围内的元素进行转换并将结果存储到另一个范围。以下是简要解释和一个示例…

ES6模块化导出

ES6模块规范中&#xff0c;主要使用export和import来达成模块化的导入与导出。不同于CommonJS&#xff0c;这是ES6新引入的模块化概念&#xff0c;一个文件即是一个模块。 在ES6中&#xff0c;你可以使用export命令来导出变量、函数或类&#xff0c;以便其他模块可以使用它们。…

C++——vector互换容器与预留空间

一.vector互换容器 功能描述:实现两个容器内元进行互换 函数原型: swap(vec); //将vec与本身的元素互换 实例: //1.基本使用 void test01() {vector<int>v1;for (int i 0; i < 10; i){v1.push_back(i);}cout << "交换前:" << e…

AIGC系列之:Vision Transformer原理及论文解读

目录 相关资料 模型概述 Patch to Token Embedding Token Embedding Position Embedding ViT总结 相关资料 论文链接&#xff1a;https://arxiv.org/pdf/2010.11929.pdf 论文源码&#xff1a;https://github.com/google-research/vision_transformer PyTorch实现代码…

国内外的ERP系统存在显著的差异,差在哪?

在全球化进程中&#xff0c;众多企业逐渐采纳企业资源计划 (ERP)软件&#xff0c;以增强生产效益、整合各部门资源并降低成本。尽管我国的ERP软件公司在最近几年中逐步崭露头角并获得了部分市场份额&#xff0c;但外国的ERP软件仍然受到了更多的喜爱&#xff0c;这背后的原因是…

每日博客Day7

每日博客Day 7 每日算法 707.设计链表 题意&#xff1a; 在链表类中实现这些功能&#xff1a; get(index)&#xff1a;获取链表中第 index 个节点的值。如果索引无效&#xff0c;则返回-1。addAtHead(val)&#xff1a;在链表的第一个元素之前添加一个值为 val 的节点。插入…

Nacos 端口偏移量说明

因为安全原因&#xff0c;在部署nacos-2.2.3版本时&#xff0c;将nacos的application.properties中的server.port端口值由默认值8848改成了server.port8425 问题&#xff1a;nacos 启动时(sh start.sh -m standalone)报错 如下&#xff1a; 经过分析&#xff0c;原因是 9425 …

深信服防火墙设置应用控制策略(菜鸟必看)

PS&#xff1a;前几天发布了关于深信服防火墙路由部署的流程&#xff1a;深信服防火墙路由模式开局部署-手把手教学&#xff08;小白篇&#xff09;-CSDN博客 昨天晚上有csdn的朋友联系我&#xff0c;说有一个关于ACL访问的问题要帮忙看一下 解决了以后&#xff0c;写个大概的…

Python变量及其使用

无论使用什么语言编程&#xff0c;总要处理数据&#xff0c;处理数据就需要使用变量来保存数据。 形象地看&#xff0c;变量就像一个个小容器&#xff0c;用于“盛装”程序中的数据。常量同样也用于“盛装”程序中的数据。常量与变量的区别是&#xff1a;常量一旦保存某个数据…

智感AI2.0

一、智感AI能力构建方向 1、实时监测&#xff0c;尽可能早的发现问题 2、全面的测量能力&#xff0c;jin尽可能快速定位问题 3、高效的处置能力 无线 二、主要功能 1、智感AP&#xff1a;网络质量、业务质量、认证质量感知 2、安视交换机&#xff1a;业务体验感知、网络…

Vue2中的两种普通注册方式

1. 两种注册方式&#xff1a; ① 局部注册&#xff1a; (1) 创建.vue组件 (单文件组件) (2) 使用的组件内 导入&#xff0c;并 局部注册 components: { 组件名&#xff1a;组件对象 } ② 全局注册&#xff1a; (1) 创建.vue组件 (单文件组件) (2) main.js 内导入&…

如何解决 Java 中的 IllegalArgumentException 异常?

非法参数异常&#xff08;IllegalArgumentException&#xff09;的抛出是为了表明一个方法被传递了一个非法参数。该异常扩展了 RuntimeException 类&#xff0c;因此属于在 Java 虚拟机&#xff08;JVM&#xff09;运行期间可能抛出的异常。它是一种未检查异常&#xff0c;因此…

【开题报告】基于Vue.js的膳食搭配平台的设计与实现

1.研究背景 现代社会&#xff0c;人们对健康的关注日益增加&#xff0c;膳食搭配成为了一个重要的话题。合理的膳食搭配可以保证人体摄取到足够的营养物质&#xff0c;维持身体健康。然而&#xff0c;很多人由于缺乏相关的知识和指导&#xff0c;无法进行科学的膳食搭配&#…

Android开源框架--Dagger2详解

功名只向马上取&#xff0c;真是英雄一丈夫 一&#xff0c;定义 我们知道在一个类中&#xff0c;通常会定义其他类型的变量&#xff0c;这个变量就是我们所说的“依赖“。 对一个类的变量进行初始化&#xff0c;有两种方式。第一种&#xff0c;这个类自己进行初始化&#xff…

4.Spring源码解析-loadBeanDefinitions(XmlBeanDefinitionReader)

第一个点进去 发现是空 肯定走的第二个逻辑了 这里在这里已经给属性设置了值&#xff0c;所以肯定不是空能拿到。 1.ClassPathXmlApplicationContext 总结&#xff1a;该loadBeanDefinitions是XmlBeanDefinitionReader设置xml文件在哪。

M3VSNET:无监督多度量多视图立体视觉网络(2021年)

M3VSNET&#xff1a;无监督多度量多视图立体视觉网络&#xff08;2021年&#xff09; 摘要1 引言2 相关工作3 实现方法3.1 网络架构 B. Huang, H. Yi, C. Huang, Y. He, J. Liu and X. Liu, “M3VSNET: Unsupervised Multi-Metric Multi-View Stereo Network,” 2021 IEEE Inte…

轻巧高效的剃须好工具,DOCO黑刃电动剃须刀上手

剃须刀大家都用过&#xff0c;我比较喜欢电动剃须刀&#xff0c;尤其是多刀头的悬浮剃须刀&#xff0c;感觉用起来很方便&#xff0c;剃须效率也很高。最近我在用一款DOCO小蔻的黑刃电动剃须刀&#xff0c;这款剃须刀轻巧易用&#xff0c;而且性价比超高。 相比于同类产品&…

Keil5 debug

目录 debug调试功能 基本功能&#xff1a; 程序复位&#xff1a;Reset 运行&#xff1a;Run 停止&#xff1a;Stop 断点调试&#xff08;Breakpoint Debugging&#xff09; 单步调试&#xff1a; 单步调试:Step 单步跳过调试&#xff1a;Step Over&#xff1a; 单步返…