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("删除失败");
                }
    
            }
        }
    }

  • 相关阅读:
    来自lombok的注解(解决idea中的找不到get,set方法,找不到log的问题)
    IDL语言开发规范
    神经网络训练时出现nan错误
    Hadoop WordCount程序
    Hadoop2.4.1伪分布式安装
    Hadoop简介
    linux 安装tensorflow(gpu版本)
    高级映射,查询缓存和与spring整合
    用mybatis实现dao的编写或者实现mapper代理
    mybatis介绍与环境搭建
  • 原文地址:https://www.cnblogs.com/zztong/p/6695202.html
Copyright © 2011-2022 走看看