c#之线性表

1,线性表接口 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 线性表
{
    /// 
    /// 线性表接口
    /// 
    /// 
    interface ISeqList
    {
        /// 
        /// 得到顺序表的长度
        /// 
        /// 
        int GetLength();
        /// 
        /// 添加元素
        /// 
        /// 
        void Add(T item);
        /// 
        /// 插入元素
        /// 
        /// 
        /// 
        void Insert(T item, int index);
        /// 
        /// 删除元素"即 删除第index+1个元素"
        /// 
        /// 
        /// 
        bool Delete(int index);
        /// 
        /// 顺序表是否为空
        /// 
        bool IsEmpty();
        /// 
        /// 清空顺序表
        /// 
        void Clear();
        /// 
        /// 得到元素
        /// 
        /// 
        /// 
        T GetElement(int index);
        /// 
        /// 根据索引得到对应的数据
        /// 
        /// 
        /// 
        T this[int index] { get; }
        /// 
        /// 得到所有值为value的索引
        /// 
        /// 
        /// 
        List GetIndex(T value);
    }
}

2,实现线性表接口

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

namespace 线性表
{
    /// 
    /// 顺序表的实现
    /// 
    /// 
    class SeqList : ISeqList
    {
        private T[] data;//存储数据
        private int count = 0;//数据的个数
        public SeqList(int size)
        {
            data = new T[size];
            count = 0;
        }
        public SeqList()
            : this(10)
        {
        }
        public int GetLength()
        {
            return count;
        }
        public void Add(T item)
        {
            if (count == data.Length)//数组已满
            {
                Console.WriteLine("当前数组已满,不能再添加数据");
            }
            else
            {
                data[count] = item;
                count++;
            }
        }
        public void Insert(T item, int index)
        {
            for (int i = count - 1; i >= index; i--)
            {
                data[i + 1] = data[i];
            }
            data[index] = item;
            count++;
        }

        public bool Delete(int index)
        {
            if (index >= 0 && index < count)
            {
                for (int i = index; i < count; i++)
                {
                    data[i] = data[i + 1];
                }
                count--;
                return true;

            }
            else
            {
                Console.WriteLine("索引超出了线性表的有效范围 ");
                return false;
            }
        }
        public bool IsEmpty()
        {
            return count == 0;
        }
        public void Clear()
        {
            count = 0;
        }
        public T GetElement(int index)
        {
            if (index >= 0 && index <= (count - 1))
            {
                return data[index];
            }
            else
            {
                Console.WriteLine("索引不存在");
                return default(T);
            }
        }
        //属性
        public T this[int index]
        {
            get
            {
                if (index >= 0 && index <= (count - 1))
                {
                    return data[index];
                }
                else
                {
                    Console.WriteLine("索引不存在");
                    return default(T);
                }
            }
        }

        public List GetIndex(T value)
        {
            List index = new List();//存储值为value的所有索引
            for (int i = 0; i < count; i++)
            {
                if (value.Equals(data[i]))
                {
                    index.Add(i);
                }
            }
            return index;

        }
    }
}

3,测试线性表

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

namespace 线性表
{
    class Program
    {
        static void Main(string[] args)
        {
            ISeqList list = new SeqList();
            list.Add('a');
            list.Add('b');
            list.Add('c');
            list.Add('d');
            list.Add('b');
            list.Add('b');
            list.Add('e');
            Console.WriteLine("线性表中的数据有:");
            for (int i = 0; i < list.GetLength(); i++)
            {
                Console.Write(" " + list.GetElement(i));
            }
            Console.WriteLine();
            Console.WriteLine("值为‘b’的索引是:");
            List index = list.GetIndex('b');
            foreach (int i in index)
            {
                Console.Write(" " + i);
            }
            Console.WriteLine();
            Console.WriteLine("删除c之后的线性表:");
            list.Delete(5);
            for (int i = 0; i < list.GetLength(); i++)
            {
                Console.Write(" " + list.GetElement(i));
            }
            Console.WriteLine();
            Console.WriteLine("插入f之后的线性表:");
            list.Insert('f', 4);
            for (int i = 0; i < list.GetLength(); i++)
            {
                Console.Write(" " + list.GetElement(i));
            }
            Console.ReadKey();
        }
    }
}

4,测试结构:

c#之线性表_第1张图片

你可能感兴趣的:(c#,数据结构与算法)