C#中的线性表

什么是线性表

线性表是最简单、最基本、最常用的数据结构。线性表是线性结构的抽象(Abstract),线性结构的特点是结构中的数据元素之间存在一对一的线性关系。这种一对一的关系指的是数据元素之间的位置关系,即:(1)除第一个位置的数据元素外,其它数据元素位置的前面都只有一个数据元素;(2)除最后一个位置的数据元素外,其它数据元素位置的后面都只有一个元素。也就是说,数据元素是一个接一个的排列。因此,可以把线性表想象为一种数据元素序列的数据结构。

线性表就是位置有先后关系,一个接着一个排列的数据结构。

C#提供了一个非泛型接口IList 接口中的项是object,实现了lList解扣子的类有ArrayList,ListDictionary,StringCollection,StringDictionary.

c#2.0提供了泛型的IList<T>接

c#1.1 提供了一个非泛型接lList接接,实现了List<T>接口的类有List<T>

使用List<>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{internal class Program{static void Main(string[] args){//1.使用BCL中的List线性表List<string> list = new List<string>();list.Add("123");//索引为0list.Add("456");//索引为1}}
}
​

线性表的接口定义

public interface IListDS<T> {
int GetLength(;//求长度
void Clear(;//清空操作
​
bool IsEmpty0;//判断线性表是否为空
void Append(T item);//附加操作
void Insert(T item,int i);//插八操作
T Delete(int i);//刪除操作
T GetElem(int i);//取表元
​
int Locate(T value);//按值查找
​
}

顺序表

自定义自己实现List

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{internal interface IlistDS<T>{int GetLength();void Clear();bool IsEmpty();void Add(T item);void Insert(int index, T item);T Delete(int index);T this[int index] {  get; }T GetEle(int index);int Locate(T value);}
}
​
​
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{internal class SeqList<T> : IlistDS<T>{private T[] data;//用来存储数据private int count = 0;//表示存了多少个数据
​
​public SeqList(int size)//size就是最大容量{data = new T[size];count = 0;}public SeqList() : this(10)//默认构造函数的大小为10{
​}public T this[int index]{get { return GetEle(index); }}
​public void Add(T item){if (count == data.Length)//说明当前数组已经存满{Console.WriteLine("当前顺序表已经存满,不允许再存");}else{data[count] = item;count++;}}
​public void Clear(){count = 0;}
​public T Delete(int index){T temp = data[index];for (int i = index + 1; i < count; i++){data[i - 1] = data[i];//把数据向前移动}count--;return temp;}
​public T GetEle(int index){if (index >= 0 && index <= count - 1)//说明所以存在{return data[index];}else{Console.WriteLine("索引不存在");return default(T);}}/// <summary>/// 取得数据的个数/// </summary>/// <returns></returns>/// <exception cref="NotImplementedException"></exception>public int GetLength(){return count;}
​public void Insert(int index, T item){for (int i = count - 1; i >= index; i--){data[i + 1] = data[i];}data[index] = item;count++;}
​public bool IsEmpty(){return (count == 0);}
​public int Locate(T value){for (int i = 0; i < count; ++i){if (data[i].Equals(value)){return i;}}return -1;}}
}
​
​
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{internal class Program{static void Main(string[] args){SeqList<string> list = new SeqList<string>();list.Add("123");list.Add("456");list.Add("789");Console.WriteLine(list.GetEle(0));Console.WriteLine(list[0]);list.Insert(1,"777");for (int i = 0; i < list.GetLength(); i++){Console.Write(list[i]+" ");  }Console.WriteLine();list.Delete(0);for (int i = 0; i < list.GetLength(); i++){Console.Write(list[i] + " ");}list.Clear();Console.WriteLine();Console.WriteLine(list.GetLength());}}
}
​

链表

自定义代码实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{/// <summary>/// 单链表的结点/// </summary>/// <typeparam name="T"></typeparam>internal class Node<T>{private T data; //存储数据private Node<T> next;//指针 用来指向下一个元素
​public Node(){data = default(T);next = null;}
​
​public Node(T value){data = value;next = null;}public Node(T value, Node<T> next){this.data = value;this.next = next;}
​public Node(Node<T> node){this.next = next;
​
​}public T Data{get { return data; }set { data = value; }}
​public Node<T> Next{get { return next; }set {  next = value; }}
​}
}
​
​
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{internal interface IlistDS<T>{int GetLength();void Clear();bool IsEmpty();void Add(T item);void Insert(int index, T item);T Delete(int index);T this[int index] {  get; }T GetEle(int index);int Locate(T value);}
}
​
​
​
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{internal class LinkList<T> : IlistDS<T>{private Node<T> head;public LinkList(){head = null;}
​
​public T this[int index]{get{Node<T> temp = head;for (int i = 1; i <= index; i++){temp = temp.Next;
​}return temp.Data;}}public void Add(T item){Node<T> newnode = new Node<T>(item);//根据新的数据创建一个新的节点//如果头节点为空,那么这个新的节点就是头节点if (head == null){head = newnode;}else{//把新来的节点放在链表的尾部//要访问到链表的尾节点Node<T> temp = head;while (true){if (temp.Next != null){temp = temp.Next;}else{break;}}temp.Next = newnode;//把新来的节点放在链表的尾部
​}
​}
​public void Clear(){head = null;}
​public T Delete(int index){T data = default(T);if (index == 0) //删除头节点 {data = head.Data;head = head.Next;}else{Node<T> temp = head;for (int i = 1; i <= index - 1; i++){temp = temp.Next;
​}Node<T> perNode = temp;Node<T> currentNode = temp.Next;data = currentNode.Data;Node<T> nextNode = temp.Next.Next;perNode.Next = nextNode;}return data;}
​public T GetEle(int index){return this[index];}
​public int GetLength(){if (head == null){
​return 0;}Node<T> temp = head;int count = 1;while (true){if (temp.Next != null){count++;temp = temp.Next;}else { break; }
​}return count;}
​public void Insert(int index, T item){Node<T> newNode = new Node<T>(item);if (index == 0)//插入头节点{newNode.Next = head;head = newNode;}else{Node<T> temp = head;for (int i = 1; i <= index - 1; i++){//让temp向后移动一个位置temp = temp.Next;}
​Node<T> perNode = temp;Node<T> currentNode = temp.Next;perNode.Next = newNode;newNode.Next = currentNode;}}public bool IsEmpty(){return head == null;}
​public int Locate(T value){Node<T> temp = head;if (temp == null){return - 1;}else{int index = 0;while (true){if (temp.Data.Equals(value)){return index;}else{if(temp.Next == null){temp=temp.Next;}else{break;}}}return -1;}}}
}
​
​
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{internal class Program{static void Main(string[] args){LinkList<string> list = new LinkList<string>();list.Add("123");list.Add("456");list.Add("789");Console.WriteLine(list.GetEle(0));Console.WriteLine(list[0]);list.Insert(1,"777");for (int i = 0; i < list.GetLength(); i++){Console.Write(list[i]+" ");  }Console.WriteLine();list.Delete(0);for (int i = 0; i < list.GetLength(); i++){Console.Write(list[i] + " ");}list.Clear();Console.WriteLine();Console.WriteLine(list.GetLength());}}
}
​

双向链表

循环链表

课程:        201-线性表介绍List-T_哔哩哔哩_bilibili

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

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

相关文章

Kotlin协程最佳实践

使用合适的作用域&#xff1a; 避免使用GlobalScope&#xff0c;因为它的生命周期是整个应用程序&#xff0c;可能会导致内存泄漏。相反&#xff0c;使用与组件生命周期绑定的CoroutineScope&#xff0c;例如在Android中使用lifecycleScope。 管理协程的生命周期&#xff1a; …

代码随想录训练营【贪心算法篇】

贪心 注&#xff1a;本文代码来自于代码随想录 贪心算法一般分为如下四步&#xff1a; 将问题分解为若干个子问题找出适合的贪心策略求解每一个子问题的最优解将局部最优解堆叠成全局最优解 这个四步其实过于理论化了&#xff0c;我们平时在做贪心类的题目 很难去按照这四步…

深潜数据海洋:Java文件读写全面解析与实战指南

在软件开发的世界里&#xff0c;文件如同沉默的守护者&#xff0c;承载着程序与现实世界的交流。Java语言&#xff0c;以其强大的文件处理能力&#xff0c;为我们提供了丰富的工具箱&#xff0c;让数据的读写变得既优雅又高效。本文将带你从零开始&#xff0c;逐步深入Java文件…

掌握Python中的文件序列化:Json和Pickle模块解析

Python 文件操作与管理&#xff1a;Open函数、Json与Pickle、Os模块 在Python中&#xff0c;文件是一个重要的数据处理对象。无论是读取数据、保存数据还是进行数据处理&#xff0c;文件操作都是Python编程中不可或缺的一部分。本文将详细介绍Python中文件操作的几种常用方法&…

ImportError: cannot import name ‘packaging‘ from ‘pkg_resources‘

降低setuptools版本 pip install setuptools69.5.1https://github.com/aws-neuron/aws-neuron-sdk/issues/893

阿尔泰科技利用485模块搭建自动灌溉系统实现远程控制

自动灌溉系统又叫土壤墒情监控系统&#xff0c;土壤墒情监控系统主要实现固定站无人值守情况下的土壤墒情数据的自动采集和无线传输&#xff0c;数据在监控中心自动接收入库&#xff1b;可以实现24小时连续在线监控并将监控数据通过有线、无线等传输方式实时传输到监控中心生成…

Express+mysql单表分页条件查询

声明&#xff08;自己还没测试过&#xff0c;只提供大概逻辑&#xff0c;什么多表连接查询可以在原基础上添加&#xff09; class /*** param connection Express的mysql数据库链接对象* current 当前页* pageSize 一页显示行数* where [{key:id,operator:,value15}], key查询…

open3d:ransac分割多个平面(源码)

1、背景介绍 随机采样一致性算法(RANSAC Random Sample Consensus)是一种迭代的参数估计算法,主要用于从包含大量噪声数据的样本中估计模型参数。其核心思想是通过随机采样和模型验证来找到数据中最符合模型假设的点。因此,只要事先给定要提取的参数模型,即可从点云中分割…

[rustlings]13_error_handling

errors6 这一个就是在Err(E)中加了点手脚,就是Err(E)中E的类型也是一个Err类型. 这里是创建了一个新的Err类型,Err类型中有两种不同的枚举值.对于不同的枚举值代表两种不同的错误. // Using catch-all error types like Box<dyn Error> isnt recommended for // library…

【HarmonyOS】HarmonyOS NEXT学习日记:四、布局与容器组件

【HarmonyOS】HarmonyOS NEXT学习日记&#xff1a;四、布局与容器组件 学习了基础组件之后&#xff0c;想要利用基础组件组装成一个页面&#xff0c;自然就要开始学习布局相关的知识。我理解的ArkUI的布局分为两个部分 一、组件自身的通用属性&#xff0c;诸如weight、height、…

Linux 下的项目开发:从入门到精通

在 Linux 系统上开发项目是一种常见且高效的实践。Linux 提供了强大的工具和环境&#xff0c;使得开发过程更加流畅。本文将带你了解如何在 Linux 下进行项目开发&#xff0c;从环境搭建到代码管理&#xff0c;再到最终的部署。 一、环境搭建 1.1 安装 Linux 发行版 首先&am…

加密软件有什么用?五款电脑文件加密软件推荐

加密软件对于个人和企业来说至关重要&#xff0c;尤其是在2024年这样一个高度数字化的时代&#xff0c;数据安全变得尤为重要。 数据保护&#xff1a;加密软件可以保护敏感信息不被未经授权的人访问。这包括个人数据、财务记录、健康信息、企业机密等。 防泄漏&#xff1a;防…

HarmonyOS工程目录结构

应用级配置文件app.json5 应用唯一标识、版本号、应用图标、应用名称等信息 模块级配置文件module.json5 oh-package.json5 三方库的管理 其他配置 用于编译构建&#xff0c;包括构建配置文件、编译构建任务脚本、混淆规则文件、依赖的共享包信息等。 build-profile.json…

用Wireshark观察IPsec协议的通信过程

目录 一、配置本地安全策略 二、启动Wireshark&#xff0c;设置过滤器&#xff0c;开始捕获 1. 主模式 2. Quick mode 三、心得体会 1. 碰到的问题和解决办法 2. 心得 一、配置本地安全策略 配置好IPsec如下&#xff1a; 由于在windows server2008安装wireshark失败&…

常见的排序算法,复杂度

稳定 / 非稳定排序&#xff1a;两个相等的数 排序前后 相对位置不变。插入排序&#xff08;希尔排序&#xff09;&#xff1a; 每一趟将一个待排序记录&#xff0c;按其关键字的大小插入到已排好序的一组记录的适当位置上&#xff0c;直到所有待排序记录全部插入为止。稳定&…

Android IjkPlayer内核编译记(一)so库编译使用

转载请注明出处&#xff1a;https://blog.csdn.net/kong_gu_you_lan/article/details/140528831 本文出自 容华谢后的博客 0.写在前面 最近在搞RTMP协议直播拉流的功能&#xff0c;使用了B站开源的IjkPlayer作为播放器内核&#xff0c;在网络不好的情况下延迟会比较高&#xf…

网络安全防御【防火墙双机热备带宽管理综合实验】

目录 一、实验拓扑图 二、实验要求 三、实验思路&#xff1a; 四、实验步骤&#xff1a; 1、FW3的网络相关配置&#xff1a; 2、FW1的新增配置&#xff1a; 3、交换机LSW6&#xff08;总公司&#xff09;的新增配置&#xff1a; 4、双机热备技术配置&#xff08;双机热…

Windows环境Apache配置解析PHP,以及配置虚拟主机详解

1. 安装 Apache 和 PHP 确保你已经安装了 Apache Web 服务器和 PHP。你可以从官方网站下载它们的 Windows 版本&#xff1a; Apache HTTP ServerPHP 2. 配置 Apache 配置 httpd.conf 文件 找到你的 Apache 安装目录下的 conf 文件夹中的 httpd.conf 文件&#xff0c;使用文…

甲骨文闲置ARM实例防回收的方法

前几日挖了个大坑&#xff0c;今天补一下&#xff0c;谈谈甲骨文闲置实例如何防止回收。 回收原则 2022年11月16日 Oracle添加声明&#xff1a; 从 2022 年 11 月 24 日开始&#xff0c;您闲置的 Always Free 计算实例可能会停止。巴拉巴拉&#xff0c;您还可以随时升级您的帐…

【python】练习 11.3:雇员 编写⼀个名为 Employee 的类,其 __init__() ⽅法接受名、姓和年薪,并将它们都存储在属性中。

练习 11.3&#xff1a;雇员 编写⼀个名为 Employee 的类&#xff0c;其 init() ⽅法接受名、姓和年薪&#xff0c;并将它们都存储在属性中。 要求 编写⼀个名为give_raise() 的⽅法&#xff0c;它默认将年薪增加 5000 美元&#xff0c;同时能够接受其他的年薪增加量。为 Empl…