数据结构P46(2-1~2-4)

2-1编写算法查找顺序表中值最小的结点,并删除该结点

#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
struct List
{int Max;//最大元素 int n;//实际元素个数 DataType *elem;//首地址 
};
typedef struct List*SeqList;//顺序表类型定义
SeqList SetNullList_Seq(int m)
{SeqList slist=(SeqList)malloc(sizeof(struct List));if(slist!=NULL){slist->elem=(DataType*)malloc(sizeof(DataType)*m);//申请顺序表空间,大小为m个DataType空间 if(slist->elem){slist->Max=m;//顺序表赋予的最大值 slist->n=0;//顺序表长度赋值为0 return(slist);}else free(slist);}printf("Alloc failure!\n");return NULL;}int IsNullList_seq(SeqList slist)
{return(slist->n==0);
} /*int InsertPre_seq(SeqList slist ,int p, DataType x)
{int q;if (slist->n>=slist->Max)//顺序表满了 {printf("overflow");return 0;}if(p<0||p>slist->n)//下标不存在 {printf("not exist!\n");return 0;}for(q=slist->n-1;q>=p;q--){slist->elem[q+1]=slist->elem[q];}slist->elem[p]=x;slist->n=slist->n+1;return 1;
}*/int DelIndex_seq(SeqList slist,int p)
{int i=0,q=0;for(i=0;i<slist->n;i++){if(slist->elem[i]==p){for(q=i;q<slist->n-1;q++){slist->elem[q]=slist->elem[q+1];}slist->n=slist->n-1;return 1;}}
}
void Print(SeqList slist)
{int i;for(i=0;i<slist->n;i++){printf("%d\n",slist->elem[i]);}
}int Insert_min(SeqList slist)
{int min=0,i=0;min=slist->elem[0];for(i=0;i<slist->n;i++){if(slist->elem[i]<min){min=slist->elem[i];}}return min;
}
int main()
{SeqList alist;int max,len,i,x,p;printf("\n please input the max value(<100) of max=");scanf("%d",&max);alist=SetNullList_Seq(max);printf("%d\n",IsNullList_seq(alist));if(alist!=NULL){printf("\n please input the length of list len =");scanf("%d",&len);for(i=0;i<len;i++){scanf("%d",&x);alist->elem[i]=x;alist->n=i+1;}}printf("The List is:\n"); Print(alist);p=Insert_min(alist);DelIndex_seq(alist,p);printf("After deleting the min the list is :\n");Print(alist);return 1;
}

在这里插入图片描述

2-2编写算法查找单链表中值最大的结点,并将该结点移至链表尾部


#include<stdio.h>
#include<stdlib.h>typedef int DataType; 
struct Node
{DataType      data; struct Node*  next;  
};
typedef struct Node  *PNode;    
typedef struct Node  *LinkList;   void MoveMaxToTail(head);LinkList SetNullList_Link() //设置头结点
{LinkList head = (LinkList)malloc(sizeof(struct Node));if (head != NULL) head->next = NULL;else printf("alloc failure");return head; 
}void CreateList_Tail(struct Node* head)//利用尾插法
{PNode p = NULL;PNode q = head;DataType data;scanf("%d", &data);while (data != -1){   p = (struct Node*)malloc(sizeof(struct Node));p->data = data;p->next = NULL;q->next = p;q = p;scanf("%d", &data);}
}
void  MoveMaxToTail(LinkList head)//移动到最后
{PNode pmax=NULL,p=NULL,pre=NULL,end=NULL;pmax=head->next;p=head->next->next;while(p){if(p->data>pmax->data){pmax=p;}p=p->next;}if(pmax->next==NULL){return 1;}else{p=head;while(p){if(p->next==pmax){pre=p;}if(p->next==NULL){end=p;}p=p->next;}pre->next=pmax->next;pmax->next=end->next;end->next=pmax;} return 0;
}
void print(LinkList head)   //打印
{PNode  p = head->next;while (p){printf("%d ", p->data);p = p->next;}
}
void DestoryList_Link(LinkList head)  //销毁链表
{PNode  pre = head; PNode p = pre->next;while (p){free(pre);pre = p;p = pre->next;}free(pre);
}int main()
{LinkList head = NULL;head = SetNullList_Link();CreateList_Tail(head);MoveMaxToTail(head);print(head);DestoryList_Link(head);return 0;
}

在这里插入图片描述

2-3编写算法实现顺序表的就地逆序置,即利用原表的存储空间将线性表(a1.a2,…,an)逆置为(an,an-1,…,a1),并分析设计的算法时间复杂度

#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
struct List
{int Max;//最大元素 int n;//实际元素个数 DataType *elem;//首地址 
};
typedef struct List*SeqList;//顺序表类型定义
SeqList SetNullList_Seq(int m)
{SeqList slist=(SeqList)malloc(sizeof(struct List));if(slist!=NULL){slist->elem=(DataType*)malloc(sizeof(DataType)*m);//申请顺序表空间,大小为m个DataType空间 if(slist->elem){slist->Max=m;//顺序表赋予的最大值 slist->n=0;//顺序表长度赋值为0 return(slist);}else free(slist);}printf("Alloc failure!\n");return NULL;}int IsNullList_seq(SeqList slist)
{return(slist->n==0);
} void Print(SeqList slist)
{int i;for(i=0;i<slist->n;i++){printf("%d\n",slist->elem[i]);}
}void DispList(SeqList slist)
{int i,x;for(i=0;i<slist->n/2;i++)// 只需要遍历原表的一半就可以实现数据元素位置的交换{x=slist->elem[i];slist->elem[i]=slist->elem[slist->n-i-1];slist->elem[slist->n-i-1]=x;}
}int main()
{SeqList alist;int max,len,i,x,p;printf("\n please input the max value(<100) of max=");scanf("%d",&max);alist=SetNullList_Seq(max);printf("%d\n",IsNullList_seq(alist));if(alist!=NULL){printf("\n please input the length of list len =");scanf("%d",&len);for(i=0;i<len;i++){scanf("%d",&x);alist->elem[i]=x;alist->n=i+1;}}printf("The List is:\n"); Print(alist);DispList(alist);printf("The dispList is:\n"); Print(alist);return 1;
}

在这里插入图片描述
这段代码实现了一个顺序表,其中包括初始化、判断是否为空表、打印表元素、反转表元素等功能。算法时间复杂度如下:

  1. SetNullList_Seq:申请顺序表空间,时间复杂度为O(1)。
  2. IsNullList_seq:判断是否为空表,时间复杂度为O(1)。
  3. Print:遍历表元素并打印,时间复杂度为O(n),其中n为表的长度。
  4. DispList:遍历表元素并交换位置,时间复杂度为O(n/2),其中n为表的长度。

所以,整个代码的时间复杂度取决于最耗时的操作,即遍历表元素和交换位置。因此,整体的时间复杂度为O(n)。

2-4编写算法实现链表得到就地逆置,即利用原表的存储空间将线性表(a1,a2,…,an),逆置为(an,an-1,…,a1),并分析设计的算法时间复杂度

#include<stdio.h>
#include<stdlib.h>typedef int DataType; 
struct Node
{DataType      data; struct Node*  next;  
};
typedef struct Node  *PNode;    
typedef struct Node  *LinkList;   LinkList SetNullList_Link() //设置头结点
{LinkList head = (LinkList)malloc(sizeof(struct Node));if (head != NULL) head->next = NULL;else printf("alloc failure");return head; 
}void CreateList_Tail(struct Node* head)//利用尾插法
{PNode p = NULL;PNode q = head;DataType data;scanf("%d", &data);while (data != -1){   p = (struct Node*)malloc(sizeof(struct Node));p->data = data;p->next = NULL;q->next = p;q = p;scanf("%d", &data);}
}
void ListOppose(LinkList head)//逆置 
{LinkList p,t;p=head->next;while(p->next!=NULL){t=p->next;p->next=t->next;//此时跳过了t结点元素,也就是说t结点元素脱离了链表t->next=head->next; //将t结点元素放在头结点后面,即t结点元素成为第一个结点head->next=t; //头结点的指针指向t}
}
void print(LinkList head)   //打印
{PNode  p = head->next;while (p){printf("%d ", p->data);p = p->next;}
}
void DestoryList_Link(LinkList head)  //销毁链表
{PNode  pre = head; PNode p = pre->next;while (p){free(pre);pre = p;p = pre->next;}free(pre);
}int main()
{LinkList head = NULL;head = SetNullList_Link();CreateList_Tail(head);print(head);printf("\n");ListOppose(head);print(head);return 0;
}

在这里插入图片描述
这段代码实现了一个链表,并包括了创建链表、逆置(反转)链表、打印链表和销毁链表等功能。算法的时间复杂度如下:

  1. SetNullList_Link:创建头结点,时间复杂度为O(1)。
  2. CreateList_Tail:利用尾插法创建链表,时间复杂度为O(n),其中n为输入的元素个数。
  3. ListOppose:逆置链表,时间复杂度为O(n),其中n为链表的长度。
  4. print:遍历链表并打印,时间复杂度为O(n),其中n为链表的长度。
  5. DestroyList_Link:销毁链表,时间复杂度为O(n),其中n为链表的长度。

因此,整个代码的时间复杂度取决于具有最高时间复杂度的操作,即创建链表、逆置链表和销毁链表,这三者的时间复杂度均为O(n)。所以,整体的时间复杂度为O(n)。

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

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

相关文章

Spring 作用域解析器AnnotationScopeMetadataResolver

博主介绍&#xff1a;✌全网粉丝近5W&#xff0c;全栈开发工程师&#xff0c;从事多年软件开发&#xff0c;在大厂呆过。持有软件中级、六级等证书。可提供微服务项目搭建与毕业项目实战&#xff0c;博主也曾写过优秀论文&#xff0c;查重率极低&#xff0c;在这方面有丰富的经…

基于SSM的大学生就业信息管理系统设计与实现

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;采用JSP技术开发 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#x…

利用freesurfer6进行海马分割的环境配置和步骤,以及获取海马体积

利用freesurfer6进行海马分割的环境配置和步骤 Matlab Runtime 安装1. 运行recon-all&#xff1a;2. 利用 recon-all -s subj -hippocampal-subfields-T1 进行海马分割3. 结束后需要在/$SUBJECTS_DIR/subject/的文件夹/mri路径下输入下面的代码查看分割情况4. 在文件SUBJECTS_D…

C++转换函数

什么是转换函数? C转换函数是一种特殊的成员函数&#xff0c;用于将一个类的对象转换为另一个类型。它是通过在类中定义特定的函数来实现的。 转换函数的用途&#xff1a; 类型转换&#xff1a;转换函数可以将一个类的对象从一种类型转换为另一种类型。这样可以方便地在不同…

A*算法和Dijkstra

A*算法 https://www.redblobgames.com/pathfinding/a-star/introduction.html这是个宝藏网页&#xff0c;https://www.redblobgames.com/pathfinding/a-star/introduction.html&#xff0c;里边的图可以一步一步演示&#xff01; A*算法 个人理解FGH&#xff0c;F是总距离&a…

win10睡眠快捷方式

新建快捷方式 如下图 内容如下 rundll32.exe powrprof.dll,SetSuspendState 0,1,0 下一步 点击完成即可。 特此记录 anlog 2023年10月6日

一年一度的国庆节又结束了

这里写自定义目录标题 欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题&#xff0c;有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants 创建一个自定义列表如何创建一个…

软件项目和安全项目案例(承接软件和安全项目合作)

公司有专业的软件开发团队和安全研究团队&#xff0c;具备完善的安全测试、安全培训、安全开发、安全服务等安全解决方案&#xff0c;可以助力政企研发专业、高效、安全、稳定的软件产品&#xff0c;欢迎项目咨询、商务合作&#xff01; 一、软件开发项目咨询 1.承接车载等终…

Iphone文件传到电脑用什么软件,看这里

在数字化时代&#xff0c;文件传输已经成为我们日常生活中不可或缺的一部分。然而&#xff0c;苹果用户在将手机文件传输到电脑时&#xff0c;往往会面临一些困扰。曾经的“文件传输助手”并不能完全满足用户的需求。于是&#xff0c;很多人开始寻找更便捷的解决方案。在本文中…

【CMU15-445 Part-15】Query Planning Optimization II

Part15-Query Planning & Optimization II Selection Statistics 维护每张表中的基本主要信息也就是tuple数量 N R N_R NR​以及每个属性中不同值的数量 V ( A , R ) V(A,R) V(A,R)&#xff0c; N R N_R NR​关系R中的元组数量&#xff0c;单独维护&#xff0c;不能用pag…

Python 无废话-基础知识函数详解

函数定义 函数是一段可重复使用的代码块&#xff0c;用于实现特定的功能。 类似的前面已学过print(),len(),input(),str(),list(),tuple()等内置函数 自定义函数 内置函数是python类库的已定义好的函数&#xff0c;如果某些功能不能满足时&#xff0c;需要我们自定义函数&am…

网页版”高德地图“如何设置默认城市?

问题&#xff1a; 每次打开网页版高德地图时默认定位的都是“北京”&#xff0c;想设置起始点为目前本人所在城市&#xff0c;烦恼的是高德地图默认的初始位置是北京。 解决&#xff1a; 目前网页版高德地图暂不支持设置起始点&#xff0c;打开默认都是北京&#xff0c;只能将…

假期题目整合

1. 下载解压题目查看即可 典型的猪圈密码只需要照着输入字符解开即可得到答案 2. 冷门类型的密码题型&#xff0c;需要特意去找相应的解题思路&#xff0c;直接百度搜索天干地支解密即可 3. 一眼能出思路他已经给了篱笆墙的提示提示你是栅栏密码对应解密即可 4. 最简单的社会主…

SLAM面试笔记(8) — 计算机视觉面试题

目录 问题1&#xff1a;目标检测的算法分类 问题2&#xff1a;卷积神经网络的组成 问题3&#xff1a;输入层的作用 问题4&#xff1a;卷积层作用 问题5&#xff1a;卷积核类型 问题6&#xff1a;11卷积核作用 问题7&#xff1a;卷积核是否越大越好 问题8&#xff1a;棋…

纯css实现3D鼠标跟随倾斜

老规矩先上图 为什么今天会想起来整这个呢?这是因为和我朋友吵架, 就是关于这个效果的,就是这个 卡片懸停毛玻璃效果, 我朋友认为纯css也能写, 我则坦言他就是在放狗屁,这种跟随鼠标的3D效果要怎么可能能用纯css写, 然后吵着吵着发现,欸,好像真能用css写哦,我以前还写过这种…

大华城市安防系统平台任意文件下载漏洞

一、漏洞描述 大华城市安防监控系统平台是一款集视频、报警、存储、管理于一体的综合安防解决方案。该平台支持多种接入方式&#xff0c;包括网络视频、模拟视频、数字视频、IP电话、对讲机等。此外&#xff0c;该平台还支持多种报警方式&#xff0c;包括移动侦测、区域入侵、…

7 航空公司客户价值分析

第7章 航空公司客户价值分析 7.1 了解航空公司现状与客户价值分析7.1.1 了解航空公司现状7.1.2 认识客户价值分析7.1.3 熟悉航空客户价值分析的步骤与流程 7.2 预处理航空客户数据7.2.1 处理数据缺失值与异常值7.2.2 构建航空客户价值分析的关键特征1. RFM模型介绍2. RFM模型结…

PyTorch入门之【AlexNet】

参考文献&#xff1a;https://www.bilibili.com/video/BV1DP411C7Bw/?spm_id_from333.999.0.0&vd_source98d31d5c9db8c0021988f2c2c25a9620 AlexNet 是一个经典的卷积神经网络模型&#xff0c;用于图像分类任务。 目录 大纲dataloadermodeltraintest 大纲 各个文件的作用&…

通过ElementUi在Vue搭建的项目中实现CRUD

&#x1f3c5;我是默&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f31f;在这里&#xff0c;我要推荐给大家我的专栏《Vue》。&#x1f3af;&#x1f3af; &#x1f680;无论你是编程小白&#xff0c;还是有一定基础的程序员&#xff0c;这个专栏…

摄影后期图像编辑软件Lightroom Classic 2023 mac中文特点介绍

Lightroom Classic 2023 mac是一款图像处理软件&#xff0c;是数字摄影后期制作的重要工具之一&#xff0c;lrc2023 mac适合数字摄影后期制作、摄影师、设计师等专业人士使用。 Lightroom Classic 2023 mac软件特点 高效的图像管理&#xff1a;Lightroom Classic提供了强大的图…