线性结构是什么?

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

  简单示例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. Android判断一个点是否在矩形区域内

    个人遇到的问题判断按钮的点击事件还是滑动事件 private boolean button1Down = false; private boolean button2Down = false; pri ...

  2. SpringMVC避免IE执行AJAX,返回JSON出现下载文件

  3. devexpress总结 accordionControl 加载panelcontrol 的快捷方式

    先说保存: UserControl control; private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars. ...

  4. JAVA多线程之线程间的通信方式

    (转发) 收藏 记 周日,北京的天阳光明媚,9月,北京的秋格外肃穆透彻,望望窗外的湛蓝的天,心似透过栏杆,沐浴在这透亮清澈的蓝天里,那朵朵白云如同一朵棉絮,心意畅想....思绪外扬, 鱼和熊掌不可兼得 ...

  5. github 生成配置ssh 秘钥方法详解

    如果安装github成功后,当从本地提交文件到github的时候,提交不成功,报错,可能问题就是你还没有生成ssh秘钥 1.当你提交文件到github,不成功,出现如下的情况,就代表着github上面 ...

  6. [Draft]iOS.ObjC.Pattern.Builder-Pattern

    Builder Pattern in Objective-C Reference 1. The Builder pattern in Objective-C Published on 04 Apr 2 ...

  7. Python-docx 读取word.docx内容

    第一次写博客,也不知道要写点儿什么好,所以就把我在学习Python的过程中遇到的问题记录下来,以便之后查看,本人小白,写的不好,如有错误,还请大家批评指正! 中文编码问题总是让人头疼,想要用Pytho ...

  8. HDU 6161.Big binary tree 二叉树

    Big binary tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  9. python基础之Day23

    1.封装 什么是? 封:明确地把属性隐藏起来 ,对外隐藏,对内开放 申请名称空间,往里面装入一系列名字 /属性(类比 类 和对象   只是装的概念) 为什么要用? __init__往对象里丢属性 封装 ...

  10. HttpWebRequest.AddRange 支持long类型

    很久很久以前,在哪个FAT32格式还流行的年代,文件大小普遍还没超过4G的年代,.Net已经出来了. 而那时候.Net实现的HTTP断点续传协议,还没预料到如此普及(我猜的).那时候的HttpWebR ...