代码改变世界

[原创]c#的线性表

2007-08-04 11:01  Virus-BeautyCode  阅读(899)  评论(0编辑  收藏  举报

using System;
using System.Collections.Generic;
using System.Text;

namespace SWB.ConsoleApp.List
{
    class MyList
    {
        private int [] _element;
        public int this[int k]
        {
            get
            {
                return _element[k];
            }
        }
       //
       
        private int _len;
        public int Len
        {
            get { return _len; }
        }
        private int _maxLen;

        public MyList()
        {
            string s="";
            Console.WriteLine("输入线性表的长度:\n");
            s = Console.ReadLine();
            _len = Int32.Parse(s);
            _element = new int[_len];
            Console.WriteLine("输入线性表的元素:\n");
            for (int i = 0; i < _len; i++)
            {
                s = Console.ReadLine();
                _element[i] = Int32.Parse(s);
            }
        }//构造函数,初始化线性表

        public void ListEmpty(MyList l)
        {
            if (_len == 0)
            {
                Console.WriteLine("该表为空表。");
            }
            else
            {
                Console.WriteLine("该表不空。");
            }
        }//判断列表是不是空表

        public bool GetElement(MyList l, int i, ref int e)
        {
            if (i < 1 || i > l._len)
            {
                return false;
                //Console.WriteLine("err");
            }
            e = l._element[i - 1];
            return true;
        }//返回表中的第i个元素

        public bool DeleteElement(MyList l, int i)
        {
            if (i < 1 || i > l._len)
            {
                return false;
            }
            for (int j = i - 1; j < l._len - 1; j++)
            {
                l._element[j] = l._element[j + 1];
            }
            return true;
        }//删除第i个元素

        public bool InsertElement(MyList l, int i, int e)
        {
            if(i<1||i>l._len)
            {
                return false;
            }
            for (int j = l._len-1; j >= i - 1; j--)
            {
                l._element[j] = l._element[j - 1];
            }
            l._element[i - 1] = e;
            l._len++;
            return true;
        }//把元素e插入到位置i前

        public int LocateElement(MyList l, int e)
        {
            for (int i = 0; i < l._len; i++)
            {
                if (l._element[i] == e)
                    return i + 1;
            }
            return 0;
        }//定位操作

        public void PriorElement(MyList l, int cur_e, ref int pre_e)
        {
            int k = LocateElement(l, cur_e);
            if (k == 0 || k == 1)
                Console.WriteLine("没有前驱。");
            else
                pre_e = l._element[k - 2];
        }//求前驱

        public void NextElement(MyList l, int cur_e, ref int next_e)
        {
            int k = LocateElement(l, cur_e);
            if (k < 0 || k > l._len)
                Console.WriteLine("没有后继。");
            else
                next_e = l._element[k];
        }//求后继
    }

    class Program
    {
        static void Main(string[] args)
        {
            int a = 0;//用来存储所取元素
            int b = 0;//用来存储删除的元素
            int c = 0;//用c来存储元素4的位置
            int d = 0;//用d来存储元素4的前驱
            int f = 0;//用f来存储元素4的后继
            MyList La = new MyList();//创建线性表实例
            //La.Length=0将线性表置
            La.ListEmpty(La);//判断表是不是空表
            La.GetElement(La, 3, ref a);//取线性表中的第三个元素并用a来存储
            La.DeleteElement(La, 3);//用b存储删除线性表中的第三个元素
            La.InsertElement(La, 4, 5);//在线性表第四个元素前插入元素5
            c = La.LocateElement(La, 4);//用c来存储元素4的位置
            La.PriorElement(La, 4, ref d);//用d来存储元素4的前驱
            La.NextElement(La, 4, ref f);//用f来存储元素4的后驱
            Console.WriteLine("c=" + c);
            Console.WriteLine("d=" + d);
            Console.WriteLine("f=" + f);
            Console.Write("线性表中的元素:");
            for (int i = 0; i < La.Len-1; i++)
                Console.Write(La[i] + " ");

            Console.ReadLine();


        }
    }
}