写一个MyList
首先定义接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _001_线性表
{
interface IListDs<T>
{
int GetLenght();
void Clear();
bool isEmpty();
void Add(T item);
void Insert(T item, int index);
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 _001_线性表
{
/// <summary>
/// 顺序表实现方式
/// </summary>
/// <typeparam name="T"></typeparam>
class SeqList<T> : IListDs<T>
{
private T[] data; //存储数据
private int count;//表示存了多少个数据
public SeqList(int size)//创建数据
{
data = new T[size];
count = ;
}
){}//默认
public T this[int index]
{
get
{
return GetEle(index);
}
}
/// <summary>
/// 添加元素
/// </summary>
/// <param name="item"></param>
public void Add(T item)
{
if (count == data.Length)
{
Console.WriteLine("饱满了");
}
else
{
data[count] = item;
count++;
}
}
/// <summary>
/// 清空
/// </summary>
public void Clear()
{
count = ;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T Delete(int index)
{
T temp = data[index];
&& index <= count - )
{
; i < count; i++)
{
data[i - ] = data[i];//把数据向前移动
}
count--;
return temp;
}
else
{
return default(T);
Console.WriteLine("不存在索引");
}
}
/// <summary>
/// 获取元素
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T GetEle(int index)
{
&& index <= count - )
{
return data[index];
}
else
{
Console.WriteLine("索引不存在");
return default(T);
}
}
/// <summary>
/// 取得数据得长度
/// </summary>
/// <returns></returns>
public int GetLenght()
{
return count;
}
/// <summary>
/// 插入
/// </summary>
/// <param name="item"></param>
/// <param name="index"></param>
public void Insert(T item, int index)
{
&& index <= count - )
{
for (int i = count; i >= index; i--)
{
data[i + ] = data[i];
}
data[index] = item;
count++;
}
else
{
Console.WriteLine("不存在索引");
}
}
public bool isEmpty()
{
;
}
/// <summary>
/// 数据的位置
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public int Locate(T value)
{
; i < count; i++)
{
if (i.Equals(value))
{
return i;
}
}
;
}
}
}
测试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _001_线性表
{
class Program
{
static void Main(string[] args)
{
SeqList<string> seqList = new SeqList<string>();
seqList.Add(");
seqList.Add("1zxc");
seqList.Add("asd3");
Console.WriteLine(seqList[]);
Console.WriteLine(seqList.GetEle());
seqList.Insert();
; i < seqList.GetLenght(); i++)
{
Console.Write(seqList[i]+" ");
}
Console.WriteLine();
seqList.Delete();
; i < seqList.GetLenght(); i++)
{
Console.Write(seqList[i] + " ");
}
Console.WriteLine();
seqList.Clear();
Console.WriteLine(seqList.GetLenght());
}
}
}
写一个MyList的更多相关文章
- 用C#写一个函数,在一个数组中找出随意几个值相加等于一个值 与迭代器对比
算法!用C#写一个函数,在一个数组中找出随意几个值相加等于一个值比如,数组{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20} 要找出那些数相加等 ...
- 使用golang写一个redis-cli
使用golang写一个redis-cli 0. redis通信协议 redis的客户端(redis-cli)和服务端(redis-server)的通信是建立在tcp连接之上, 两者之间数据传输的编码解 ...
- 用过消息队列?Kafka?能否手写一个消息队列?懵
是否有同样的经历?面试官问你做过啥项目,我一顿胡侃,项目利用到了消息队列,kafka,rocketMQ等等. 好的,那请开始你的表演,面试官递过一支笔:给我手写一个消息队列!!WHAT? 为了大家遇到 ...
- 怎样写一个webpack loader
div{display:table-cell;vertical-align:middle}#crayon-theme-info .content *{float:left}#crayon-theme- ...
- 请写一个php函数,可以接受任意数量的参数
请写一个php函数,可以接受任意数量的参数 这是一道面试题.怎么写这个函数呢? function fun(......) { } ----------------------------------- ...
- 学记:为spring boot写一个自动配置
spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...
- 自己写一个 jQuery 插件
我知道这一天终将会到来,现在,它来了. 需求 开发 SharePoint 的 CSOM 应用时,经常需要在网页上输出一些信息. 这种需求和 alert 的弹窗.F12 的断点查看信息的场景是不一样的: ...
- 深入浅出React Native 3: 从零开始写一个Hello World
这是深入浅出React Native的第三篇文章. 1. 环境配置 2. 我的第一个应用 将index.ios.js中的代码全部删掉,为什么要删掉呢?因为我们准备从零开始写一个应用~学习技术最好的方式 ...
- 【转】用C写一个简单病毒
[摘要]在分析病毒机理的基础上,用C语言写了一个小病毒作为实例,用TURBOC2.0实现. [Abstract] This paper introduce the charateristic of t ...
随机推荐
- easyui datagrid显示进度条控制操作
在当我们需要控制时间前台实际项目页面datagrid显示进度条的数据加载时运行,和datagrid默认情况下只在有url加载运行时的数据显示方式的进度条.下面的代码手动控制: 打开一个进度条: $(' ...
- Windows注册表的基本知识及应用
转帖:Windows注册表的基本知识及应用 2009-12-23 11:30:56 分类: Windows注册表的基本知识及应用 一.注册表的重要性 在DOS年代,对计算机的内存管理及系统配置主要通 ...
- C#自带组件
C#自带组件 在项目正式上线后,如果出现错误,异常,崩溃等情况 我们往往第一想到的事就是查看日志 所以日志对于一个系统的维护是非常重要的 贯穿所有的日志系统 日志系统,往往是贯穿一个程序的所有代码的; ...
- Java类之间的关联关系(转载)
Java类之间的关联关系 UML类图中的关系分为四种:泛化.依赖.关联.实现:关联关系又可以细化为聚合和组合. 一.泛化(Generalization) 泛化是父类和子类之间的关系,子类继承父类的所有 ...
- Design Thinking BrainWalk
Design Thinking Design Thinking Workshop @ Agile Tour 2013 Shanghai 摘要: 设计思维工作坊上周日在2013年敏捷之旅上海站,引导 ...
- Linux网络编程(二)
Linux网络编程(二) 使用多进程实现服务器并发访问. 采用多进程的方式实现服务器的并发访问的经典范例. 程序实现功能: 1.客户端从标准输入读入一行文字,发送到服务器. 2.服务器接收到客户端发来 ...
- start running 开始跑步减肥
begin 两个月前,逛超市的时候站在体重秤上称了称,一直以为自己体重很正常(BMI<25,虽然也不轻~~~),结果直接迈过超重,奔着肥胖跑去了(BMI>30,BMI计算器 http:// ...
- 联想E430Cwindow8系统换成win7
垃圾win8系统各种不习惯,尤其无线网络老是自己断掉,忍无可忍只能换成win7,之前换过一次,没有成功,记得是提示硬盘模式要由GPT换成MBR. 这次换系统采用了PE里直接分区,格式化所有数据,然后g ...
- PHP:执行模型和内存模型
PHP:执行模型和内存模型 背景 对于任何一种语言,了解其执行模型和内存模型都是有意义的,本文中的内容不见得正确,请多批评. 执行模型 每个请求都是一个独立的PHP进程,两个请求之间会完全隔离,会话和 ...
- 单一职责原则SRP
定义: There should nerver be more then one reason for a class to change. 优点: 1.类的复杂性降低,实现什么职责都有清晰明确的定义 ...