线性结构是什么?

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

  简单示例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. HBase数据压缩算法编码探索

    摘要: 本文主要介绍了hbase对数据压缩,编码的支持,以及云hbase在社区基础上对数据压缩率和访问速度上了进行的改进. 前言 你可曾遇到这种需求,只有几百qps的冷数据缓存,却因为存储水位要浪费几 ...

  2. ReentrantLock示例说明

    1.ReentrantLock锁 import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; impor ...

  3. .NET Core PartialView 与 Ajax

    Ajax的核心是XMLHttpRequest对象(XHR),能够以异步方式从服务器获取新数据.开发主要利用Ajax来执行异步刷新和局部视图更新的功能. 而开发常常在前段页面利用JQuery封装的Aja ...

  4. 基于角色的访问控制 (RBAC)权限管理

    RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成“用户-角色- ...

  5. vmware 挂起后不能恢复

    报错:未能锁定主内存文件,还原虚拟机状态时出错 虚拟机目录下有一个文件夹,xxx.vmem.lck,里面的lck文件是很久以前的,把它删掉重新恢复就可以了.

  6. makefile入门-初步了解

    自己开始学习makefile是由于VScode配置工程文件,看别人的配置不是很懂,于是决定入门学习下makefile. 先来说说makefile是做什么用的:makefile可以实现工程的自动化编译, ...

  7. 201621123002《java程序设计》第十三周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系统可以被多个用户通过网 ...

  8. Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分

    D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...

  9. Numpy用户指南

    Numpy是Python语言的一个扩展库,支持大量的维度数组和矩阵运算,此外也针对数组运算提供大量的数学函数库. Mumpy是一个运行速度非常快的数学库,主要用于数组计算,包涵: 1.一个强大的N维数 ...

  10. 第二阶段第九次spring会议

    今天我将对软件进行宠物信息的添加. 清屏功能 private void button5_Click(object sender, EventArgs e) { textBox2.Text = &quo ...