using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SeqList
{
public class Program
{
static void Main(string[] args)
{
SeqList seq = new SeqList();
SeqListTypeStudent> list = new SeqListTypeStudent>();
Console.WriteLine("\n********************** 添加二條數(shù)據(jù) ************************\n");
seq.SeqListAddStudent>(list, new Student() { ID = "1", Name = "一線碼農(nóng)", Age = 23 });
seq.SeqListAddStudent>(list, new Student() { ID = "3", Name = "huangxincheng520", Age = 23 });
Console.WriteLine("添加成功");
//展示數(shù)據(jù)
Display(list);
Console.WriteLine("\n********************** 正在搜索Name=“一線碼農(nóng)”的實體 ************************\n");
var student = seq.SeqListFindByKeyStudent, string>(list, "一線碼農(nóng)", s => s.Name);
Console.WriteLine("\n********************** 展示一下數(shù)據(jù) ************************\n");
if (student != null)
Console.WriteLine("ID:" + student.ID + ",Name:" + student.Name + ",Age:" + student.Age);
else
Console.WriteLine("對不起,數(shù)據(jù)未能檢索到。");
Console.WriteLine("\n********************** 插入一條數(shù)據(jù) ************************\n");
seq.SeqListInsert(list, 1, new Student() { ID = "2", Name = "博客園", Age = 40 });
Console.WriteLine("插入成功");
//展示一下
Display(list);
Console.WriteLine("\n********************** 刪除一條數(shù)據(jù) ************************\n");
seq.SeqListDelete(list, 0);
Console.WriteLine("刪除成功");
//展示一下數(shù)據(jù)
Display(list);
Console.Read();
}
///summary>
/// 展示輸出結(jié)果
////summary>
static void Display(SeqListTypeStudent> list)
{
Console.WriteLine("\n********************** 展示一下數(shù)據(jù) ************************\n");
if (list == null || list.ListLen == 0)
{
Console.WriteLine("嗚嗚,沒有數(shù)據(jù)");
return;
}
for (int i = 0; i list.ListLen; i++)
{
Console.WriteLine("ID:" + list.ListData[i].ID + ",Name:" + list.ListData[i].Name + ",Age:" + list.ListData[i].Age);
}
}
}
#region 學生的數(shù)據(jù)結(jié)構(gòu)
///summary>
/// 學生的數(shù)據(jù)結(jié)構(gòu)
////summary>
public class Student
{
public string ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
#endregion
#region 定義一個順序表的存儲結(jié)構(gòu)
///summary>
/// 定義一個順序表的存儲結(jié)構(gòu)
////summary>
public class SeqListTypeT>
{
private const int maxSize = 100;
public int MaxSize { get { return maxSize; } }
//數(shù)據(jù)為100個存儲空間
public T[] ListData = new T[maxSize];
public int ListLen { get; set; }
}
#endregion
#region 順序表的相關(guān)操作
///summary>
///順序表的相關(guān)操作
////summary>
public class SeqList
{
#region 順序表初始化
///summary>
/// 順序表初始化
////summary>
///param name="t">/param>
public void SeqListInitT>(SeqListTypeT> t)
{
t.ListLen = 0;
}
#endregion
#region 順序表的長度
///summary>
/// 順序表的長度
////summary>
///param name="t">/param>
///returns>/returns>
public int SeqListLenT>(SeqListTypeT> t)
{
return t.ListLen;
}
#endregion
#region 順序表的添加
///summary>
///順序表的添加
////summary>
///param name="t">/param>
///returns>/returns>
public bool SeqListAddT>(SeqListTypeT> t, T data)
{
//防止數(shù)組溢出
if (t.ListLen == t.MaxSize)
return false;
t.ListData[t.ListLen++] = data;
return true;
}
#endregion
#region 順序表的插入操作
///summary>
/// 順序表的插入操作
////summary>
///param name="t">/param>
///param name="n">/param>
///param name="data">/param>
///returns>/returns>
public bool SeqListInsertT>(SeqListTypeT> t, int n, T data)
{
//首先判斷n是否合法
if (n 0 || n > t.MaxSize - 1)
return false;
//說明數(shù)組已滿,不能進行插入操作
if (t.ListLen == t.MaxSize)
return false;
//需要將插入點的數(shù)組數(shù)字依次向后移動
for (int i = t.ListLen - 1; i >= n; i--)
{
t.ListData[i + 1] = t.ListData[i];
}
//最后將data插入到騰出來的位置
t.ListData[n] = data;
t.ListLen++;
return true;
}
#endregion
#region 順序表的刪除操作
///summary>
/// 順序表的刪除操作
////summary>
///param name="t">/param>
///param name="n">/param>
///returns>/returns>
public bool SeqListDeleteT>(SeqListTypeT> t, int n)
{
//判斷刪除位置是否非法
if (n 0 || n > t.ListLen - 1)
return false;
//判斷數(shù)組是否已滿
if (t.ListLen == t.MaxSize)
return false;
//將n處后的元素向前移位
for (int i = n; i t.ListLen; i++)
t.ListData[i] = t.ListData[i + 1];
//去掉數(shù)組最后一個元素
--t.ListLen;
return true;
}
#endregion
#region 順序表的按序號查找
///summary>
/// 順序表的按序號查找
////summary>
///param name="t">/param>
///param name="n">/param>
///returns>/returns>
public T SeqListFindByNumT>(SeqListTypeT> t, int n)
{
if (n 0 || n > t.ListLen - 1)
return default(T);
return t.ListData[n];
}
#endregion
#region 順序表的關(guān)鍵字查找
///summary>
/// 順序表的關(guān)鍵字查找
////summary>
///typeparam name="T">/typeparam>
///typeparam name="W">/typeparam>
///param name="t">/param>
///param name="key">/param>
///param name="where">/param>
///returns>/returns>
public T SeqListFindByKeyT, W>(SeqListTypeT> t, string key, FuncT, W> where) where W : IComparable
{
for (int i = 0; i t.ListLen; i++)
{
if (where(t.ListData[i]).CompareTo(key) == 0)
{
return t.ListData[i];
}
}
return default(T);
}
#endregion
}
#endregion
}