线性结构是什么?

  线性结构是一种数据结构,它有一对一的关系,就像一个长对,一个接一个,特点是,除第一个元素和最后一个元素外,其它元素前后只有一个元素。

  简单示例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#算法与数据结构之线性结构的更多相关文章

  1. Java数据结构和算法(一)线性结构之单链表

    Java数据结构和算法(一)线性结构之单链表 prev current next -------------- -------------- -------------- | value | next ...

  2. Java数据结构和算法(一)线性结构

    Java数据结构和算法(一)线性结构 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 线性表 是一种逻辑结构,相同数据类型的 ...

  3. 算法与数据结构(一) 线性表的顺序存储与链式存储(Swift版)

    温故而知新,在接下来的几篇博客中,将会系统的对数据结构的相关内容进行回顾并总结.数据结构乃编程的基础呢,还是要不时拿出来翻一翻回顾一下.当然数据结构相关博客中我们以Swift语言来实现.因为Swift ...

  4. Python数据结构01 线性结构

    栈 实现 后进先出的结构,主要有如下操作 *Stack() *push(item) *pop() *peek() *isEmpty() *size() class Stack(): def __ini ...

  5. Java数据结构之线性表(2)

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...

  6. Java数据结构之线性表

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...

  7. java数据结构--线性结构

    一.数据结构 数据结构由数据和结构两部分组成,就是将数据按照一定的结构组合起来,这样不同的组合方式有不同的效率,可根据需求选择不同的结构应用在相应在场景.数据结构大致 分为两类:线性结构(如数组,链表 ...

  8. 算法与数据结构(二) 栈与队列的线性和链式表示(Swift版)

    数据结构中的栈与队列还是经常使用的,栈与队列其实就是线性表的一种应用.因为线性队列分为顺序存储和链式存储,所以栈可以分为链栈和顺序栈,队列也可分为顺序队列和链队列.本篇博客其实就是<数据结构之线 ...

  9. 【算法与数据结构实战】线性表操作-实现A并B,结果放入A中

    //数据结构与算法基础题1:线性表操作,实现A并B,结果放入A中 #include "stdafx.h" #include <iostream> #include &l ...

随机推荐

  1. Python开发【第九篇】:进程、线程

    什么是进程(process)? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于,程序是指令的集合,它是进程运行的静态描述文本 ...

  2. Dockerfile的常见命令

    FROM 格式: FROM  <image> 或者  FROM <image>:<tag> FROM指令的功能是为后面的指令提供基础镜像,所以该指令一定是Docke ...

  3. python3作业:模拟登录

    __author__ = "bin007" customer = {}#存储用户信息#处理用户信息文件try: with open('login.txt','r',encoding ...

  4. 19-01【vmware machine】虚拟机无法联网访问

    问题 我本地的虚拟机上没办法访问外网,然后搞了很久很久,最终解决了. 现象 A,电脑持续运行了三天,也没有复杂的环境调整的情况下.我本地的ubuntu(使用VMWare machine创建的),突然没 ...

  5. sqlserver改主键初始ID

  6. Layer 创建 和 lamdba function 创建 和 API GateWay 配置 和 添加依赖

    进入控制台 选择 Lamdba 服务 进入控制面板, 单击右边: 创建函数 然后进入此图界面 添加lamdba 函数 名称 选择运行环境:python 选择角色 选择现有角色, 角色创建 可以参考 无 ...

  7. sftp免密登录

    最近在linux服务器上搭建了一个sftp服务器,在做免密登录时,因为理解的问题,走了些弯路,在此记录一下. 先来说一下服务器端,在服务端的每一个用户的根目录下都有一个.ssh目录,在这个下面放的公私 ...

  8. mongodb的配置文件详解()

    官方地址  https://docs.mongodb.com/manual/reference/configuration-options/#configuration-file 以下页面描述了Mon ...

  9. Kudu基本操作及概念

    Kudu:    针对 Apache Hadoop 平台而开发的列式存储管理器. 使用场景:    适用于那些既有随机访问,也有批量数据扫描的复合场景.    高计算量的场景.    使用了高性能的存 ...

  10. NC 数据库操作

    一.后台数据库操作方法(private端): 1.以下为后台查询方法 BaseDAO dao = new BaseDAO();//只能在private端使用 String querySql=" ...