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