数据结构(C#版)—线性表

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 线性表
{
    //顺序表的类
    class SqListClass
    {
        const int MaxSize = 100;
        //存放顺序表的元素
        public string[] data;
        //存放长度
        public int length;
        //构造函数
        public SqListClass()
        {
            data = new string[MaxSize];
            length = 0;
        }
        #region 顺序表的基本运算算法
        #region  由sqlit中的元素建成表
        public void CreateList(string[] split)
        {
            int i;
            for (i = 0; i < split.Length; i++)
            {
                data[i] = split[i];
                length = 1;
            }
        }
        #endregion

        #region  将L中的元素构成字符串后返回
        public string DispList()
        {
            int i;
            if (length > 0)
            {
                string mystr = data[0];
                for (i = 0; i < length; i++)
                {
                    mystr += "" + data[i];
                }
                return mystr;
            }
            else
            {
                return "空的";
            }
        }
        #endregion

        #region  求顺序表的长度
        public int ListLength()
        {
            return length;
        }
        #endregion

        #region  求某序号的元素的值
        public bool GetElem(int i, string e)
        {
            if (i < 1 || i > length)
            {
                return false;
            }
            else
            {
                e = data[i - 1];
                return true;
            }
        }
        #endregion

        #region  按照元素值查找该序号
        public int LocateElem(string e)
        {
            int i = 0;
            while (i < length && string.Compare(data[i], e) != 0)
            {
                i++;
            }
            if (i > length)
            {
                return 0;
            }
            else
            {
                return i + 1;
            }
        }
        #endregion

        #region   插入数据元素
        public bool ListInsert(int i, string e)
        {
            int j;
            if (i < 1 || i > length + 1)
            {
                return false;
            }
            else
            {
                for (j = length; j >= i; j--)
                {
                    data[j] = data[j - 1];
                    data[i - 1] = e;
                    length++;
                }
                return true;
            }
        }
        #endregion

        #region 删除数据元素
        public bool ListDelete(int i, ref string e)
        {
            int j;
            if (i < 1 || i > length)
            {
                return false;
            }
            else
            {
                e = data[i];
                for (j = i - 1; j < length - 1; j++)
                {
                    data[j] = data[j + 1];
                    length--;
                }
                return true;
            }
        }
        #endregion
       #endregion

    }
}

Written by

说点什么

欢迎讨论

avatar

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

  Subscribe  
提醒