1,线性表接口
3,测试线性表
4,测试结构:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 线性表
{
/// <summary>
/// 线性表接口
/// </summary>
/// <typeparam name="T"></typeparam>
interface ISeqList<T>
{
/// <summary>
/// 得到顺序表的长度
/// </summary>
/// <returns></returns>
int GetLength();
/// <summary>
/// 添加元素
/// </summary>
/// <param name="item"></param>
void Add(T item);
/// <summary>
/// 插入元素
/// </summary>
/// <param name="item"></param>
/// <param name="index"></param>
void Insert(T item, int index);
/// <summary>
/// 删除元素"即 删除第index+1个元素"
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
bool Delete(int index);
/// <summary>
/// 顺序表是否为空
/// </summary>
bool IsEmpty();
/// <summary>
/// 清空顺序表
/// </summary>
void Clear();
/// <summary>
/// 得到元素
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
T GetElement(int index);
/// <summary>
/// 根据索引得到对应的数据
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
T this[int index] { get; }
/// <summary>
/// 得到所有值为value的索引
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
List<int> GetIndex(T value);
}
}
2,实现线性表接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 线性表
{
/// <summary>
/// 顺序表的实现
/// </summary>
/// <typeparam name="T"></typeparam>
class SeqList<T> : ISeqList<T>
{
private T[] data;//存储数据
private int count = 0;//数据的个数
public SeqList(int size)
{
data = new T[size];
count = 0;
}
public SeqList()
: this(10)
{
}
public int GetLength()
{
return count;
}
public void Add(T item)
{
if (count == data.Length)//数组已满
{
Console.WriteLine("当前数组已满,不能再添加数据");
}
else
{
data[count] = item;
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 Delete(int index)
{
if (index >= 0 && index < count)
{
for (int i = index; i < count; i++)
{
data[i] = data[i + 1];
}
count--;
return true;
}
else
{
Console.WriteLine("索引超出了线性表的有效范围 ");
return false;
}
}
public bool IsEmpty()
{
return count == 0;
}
public void Clear()
{
count = 0;
}
public T GetElement(int index)
{
if (index >= 0 && index <= (count - 1))
{
return data[index];
}
else
{
Console.WriteLine("索引不存在");
return default(T);
}
}
//属性
public T this[int index]
{
get
{
if (index >= 0 && index <= (count - 1))
{
return data[index];
}
else
{
Console.WriteLine("索引不存在");
return default(T);
}
}
}
public List<int> GetIndex(T value)
{
List<int> index = new List<int>();//存储值为value的所有索引
for (int i = 0; i < count; i++)
{
if (value.Equals(data[i]))
{
index.Add(i);
}
}
return index;
}
}
}
3,测试线性表
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 线性表
{
class Program
{
static void Main(string[] args)
{
ISeqList<char> list = new SeqList<char>();
list.Add('a');
list.Add('b');
list.Add('c');
list.Add('d');
list.Add('b');
list.Add('b');
list.Add('e');
Console.WriteLine("线性表中的数据有:");
for (int i = 0; i < list.GetLength(); i++)
{
Console.Write(" " + list.GetElement(i));
}
Console.WriteLine();
Console.WriteLine("值为‘b’的索引是:");
List<int> index = list.GetIndex('b');
foreach (int i in index)
{
Console.Write(" " + i);
}
Console.WriteLine();
Console.WriteLine("删除c之后的线性表:");
list.Delete(5);
for (int i = 0; i < list.GetLength(); i++)
{
Console.Write(" " + list.GetElement(i));
}
Console.WriteLine();
Console.WriteLine("插入f之后的线性表:");
list.Insert('f', 4);
for (int i = 0; i < list.GetLength(); i++)
{
Console.Write(" " + list.GetElement(i));
}
Console.ReadKey();
}
}
}
4,测试结构: