Operation-使用c#实现顺序表的操作

这是原文

http://blog.csdn.net/jjx0224/article/details/5850051

这是我自己写的简易版
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{

class Program
{
    public class SqlList<T>//顺序表
    {
        int maxSize;
        public T[] data ;
         int last;
        public void Insert(T e)//插入元素
        {
            if (last < maxSize)
            {
                last++;
                data[last] = e;

            }
            else
            {
                Console.WriteLine("顺序表已满");
            }

        }
        public void Delete()//删除元素
        {
            data[last] = default(T);
            last--;

        }
        public void Locate(T element)//确定元素在顺序表中位置
        {
            for (int i = 0; i < last; i++)
            {
                if (data[i].Equals(element))
                {
                    Console.WriteLine("{0}在顺序表第{1}元素中",element, i+1);
                }

            }
        }
        public void Indexing()//检索元素
        {
            for (int i = 0; i <= last; i++)
            {
                Console.Write("{0}", data[i]);
            }
        }
        public void Empty()//清空表
        {
            for (int i = 0; i < last; i++)
            {
                data[i] = default(T);
            }
            last = -1;
        }
        public void IsEmpty()//判断表是否为空
        {
            if (last == -1)
            {
                Console.WriteLine("表为空");
            }
            else
            {
                Console.WriteLine("表不空");

            }
        }
        public SqlList(int size)
        {
            data = new T[size];
            maxSize = size;
            last = -1;


        }






    }
    static void Main(string[] args)
    {

        SqlList<string> sql = new SqlList<string>(10);
        sql.IsEmpty();
        sql.Insert("a");
        sql.Insert("b");
        sql.Insert("1");
        sql.Insert("2");
        sql.Insert("4");
        sql.Insert("6");
        sql.Insert("4");
        sql.IsEmpty();
        sql.Indexing();
        Console.Write("\n");
        sql.Delete();
        sql.Indexing();
        Console.Write("\n");
        sql.Locate("1");
        sql.Empty();
        sql.IsEmpty();



        Console.ReadKey();
    }

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容