C#数据结构之线性表

一、概念:略

二、用接口的形式表示线性表:

  public interface IListThread<T>

  {

    //线性表中的基本方法

    int Getlength();

    void Clear();

    bool IsEmpty();

    void Append(T item);

    void Insert(T item,int i);

    T Delete(int i);

    T GetElement(T value);

    int Locate(T value);

  }

  (1)、顺序表(地址连续的空间):表中相邻的数据元素在内存中的存储位置也相邻。用SeqList<T>类来实现接口IListThread<T>

  public class SeqList<T>:IListThread<T>

  {

    private int MaxSize;//最大容量

    private T[] data; //用于存放线性表的数据元素

    private int last;  //顺序表最后一个元素的位置

    

    //索引器

    public T this[int index]

    {

      get{return data[index];}

      set{data[index]=value;}

    }

    //获得最后一个元素的位置

    public int Last

    {

      get{return last;}

    }

    //线性表容量

    public int Maxsize

    {

      get{return MaxSize;}

      set{MaxSize=value;}

    }

    //构造函数

    public SeqList(int size)

    {

      data=new T[size];

      MaxSize=size;

      last=-1;

    }

    //长度

    public int GetLength()

    {

      return last+1;

    }

    //清空

    .......

    last=-1;

    //判断是否为空

    public bool IsEmpty()

    {

      if(last==-1)

      {

        return true;

      }

      else

      {

        return false;

      }

    }

    public bool IsFull()

    {

      if(last==MaxSize-1)

      {

        return true;

      }

      else

      {

        return false;

      }

    }

    //追加元素

      public void Append(T item)

    {

      if(IsFull())

      {

        return;

      }

      else

      {

        data[++last]=item;

      }

    }

//追加元素时判断是否满了,插入数据时判断位置、是否已满,

  }

原文地址:https://www.cnblogs.com/jsping/p/2532202.html