首页 >> WEB开发

C#顺序表-C#数据结构--顺序表(基础入门)

2011-03-11 16:54:00

C#数据结构--顺序表(基础入门):
  1.  using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5.  
  6. namespace ListDS 
  7. { 
  8.     public interface IDS<T> 
  9.     { 
  10.         int Count { get; }                     //求长度  
  11.         void Clear();                           //清空操作  
  12.         bool IsEmpty { get; }              //判断线性表是否为空 
  13.     } 
  14.  
  15.     //定义线性表 
  16.     public interface IListDS<T> : IDS<T> 
  17.     { 
  18.         void Append(T item);                 //附加操作  
  19.         void Insert(T item, int index);     //插入操作  
  20.         T Delete(int index);                    //删除操作 
  21.         T GetElem(int index);                //取表元  
  22.         int Locate(T value);                   //按值查找  
  23.     } 
  24.  
  25.     //定义顺序表 
  26.     public class SeqList<T> : IListDS<T> 
  27.     { 
  28.         private int maxsize; //顺序表的容量 
  29.         private T[] data; //数组,用于存储顺序表中的数据元素 
  30.         private int last; //指示顺序表最后一个元素的位置 
  31.         //索引器 
  32.         public T this[int index] 
  33.         { 
  34.             get 
  35.             { 
  36.                 return data[index]; 
  37.             } 
  38.             set 
  39.             { 
  40.                 data[index] = value; 
  41.             } 
  42.         } 
  43.         //最后一个数据元素位置属性 
  44.         public int Last 
  45.         { 
  46.             get 
  47.             { 
  48.                 return last; 
  49.             } 
  50.         } 
  51.         //容量属性 
  52.         public int Maxsize 
  53.         { 
  54.             get 
  55.             { 
  56.                 return maxsize; 
  57.             } 
  58.             set 
  59.             { 
  60.                 maxsize = value; 
  61.             } 
  62.         }         
  63.         //构造器 
  64.         public SeqList(int size) 
  65.         { 
  66.             data = new T[size]; 
  67.             maxsize = size; 
  68.             last = -1; 
  69.         } 
  70.         //求顺序表的长度 
  71.         public int GetLength() 
  72.         { 
  73.             return last + 1; 
  74.         } 
  75.         //清空顺序表 
  76.         public void Clear() 
  77.         { 
  78.             last = -1; 
  79.         } 
  80.         //判断顺序表是否为空 
  81.         public bool IsEmpty() 
  82.         { 
  83.             if (last == -1) 
  84.             { 
  85.                 return true; 
  86.             } 
  87.             else 
  88.             { 
  89.                 return false; 
  90.             } 
  91.         } 
  92.         public bool IsFull() 
  93.         { 
  94.             if (last == maxsize - 1) 
  95.             { 
  96.                 return true; 
  97.             } 
  98.             else 
  99.             { 
  100.                 return false; 
  101.             } 
  102.         } 
  103.         public void Append(T item) 
  104.         { 
  105.             if (IsFull()) 
  106.             { 
  107.                 Console.WriteLine("List is full"); 
  108.                 return; 
  109.             } 
  110.             data[++last] = item; 
  111.         } 
  112.         public void Insert(T item, int i) 
  113.         { 
  114.             if (IsFull()) 
  115.             { 
  116.                 Console.WriteLine("List is full"); 
  117.                 return; 
  118.             } 
  119.             if (i < 1 || i > last + 2) 
  120.             { 
  121.                 Console.WriteLine("Position is error!"); 
  122.                 return; 
  123.             } 
  124.             if (i == last + 2) 
  125.             { 
  126.                 data[last + 1] = item; 
  127.             } 
  128.             else 
  129.             { 
  130.                 for (int j = last; j >= i - 1; --j) 
  131.                 { 
  132.                     data[j + 1] = data[j]; 
  133.                 } 
  134.                 data[i - 1] = item; 
  135.             } 
  136.             ++last;
  137.         } 
  138.         public T Delete(int index) 
  139.         { 
  140.             T tmp = default(T); 
  141.             if (IsEmpty()) 
  142.             { 
  143.                 Console.WriteLine("List is empty"); 
  144.                 return tmp; 
  145.             } 
  146.             if (index < 1 || index > last + 1) 
  147.             { 
  148.                 Console.WriteLine("Position is error!"); 
  149.                 return tmp; 
  150.             } 
  151.             if (index == last + 1) 
  152.             { 
  153.                 tmp = data[last--]; 
  154.             } 
  155.             else 
  156.             { 
  157.                 tmp = data[index - 1]; 
  158.                 for (int j = index; j <= last; ++j) 
  159.                 { 
  160.                     data[j] = data[j + 1]; 
  161.                 } 
  162.             } 
  163.             --last; 
  164.             return tmp; 
  165.         } 
  166.         public T GetElem(int index) 
  167.         { 
  168.             if (IsEmpty() || (index < 1) || (index > last + 1)) 
  169.             { 
  170.                 Console.WriteLine("List is empty or Position is error!"); 
  171.                 return default(T); 
  172.             } 
  173.             return data[index - 1]; 
  174.         } 
  175.         public int Locate(T value) 
  176.         { 
  177.             if (IsEmpty()) 
  178.             { 
  179.                 Console.WriteLine("List is Empty!"); 
  180.                 return -1; 
  181.             } 
  182.             int i = 0; 
  183.             for (i = 0; i <= last; ++i) 
  184.             { 
  185.                 if (value.Equals(data[i])) 
  186.                 { 
  187.                     break; 
  188.                 } 
  189.             } 
  190.             if (i > last) 
  191.             { 
  192.                 return -1; 
  193.             } 
  194.             return i; 
  195.         } 
  196.         public int Count 
  197.         { 
  198.             get 
  199.             { 
  200.                 return last; 
  201.             } 
  202.         } 
  203.         bool IDS<T>.IsEmpty 
  204.         { 
  205.             get { 
  206.                 if (last == - 1) 
  207.                 { 
  208.                     return true; 
  209.                 } 
  210.                 else 
  211.                 { 
  212.                     return false; 
  213.                 } 
  214.             } 
  215.         } 
  216.     } 
  217.  
  218.  
  219.     class Program 
  220.     {    
  221.        
  222.         static void Main(string[] args) 
  223.         { 
  224.             SeqList <int> list=new SeqList<int> (10); 
  225.             for (int i = 0; i < 8; i++) 
  226.             { 
  227.                 list.Append(i+1);//附加 
  228.                 Console.Write("{0} ", list[i]);//输出 
  229.             } 
  230.            
  231.         } 
  232.     } 
  233. }