zoukankan      html  css  js  c++  java
  • 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("删除失败");
                }
    
            }
        }
    }

  • 相关阅读:
    python 学习笔记 数值型(1)
    python 学习笔记 标识符和变量(3)
    python 学习笔记 字符串(2)
    jsp+servlet+javaBean+Dao
    面试被问到岗时间,是越快越好吗?
    有赞多平台推广接入与测试
    HTTPS 加密、证书、签名与握手
    开发到底要不要自己做测试?
    我也曾找不到工作
    世界第三大浏览器正在消亡
  • 原文地址:https://www.cnblogs.com/zztong/p/6695202.html
Copyright © 2011-2022 走看看