C#算法与数据结构之线性结构
线性结构是什么?
线性结构是一种数据结构,它有一对一的关系,就像一个长对,一个接一个,特点是,除第一个元素和最后一个元素外,其它元素前后只有一个元素。
简单示例1:
static void Main(string[] args)
{
//使用BCL中的List线性表
List<string> strList = new List<string>();
strList.Add("123");
strList.Add("456");
strList.Add("789");
Console.WriteLine(strList[1]);
Console.WriteLine(strList.Count);
strList.Remove("123"); //去除某一元素
Console.WriteLine(strList[1]);
Console.WriteLine(strList.Count); }
输出为:456
3
789
2
线性表实现方式
顺序表:连续排放
单链表:
双向链表
循环链表
自设一个List<>:
首先创建一个接口,然后实现接口得所有方法,完程自建List功能:
class SeqList<T>:IListDS<T>
{
private T[] data; //存储数据
private int count = ; public SeqList(int size)
{
data = new T[size];
count = ;
}
public SeqList():this() //默认构造函数10
{ }
public void Add(T item) //添加物件
{
if(count == data.Length)
{
Console.WriteLine("空间已存满");
}
else
{
data[count] = item;
count++;
}
}
//取得数据个数 public int GetLength() //得到长度
{
return count;
} public T GetEle(int index) //返回索引
{
if(index>=&&index<=count-)
{
return data[index];
}
else
{
Console.WriteLine("索引不存在");
return default(T);
}
}
public void Clear()
{
count = ;
}
public bool IsEmpty()
{
return count == ;
}
public void Insert(T item,int index)
{ for(int i=count-;i>=count;i--)
{
data[i + ] = data[i];
}
data[index] = item;
count++;
}
public T Delete(int index)
{
T temp = data[index];
for(int i=index+;i<count;i++)
{
data[i - ] = data[i];
}
count--;
return temp;
}
public T this[int index]
{
get { return GetEle(index); }
}
public int Locate(T value)
{
for(int i=;i<count;i++)
{
if(data[i].Equals(value))
{
return i;
}
}
return -;
} }
测试:
static void Main(string[] args)
{
SeqList<string> seqList = new SeqList<string>();
seqList.Add("");
seqList.Add("");
seqList.Add("");
Console.WriteLine(seqList.GetEle());
Console.WriteLine(seqList[]);
seqList.Insert("",);
for(int i=;i<seqList.GetLength();i++)
{
Console.WriteLine(seqList[i] + ".");
}
seqList.Clear();
Console.WriteLine(seqList.GetLength());
Console.ReadKey(); }
结果:
456
456
123.
666.
456.
789.
0 结果OK,功能实现
单链表: data next 示图: 
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> next)
{
this.next = next;
} public T Data
{
get { return data; }
set { data = value; } }
public Node<T> Next
{
get { return next; }
set { next = value; }
} }
class LinkList<T>:IListDS<T>
{
private Node<T> head;
public LinkList()
{
head = null;
}
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 Insert(T item, int index)
{
Node<T> newNode = new Node<T>(item);
if(index==)
{
newNode.Next = head;
}
else
{
Node<T> temp = head;
for(int i=;i<index-;i++)
{
temp = temp.Next;
}
Node<T> preNode = temp;
Node<T> currentNode = temp;
preNode.Next = newNode;
newNode.Next = currentNode;
}
}
public T Delete(int index)
{
T data = default(T);
if(index == )
{
data = head.Data;
head = head.Next;
}
else
{
Node<T> temp = head;
for (int i = ; i < index - ; i++)
{
temp = temp.Next;
}
Node<T> preNode = temp;
Node<T> currentNode = temp.Next;
data = currentNode.Data;
Node<T> nextNode = temp.Next.Next;
preNode.Next = nextNode; }
return data;
}
public int GetLength()
{
if (head == null)
return ;
Node<T> temp = head;
int count = ;
while(true)
{
if(temp.Next!=null)
{
count++;
temp = temp.Next;
}
else
{
break;
}
}
return count;
}
public void Clear()
{
head = null;
}
public bool IsEmpty()
{
return head == null;
}
public T this[int index]
{
get
{
Node<T> temp = head;
for(int i=;i<=index-;i++)
{
temp = temp.Next;
}
return temp.Data;
}
}
public T GetEle(int index)
{
return this[index];
}
public int Locate(T value)
{
Node<T> temp = head;
if(temp==null)
{
return -;
}
else
{
int index = ;
while(true)
{
if(temp.Data.Equals(value))
{
return index;
}
else
{
if(temp.Next!=null)
{
temp = temp.Next;
}
else
{
break;
}
}
}
return -;
}
}
}
C#算法与数据结构之线性结构的更多相关文章
- Java数据结构和算法(一)线性结构之单链表
Java数据结构和算法(一)线性结构之单链表 prev current next -------------- -------------- -------------- | value | next ...
- Java数据结构和算法(一)线性结构
Java数据结构和算法(一)线性结构 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 线性表 是一种逻辑结构,相同数据类型的 ...
- 算法与数据结构(一) 线性表的顺序存储与链式存储(Swift版)
温故而知新,在接下来的几篇博客中,将会系统的对数据结构的相关内容进行回顾并总结.数据结构乃编程的基础呢,还是要不时拿出来翻一翻回顾一下.当然数据结构相关博客中我们以Swift语言来实现.因为Swift ...
- Python数据结构01 线性结构
栈 实现 后进先出的结构,主要有如下操作 *Stack() *push(item) *pop() *peek() *isEmpty() *size() class Stack(): def __ini ...
- Java数据结构之线性表(2)
从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...
- Java数据结构之线性表
从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...
- java数据结构--线性结构
一.数据结构 数据结构由数据和结构两部分组成,就是将数据按照一定的结构组合起来,这样不同的组合方式有不同的效率,可根据需求选择不同的结构应用在相应在场景.数据结构大致 分为两类:线性结构(如数组,链表 ...
- 算法与数据结构(二) 栈与队列的线性和链式表示(Swift版)
数据结构中的栈与队列还是经常使用的,栈与队列其实就是线性表的一种应用.因为线性队列分为顺序存储和链式存储,所以栈可以分为链栈和顺序栈,队列也可分为顺序队列和链队列.本篇博客其实就是<数据结构之线 ...
- 【算法与数据结构实战】线性表操作-实现A并B,结果放入A中
//数据结构与算法基础题1:线性表操作,实现A并B,结果放入A中 #include "stdafx.h" #include <iostream> #include &l ...
随机推荐
- ViewPager和Fragment中的View的点击事件冲突
ViewPager属于父布局,View属于子布局: 触摸事件是先到父View,再到子View,所以可以让ViewPager取消拦截事件: public class ComposeViewPager e ...
- .Net代码控制PrivateBinPath和ConfigurationFile的位置
.Net的WinForm程序有的时候让人很烦的是,在执行目录下总是一大堆的DLL,配置文件,最少则是个以下,多的时候怕有四五十个吧……,自己程序中的类库,第三方的类库……加载一起让人感觉乱糟糟的,非常 ...
- 将jar包添加到本地maven仓库中
在使用maven依赖添加jar包时,有时会遇到下载不成功的问题,这时需要将jar手动添加到本地的maven仓库中. 准备工作 配置好maven的环境变量 已经下载好的jar包 具体过程 win + R ...
- 《Java从入门到精通》学习总结1
1. Java既是编译型语音,也是解释型语言:先将源代码编译成Java字节码,然后Java虚拟机对Java字节码进行解释运行 2. 使用命令行编译Java源代码时,如果代码中有中文,在编译时需要指定编 ...
- Mapnik 3.0.20编译安装
1. 确定epel安装 yum install -y epel-release 2. 按照<CentOS7.2部署node-mapnik>一文中的步骤,手动安装 gcc-6.2.0 和 b ...
- Ax2009中使用CLR发送邮件
由于Ax2009系统方法SysMailer 发送中文的时候会乱码,一直找不到原因,用.NEt Framwork的类库可以解决中文乱码的问题.static void CKT_DotNetMail(Arg ...
- yarn 常用命令
1.安装 yarn npm install yarn -g 2.卸载yarn npm uninstall yarn -g
- oracle 关于房贷计算过程
create or replace procedure fd(p_bj in number, --贷款本金 p_nll in number, --年利率 p_ns in number, --贷款年数 ...
- 能量模型与softmax和RBM以及正态分布的关联
上面一篇文章中探讨了玻尔兹曼分布的起源: 在不清楚目标的真实分布,也不知道样本分布的时候,假设任意输入与输出组合都是同样可能发生的,这样是最公平,最无偏的先验. 因为无法直接统计出给定任意一种输入x, ...
- CENTOS7上安装MYSQL5.7.21流程
1系统约定安装文件下载目录:/data/softwareMysql目录安装位置:/usr/local/mysql数据库保存位置:/data/mysql日志保存位置:/data/log/mysql 2下 ...