C#顺序表操作

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace test
{
    class Program
    {
        public int Length;
        public string Path = @"F:	est.txt";
        public void CreateOrderList(ref int[] a,ref int length)  //创建一个顺序表并获取其含有元素的个数
        {
            StreamReader sR = new StreamReader(Path, Encoding.Default);
            //读取文件中的第一行数据并分割放入字符串数组
            string[] s = sR.ReadLine().Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);
            int[] IArry;
            IArry = Array.ConvertAll<string, int>(s, e => int.Parse(e));//将字符串数组转换为整型数组
            length = IArry.Length;   //获取数组长度
            for (int i = 0; i < IArry.Length; i++)
            {
                a[i] = IArry[i];
            }
        }
        public bool InsertOrderList(int position, int value,ref int[]a)//在数组a的position位置插入值为value
        {
            if (position > Length||position<=0)     //超出范围
            {
                Console.WriteLine("插入的位置不符合");
                return false;
            }
            else if (position == Length)  //最后一个位置
            {
                a[a.Length] = value;
            }
            else if (position < Length)  
            {
                for (int i = Length; i >=position; i--)
                {
                    a[i] = a[i - 1];
                }
                a[position - 1] = value;
            }
            Length++;
            return true;
        }
        public bool RemoveElement(int position,ref int[]a)      //删除数组a位置position的元素
        {
            if (position > Length || position <= 0)
            {
                Console.WriteLine("不存在此位置");
                return false;
            }
            else
            {
                for (int i = position; i < Length;i++ )
                {
                    a[i - 1] = a[i];
                }
            }
            Length--;
            return true;
        }
        public void DisList(ref int[] a)    //输出数组
        {
            for (int i = 0; i < Length; i++)
            {
                Console.Write(a[i] + "  ");
            }
            Console.WriteLine();
        }
        static void Main()
        {
            Program p = new Program();
            int[] A=new int[13];
            p.CreateOrderList(ref A, ref p.Length);
            Console.WriteLine("顺序表的长度为{0}", p.Length);
            p.DisList(ref A);
            if (p.InsertOrderList(3, 6, ref A))
            {
                Console.WriteLine("插入成功");
                p.DisList(ref A);
            }
            else
            {
                Console.WriteLine("插入失败");
            }
            if (p.RemoveElement(4, ref A))
            {
                Console.WriteLine("删除成功");
                p.DisList(ref A);
            }
            else
            {
                Console.WriteLine("删除失败");
            }

        }
    }
}

原文地址:https://www.cnblogs.com/zztong/p/6695202.html