编程实现自定义的顺序查找表类LinearSearchList,实现其中的造表(Add,Insert)、顺序查找(IndexOf)、二分查找 (BinarySearch)等算法。在exp7app项目中定义一个含Main方法的测试类(LinearSearchListTest.cs),在其中定义和随机初始化一个查找表实例,测试顺序查找和二分查找算法。

时间: 2024-02-11 22:08:53 浏览: 19
以下是C#语言实现自定义的顺序查找表类LinearSearchList和测试类LinearSearchListTest.cs的代码: ```csharp //LinearSearchList.cs using System; namespace LinearSearchList { public class LinearSearchList<T> { private T[] elements; private int count; public LinearSearchList(int size) { elements = new T[size]; count = 0; } public void Add(T element) { if (count == elements.Length) { throw new InvalidOperationException("List is full"); } elements[count++] = element; } public void Insert(int index, T element) { if (index < 0 || index > count) { throw new ArgumentOutOfRangeException("Index is out of range"); } if (count == elements.Length) { throw new InvalidOperationException("List is full"); } for (int i = count; i > index; i--) { elements[i] = elements[i - 1]; } elements[index] = element; count++; } public int IndexOf(T element) { for (int i = 0; i < count; i++) { if (elements[i].Equals(element)) { return i; } } return -1; } public int BinarySearch(T element) { int left = 0; int right = count - 1; while (left <= right) { int mid = (left + right) / 2; int compare = Comparer<T>.Default.Compare(element, elements[mid]); if (compare == 0) { return mid; } else if (compare < 0) { right = mid - 1; } else { left = mid + 1; } } return -1; } } } ``` ```csharp //LinearSearchListTest.cs using System; namespace LinearSearchList { class LinearSearchListTest { static void Main(string[] args) { LinearSearchList<int> list = new LinearSearchList<int>(10); // 随机初始化列表元素 Random random = new Random(); for (int i = 0; i < 10; i++) { list.Add(random.Next(100)); } Console.WriteLine("List elements:"); for (int i = 0; i < list.Count; i++) { Console.Write(list[i] + " "); } Console.WriteLine(); Console.WriteLine("Index of 50: " + list.IndexOf(50)); Console.WriteLine("Index of 100: " + list.IndexOf(100)); Console.WriteLine("Index of 200: " + list.IndexOf(200)); Console.WriteLine("Binary search for 50: " + list.BinarySearch(50)); Console.WriteLine("Binary search for 100: " + list.BinarySearch(100)); Console.WriteLine("Binary search for 200: " + list.BinarySearch(200)); } } } ``` 在测试类中,我们首先创建一个长度为10的`LinearSearchList`实例,并随机初始化其中的元素。然后,我们测试`IndexOf`方法和`BinarySearch`方法,它们分别使用顺序查找和二分查找算法在列表中查找特定元素的索引。我们可以在控制台上看到输出结果,以确保这些算法的正确性。

相关推荐

最新推荐

recommend-type

Oracle数据库表中字段顺序的修改方法

问题就是当设计好表结构之后,后期如果需要往表中增加字段,默认会把该字段放到表的最后,并且字段特别多而我们又想把有关联性的字段放在一起,这时就要修改字段顺序。在修改顺序之前,我们先来看看Oracle数据库表中...
recommend-type

【深度学习入门】Paddle实现人脸检测和表情识别(基于TinyYOLO和ResNet18)

【深度学习入门】Paddle实现人脸检测和表情识别(基于YOLO和ResNet18)一、先看效果:训练及测试结果:UI 界面及其可视化:二、AI Studio 简介:平台简介:创建项目:三、创建AI Studio项目:创建并启动环境:下载...
recommend-type

在Keras中CNN联合LSTM进行分类实例

我就废话不多说,大家还是直接看代码吧~ def get_model(): n_classes = 6 inp=Input(shape=(40, 80)) reshape=Reshape((1,40,80))(inp) # pre=ZeroPadding2D... #model.add(Activation('relu')) l1=LeakyReLU
recommend-type

JS端基于download.js实现图片、视频时直接下载而不是打开预览

如果是图片、视频、txt等格式的会直接预览文件,而不是像docx、xlsx一样直接下载,项目中为了功能明确,将预览单独加了一个查看按钮,下载按钮要实现无论什么文件格式都直接下载。 有两种方式处理: 一.后台接口...
recommend-type

华为OD机试D卷 - 用连续自然数之和来表达整数 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。