简单的线性表-顺序表

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

namespace 顺序表
{
    class Program
    {
        static void showDatas(SeqList<int> list)
        {
            int count = list.GetLength();
            for (int idx = 0; idx < count; idx++)
            {
                Console.Write(list.GetElem(idx) + " ");
            }
        }

        static void reverse(SeqList<int> list)
        {
            int count = list.GetLength();
            int halfCount = count / 2;
            int temp = 0;
            for (int idx = 0; idx < halfCount; idx++)
            {
                temp = list[idx];
                list[idx] = list[count - 1 - idx];
                list[count - 1 - idx] = temp;

                //list[idx] += list[count - 1 - idx];
                //list[count - 1 - idx] = list[idx] - list[count - 1 - idx];
                //list[idx] = list[idx] - list[count - 1 - idx];

                //int a = 1, b = 2;
                //a = a + b;  //3
                //b = a - b;  //1
                //a = a - b;  //2
            }
        }
        static void Main(string[] args)
        {
            List<int> mylist = new List<int>();
            mylist.Add(50);

            SeqList<int> list = new SeqList<int>(10);
            int count = 8;
            for (int i = 0; i < count; i++)
            {
                list.Append(i);
            }
            list.Insert(100, 5);//在第五的位置插入一个数据
            list.Insert(10, 3);
            int x = list.GetElem(5);//取表元
            Console.WriteLine("翻转之前的数据是:");
            showDatas(list);

          
            Console.WriteLine("\n翻转之前的数据是:");
            reverse(list);
            showDatas(list);
            

            SeqList<int> listA = new SeqList<int>(5);
            SeqList<int> listB = new SeqList<int>(5);
            listA.Append(10);
            listA.Append(20);
            listA.Append(30);
            listA.Append(40);
             Console.ReadKey();
            SeqList<int> listC = listA + listB;
        }
    }
    public interface ILisDS<T>
    {
        int GetLength(); //求长度
        void Clear(); //清空操作
        bool IsEmpty(); //判断线性表是否为空
        bool Append(T item); //附加操作
        bool Insert(T item, int i); //插入操作
        T Delete(int i); //删除操作
        T GetElem(int i); //取表元
        int Locate(T value); //按值查找
    }
    public class SeqList<T>:ILisDS<T>
    {
        private int maxsize;
        private T[] data;
        private int last;
        /// <summary>
        /// 索引器
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T this[int index]
        {
            get
            {
                return data[index];
            }
            set
            {
                data[index] = value;
            }
        }
        /// <summary>
        /// 容量
        /// </summary>
        public int Maxsize
        {
            get { return maxsize; }
            
        }

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="size">容量</param>
        public SeqList(int size)
        {
            data = new T[size];
            maxsize = size;
            last = -1;
        }
        /// <summary>
        /// 最后一个元素从0下开始计数
        /// </summary>
        public int Last
        {
            get { return last; }
         
        }

        #region ILisDS<T> 成员

        public int GetLength()
        {
            return last + 1;//求顺序表的长度
        }

        public void Clear()
        {
            last = -1;//清空顺序表
        }

        public bool IsEmpty()
        {
            if (last == 1)
            {
                return true;//判断顺序表是否为空
            }
            else
            {
                return false;
            }
        }

        public bool IsFull()
        {
            if (last == maxsize - 1)
            {
                return true; // 判断顺序表是否为满
            }
            else
            {
                return false;
            }
        }

        public bool Append(T item)
        {
            if (IsFull())
            {
                return false;//在顺序表末尾添加元素
            }
            data[++last] = item;
            return true;
        }
        /// <summary>
        /// 在顺序表的第i个数据元素的位置插入一个数据元素
        /// </summary>
        /// <param name="item">新数据</param>
        /// <param name="i">将要插入的位置,有效值为0到长度(last+1)</param>
        /// <returns>False:参数错误导致失败;True:成功插入</returns>
        public bool Insert(T item, int i)
        {
            if (IsFull())
            {
                return false;
            }
            if (i< 0 || i > last + 1)
            {
                return false;
            }
            if (i==last+1)
            {
                data[i] = item;
            }
            else
            {
                for (int index = last; index >= i; --index)
                {
                    data[index + 1] = data[index];
                }
                data[i] = item;
            }
            ++last;
            return true;
        }
        /// <summary>
        /// 删除顺序表的第i个数据元素
        /// </summary>
        /// <param name="i">删除的位置,有效值为0到长度-1(last)</param>
        /// <returns>True:删除成功,False:失败</returns>
        public T Delete(int i)
        {
            T tmp = default(T);
            if (IsEmpty())
            {
                Exception e = new Exception("删除失败,列表为空");
                throw e;
            }
            if (i<0||i>last)
            {
                Exception e = new Exception("输入位置不正确!");
                throw e;
            }
            if (i == last)
            {
                tmp = data[last];
            }
            else
            {
                tmp = data[i];
                for (int index = 0; index < last; ++index)
                {
                    data[index] = data[index + 1];
                }
            }
            --last;
            return tmp;
        }
        /// <summary>
        /// 获取顺序表中的第i个位置的数据元素
        /// </summary>
        /// <param name="i">位置,有效位置从0到长度-1(last)</param>
        /// <returns>第i个位置的数据元素</returns>
        public T GetElem(int i)
        {
            if (IsEmpty()||(i<0)||(i>last))
            {
                Exception e = new Exception("输入位置不正确或者表为空!");
                throw e;
            }
            return data[i];
        }
        /// <summary>
        /// 在顺序表中查找值为value的数据元素
        /// </summary>
        /// <param name="value">要查找的数据</param>
        /// <returns>数据元素在顺序表的下标,-1表示没有找到</returns>
        public int Locate(T value)
        {
            if (IsEmpty())
            {
                return -1;
            }

            int index = 0;
            for (index = 0; index <= last; ++index)
            {
                if (value.Equals(data[index]))
                {
                    break;
                }
            }

            if (index > last)
            {
                return -1;
            }
            return index;
        }
        public static SeqList<T> comb(SeqList<T> a, SeqList<T> b)
        {

            SeqList<T> tmp = new SeqList<T>(a.Maxsize + b.Maxsize);
            for (int i = 0; i < a.Maxsize; i++)
            {
                tmp.Append(a[i]);
            }
            for (int i = 0; i < b.Maxsize; i++)
            {
                tmp.Append(b[i]);
            }
            return tmp;
        }
        public static SeqList<T> operator +(SeqList<T> a, SeqList<T> b)
        {
            SeqList<T> tmp = new SeqList<T>(a.Maxsize + b.Maxsize);
            for (int i = 0; i < a.Maxsize; i++)
            {
                tmp.Append(a[i]);
            }
            for (int i = 0; i < b.Maxsize; i++)
            {
                tmp.Append(b[i]);
            }
            return tmp;
        }

        #endregion
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值