欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 顺序表---增删改查--逆至--删除最小值

程序员文章站 2022-05-14 08:24:56
输出结果: ......
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;

namespace 表_增删改插_逆至_最小值
{
    class myseqlist
    {
        public int _flag;
        private int[] _ints;

        public myseqlist()
        {
            _flag = 0;
            _ints = new int[30];
        }
        public myseqlist(int _count)
        {
            _flag = 0;
            _ints = new int[_count];

        }
        public  void additem(int item)   //增加
        {
            if (_flag >= _ints.length)
            {
                console.writeline("溢出..");
                return;
            }
            _ints[_flag] = item;
            _flag++;
        }
        public int  removeat(int index)  //按照指定索引下标删除
        {
            int returnvalue = default(int);
            if (index < 0 || index >= _flag)
            {

                console.writeline("超出范围...");
                goto returntip;
            }
            returnvalue = _ints[index];
            for (int i = index; i < _flag - 1; i++)
            {
                _ints[i] = _ints[i - 1];
            }
            _flag--;
        returntip:
            return returnvalue;
        }
        public void remove(int removeitem)  //按照指定元素进行删除
        {
            int tmpindex = -1;
            for (int i = 0; i < _flag; i++)
            {
                if (_ints[i].equals(removeitem))
                {
                    tmpindex = i;
                    break;
                }
            }
            if (tmpindex != -1)
            {
                removeat(tmpindex);
            }
        }

        public int indexof(int item)  //查找元素,返回对应下标
        {
            int returnvalue = -1;
            for (int i = 0; i < _flag; i++)
            {
                if (_ints[i].equals(item))
                {
                    returnvalue = i;
                    break; 
                }
            }
            return returnvalue;
        }

        public void insert(int index, int item)     //在指定位置插入元素
        {
            if (_flag >= _ints.length)
            {
                console.writeline("溢出..");
                return;
            }
            if (index < 0 || index > _flag)
            {
                console.writeline("索引出界..");
                return;
            }

            for (int i = _flag; i > index; i--)
            {
                _ints[i] = _ints[i - 1];
            }
            _ints[index] = item;
            _flag++;
        }

        public void reverse()   //顺序表反转
        {
            int tmpitem = default(int);
            for (int i = 0; i < _flag/2; i++)
            {
                tmpitem =_ints[i];
                _ints[i] = _ints[_flag - i - 1];
                _ints[_flag - i - 1] = tmpitem;
            }
        }

        public int deletemin()  //删除最小值
        {
            int tmpmin = _ints[0];
            for (int i = 1; i < _flag; i++)
            {
                if (tmpmin > _ints[i])
                {
                    tmpmin = _ints[i];
                }
            }
            return tmpmin;
        }
       
        public int fun(int index)
        {
            return _ints[index];
        }

    }

    class program
    {
        static void main(string[] args)
        {
            myseqlist num = new myseqlist();
            num.additem(1);
            num.additem(2);
            num.additem(3);
            num.additem(110);
            num.additem(4);
            num.additem(7);
            num.additem(9);
            for (int i = 0; i < num._flag; i++)
            {
                console.writeline(num.fun(i));
            }
            console.writeline("反转为========================");
            num.reverse(); //反转
            for (int i = 0; i < num._flag; i++)
            {
                console.writeline(num.fun(i));
            }

            console.writeline("最小值为=========");
            console.writeline(num.deletemin());
            console.readline();
        }
    }
}

输出结果:

C#  顺序表---增删改查--逆至--删除最小值