数据结构——顺序表 (C#)
1. 顺序表
线性表的顺序存储是指在内存中用一块地址连续的空间依次存放线性表的数据元素,用这种方式存储的线性表叫顺序表。
特征:表中相邻的数据元素在内存中存储位置也相邻。
3. 顺序表的存储
假设顺序表中的每个数据元素占w个存储单元,设第1个数据元素的存储地址为Loc(ai),则有:Loc(ai)= Loc(a1)+(i-1)*w 1<=i<=n 式中的LoC(a1)表示第一个数据元素a1的存储地址,也是顺序表的起始存储地址,称为顺序表的基地址(Base Address)。也就是说,只要知道顺序表的基地址和每个数据元素所占的存储单元的个数就可以求出顺序表中任何一个数 据元素的存储地址。并且,由于计算顺序表中每个数据元素存储地址的时间相同,所以顺序表具有任意存取的特点。(可以在任意位置存取东西)
4. C#语言中的数组在内存中占用的存储空间就是一组连续的存储区域,因此,数组具有任意存取的特点。所以,数组天生具有表示顺序表的数据存储区域的特性。
5. 动手实现一个顺序表:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _顺序表_复习
{
interface IListDS<T>
{
void Add(T item); //向表中添加数据
void Insert(T item, int index); //向表中指定位置插入一个数据
T Delete(int index); //删除表中指定位置的数据,返回值为删除的这个T类型的数据
void Clear(); //清空表
int GetLength(); //得到表长度
bool IsEmpty(); //判断表是否为空
T GetEle(int index); //根据索引得到值
T this[int index]
{
get; //根据索引访问元素
}
int Locate(T value); //根据值得到索引
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _顺序表_复习
{
class SeqList<T>:IListDS<T>
{
private T[] data; //定义一个数组用来存储数据
private int count; //表示存了多少个数据
private int len = 10; //表示数组容量
public SeqList(int size) //传递的size就是最大容量
{
data = new T[size]; //给数组赋值,数组长度为size
count = 0;
len = size; //初始化,传递的size就是数组容量
}
public SeqList() : this(10) //默认构造函数 容量是10
{
}
public T this[int index] //根据索引访问元素
{
get
{
return GetEle(index);
}
}
public T GetEle(int index) //根据索引得到值
{
if (index >= 0 && index <= count - 1) //索引存在
{
return data[index];
}
else
{
Console.WriteLine("索引不存在");
return default(T); //返回T类型的默认值
}
}
public void Add(T item) //向表中添加数据
{
if (count == len) //当count = len 时,顺序表已存满,再添加数据,就会导致数组下标越界
{
Console.WriteLine("顺序表已存满,不允许再存!");
}
else
{
data[count] = item; //count可以作为索引
count++;
}
}
public void Clear() //清空表
{
count = 0;
}
public T Delete(int index) //删除表中指定位置的数据,返回值为删除的这个T类型的数据
{
T temp = data[index];
for (int i = index + 1; i <= count - 1; i++) //被删除数据后面的那一个数据的索引是 index+1,线性表最大索引是count - 1
{
data[i - 1] = data[i]; //被删除数据的 索引index= i-1, 把数据向前移动
}
count--;
return temp;
}
/// <summary>
/// 取得数据的个数
/// </summary>
/// <returns></returns>
public int GetLength()
{
return count;
}
public void Insert(T item, int index) //向表中指定位置插入一个数据
{
for (int i = count - 1; i >= index; i--)
{
data[i + 1] = data[i];
}
data[index] = item;
count++;
}
public bool IsEmpty() //判断表是否为空
{
if (count == 0)
{
return false;
}
return true;
//return count == 0;
}
public int Locate(T value) //根据值得到索引
{
for (int i = 0; i < count; i++) //遍历顺序表
{
if (data[i].Equals(value)) //用Equals方法 ,判断遍历当前值是否等于要查找值
{
return i; //找到返回查找值的索引
}
}
return -1; //找不到返回-1
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _顺序表_复习
{
class Program
{
static void Main(string[] args)
{
SeqList<string> seqList = new SeqList<string>();
seqList.Add("123");
seqList.Add("456");
seqList.Add("789");
Console.WriteLine(seqList.GetLength()); //3
Console.WriteLine();
Console.WriteLine(seqList.GetEle(0)); //123
Console.WriteLine(seqList[1]); //456
seqList.Insert("777", 0); //777 123 456 789
for (int i = 0; i < seqList.GetLength(); i++)
{
Console.Write(seqList[i] + " "); //777 123 456 789
}
Console.WriteLine();
seqList.Delete(0);//123 456 789
for (int i = 0; i < seqList.GetLength(); i++)
{
Console.Write(seqList[i] + " "); //123 456 789
}
Console.WriteLine();
Console.WriteLine(seqList.Locate("123")); //0
Console.WriteLine(seqList.Locate("888")); //-1
Console.WriteLine(seqList.IsEmpty()); //True
Console.WriteLine();
seqList.Clear(); //count = 0
Console.WriteLine(seqList.GetLength()); //0
Console.WriteLine(seqList.IsEmpty()); //False
Console.ReadKey();
}
}
}