【C#】【数据结构】001-线性表:顺序表

C#数据结构:顺序表结构

  • 1、自定义顺序表结构
using System.Collections;
using System.Collections.Generic;


/// <summary>
///线性表接口
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IListDS<T>
{
    int MaxSize();

    bool IsEmpty();
    bool IsFull();

    void Add(T item);
    void Insert(T item, int index);
    T Delete(int index);

    int GetLength();

    T this[int index] { get; }
    T GetElem(int index);
    int Locate(T value);

    void Clear();



}

/// <summary>
/// 顺序表
/// </summary>
/// <typeparam name="T"></typeparam>
public class SqeList<T> : IListDS<T>
{
    private T[] data;
    private int last = -1; //表的最后索引,表空是last=-1

    //初始化顺序表
    public SqeList(int size)//size最大容量
    {
        data = new T[size];
        last = -1;
    }

    public SqeList() : this(10)//默认构造函数,容量10
    {

    }

    //表最大容量
    public int MaxSize()
    {
        return data.Length;
    }



    //判空
    public bool IsEmpty()
    {
        return last == -1;
    }

    //判满
    public bool IsFull()
    {
        return last == data.Length - 1;
    }


    //添加操作
    public void Add(T item)
    {
        if (IsFull())
        {
            Debug.LogError("当前顺序表已存满!");
            return;
        }
        data[last + 1] = item;
        last++;
    }


    //插入操作
    public void Insert(T item, int index)
    {
        if (index < 0 || index > last+1)  //合法的插入 0<=index<=last+1
        {
            Debug.LogError("插入index不合法!");
            return;
        }
        if (IsFull())
        {
            Debug.LogError("当前顺序表已存满!");
            return;
        }

        for (int i = last; i >= index; i--)
        {
            data[i + 1] = data[i];//将后面的元素向后移动
        }
        data[index] = item;
        last++;
    }

    //根据index删除表元素
    public T Delete(int index)
    {
        if (index < 0 || index > last)
        {
            Debug.LogError("删除位置index不合法!");
            return default(T);
        }
        T temp = data[index];
        for (int i = index + 1; i <= last; i++)
        {
            data[i - 1] = data[i];//将后面的元素向后移
        }
        last--;
        return temp;
    }

    //表长
    public int GetLength()
    {
        return last + 1;
    }

    //根据索引获得表元素
    public T GetElem(int index)
    {
        if (index >= 0 && index <= last)
        {
            return data[index];
        }
        else
        {
            Debug.LogError("索引不合法!");
            return default(T);
        }
    }

    public T this[int index]//索引器访问表元素值
    {
        get { return GetElem(index); }
    }



    //获取元素索引值
    public int Locate(T value)
    {
        for (int i = 0; i <= last; i++)
        {
            if (data[i].Equals(value))
            {
                return i;
            }
        }
        return -1;
    }

    //清空顺序表
    public void Clear()
    {
        last = -1;
    }

    //显示所有的表元素值
    public void Display()
    {
        if (IsEmpty())
        {
            Debug.Log("表中没有元素");
            return;
        }
        Debug.Log("表中的值:");
        for (int i = 0; i <= last; i++)
        {
            Debug.Log("index:"+i.ToString()+"   value:"+data[i]);
        }
    }
}

顺序表:测试用例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class _001LineTable : MonoBehaviour
{

    //初始化顺序表
    SqeList<string> sqeList = new SqeList<string>(20);

    void Start()
    {
        Debug.Log("顺序表的最大容量:" + sqeList.MaxSize());

        //判空操作
        Debug.Log("顺序表是否为空:" + sqeList.IsEmpty());

        //判满操作
        Debug.Log("顺序表是否已满:" + sqeList.IsFull());

        //添加操作
        Debug.Log("添加操作--------------添加'123','456','789'");
        sqeList.Add("123");
        sqeList.Add("456");
        sqeList.Add("789");        
        sqeList.Display();

        Debug.Log("顺序表是否为空:" + sqeList.IsEmpty());
        

        //插入操作
        Debug.Log("插入操作---------------在index=2处插入字符串:'111'");
        sqeList.Insert("111", 2);
        sqeList.Display();

        //删除操作
        sqeList.Delete(2);
        Debug.Log("删除操作---------------删除index=2的元素");
        sqeList.Display();

        //表长
        Debug.Log("表长-------------------顺序表表长:" + sqeList.GetLength());

        //查找
        Debug.Log("查找--------------index查value");
        Debug.Log("index=0的值:" + sqeList[0]);
        Debug.Log("index=2的值:" + sqeList.GetElem(2));
        Debug.Log("查找--------------value查index");
        Debug.Log("'789’的index值:" + sqeList.Locate("789"));

        //清空
        Debug.Log("清空表");
        sqeList.Clear();        
        sqeList.Display();

        

    }

输出结果:


注意

1.顺序表的基本操作:初始化表,判空,判满,添加,插入,删除,查找,表长,清空。

2.此处的清空表,只是将last置为-1,来表示为表空,而data中依然有原来的数据,表也没有被销毁。再次的赋值该表,只是将之前的数据覆盖掉。

3.last表示该表的最后元素的索引,last = -1:表示表为空表。

4.插入操作时,插在尾端时,不用移动元素。

posted @ 2018-11-09 13:31  此博客暂停更新  阅读(144)  评论(0编辑  收藏  举报