C#数据结构二:顺序表Sequence List
线性表基本操作包括插入操作、删除操作、查找、清空等。
http://www.cnblogs.com/walkingp/default.html?page=4
线性表的分类
按线性表的元素结构分,可将线性表分为:
1、顺序表:将线性表中的结点按逻辑顺序依次存放在一组地址的存储单元中;
2、链表
1)单向链表:链表的链接方向是单向,对链表的访问要通过顺序读取从头部开始;
2)双向链表:其每个数据结点中都有两个指针,分别指向直接后继和直接前驱。
3)循环链接:其最后一个结点指向头结点,形成一个环;从循环链表中的任何一个结点出发都能找到任何其他结点。
========================================================================
/// <summary>
/// 顺序表
/// </summary>
public class SequenceList
{
public SequenceList()
{
SeqList<int> seqList = new SeqList<int>(8);
seqList.Append(21);
seqList.Append(22);
seqList.Append(23);
Console.WriteLine("Inital the sequence list");
for (int i = 0; i < seqList.Length; i++)
{
Console.WriteLine(seqList[i]);
}
Console.WriteLine("Insert method sample:");
seqList.Insert(20, 0);
seqList.Insert(24, 4);
for (int i = 0; i < seqList.Length; i++)
{
Console.WriteLine(seqList[i]);
}
Console.WriteLine("Delete method sample:");
seqList.Del(0);
for (int i = 0; i < seqList.Length; i++)
{
Console.WriteLine(seqList[i]);
}
Console.WriteLine("The 2st item is:{0}", seqList.getElem(1));
Console.WriteLine("The position of value 23 is:{0}", seqList.Locate(23));
Console.WriteLine("Empty the sequence list");
seqList.Clear();
Console.WriteLine("The length of the sequence list is {0}", seqList.Length);
}
}
/// <summary>
/// 顺序表
/// <typeparam name="T"></typeparam>
class SeqList<T> {
T[] datas;
int max;
int last;
public SeqList(int max) {
this.max = max;
datas = new T[max];
last = -1;
}
/// <summary>
/// 是否为null
/// </summary>
public bool IsEmpty()
{
return last == -1;
}
/// <summary>
/// 是否Full
/// </summary>
public bool IsFull()
{
return last == max-1;
}
/// <summary>
/// 插入
/// </summary>
/// <param name="item"></param>
public bool Insert(T item,int pos)
{
if (IsFull()) {
Console.WriteLine("集合满了");
return false;
}
if (pos > last+1 || pos < 0) {
Console.WriteLine("位置不正确,应该插入已存在的位置");
return false;
}
if (pos == last + 1){}
else {
for (int upper = last; upper >= pos; upper--) {
datas[upper + 1] = datas[upper];
}
}
datas[pos] = item;
++last;
return true;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="pos"></param>
public T Del(int pos)
{
T temp = default(T);
if (IsEmpty()) {
Console.WriteLine("集合为null");
return temp;
}
if (pos > last || pos < 0)
{
Console.WriteLine("位置不正确,应该插入已存在的位置");
return temp;
}
if (pos == last)
temp = datas[pos];
else
{
temp = datas[pos];
for (int upper = pos; upper < last; upper++)
{
datas[upper] = datas[upper + 1];
}
}
last--;
return temp;
}
/// <summary>
/// 长度
/// </summary>
public int Length {
get { return last+1; }
}
public void Append(T item){
if (IsFull()) {
Console.WriteLine("已经满了");
return;
}
datas[++last] = item;
}
public void Clear(){
last = -1;
}
public T this[int index] {
get{return datas[index];}
set{datas[index] = value;}
}
/// <summary>
/// 获取某一位置元素
/// </summary>
public T getElem(int pos)
{
if (IsEmpty() || pos < 0 || pos > last) {
Console.WriteLine("当前位置{0}没有值");
return default(T);
}
return datas[pos];
}
/// <summary>
/// 根据值找位置
/// </summary>
public int Locate(T value) {
if (IsEmpty()) {
Console.WriteLine("集合没有数据");
return -1;
}
int index = 0;
for (index = 0; index <= last; ++index) {
if (value.Equals(datas[index])) break;
}
if (index > last)
return -1;
return index;
}
}
浙公网安备 33010602011771号