码迷,mamicode.com
首页 > 其他好文 > 详细

顺序表

时间:2015-07-23 23:28:26      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

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

namespace C数据结构与算法
{
    class Program
    {
        static void Main(string[] args)
        {
            OrderTable table = new OrderTable(5);
            table.Add(new Student("A"));
            table.Add(new Student("B"));
            table.Insert(1,new Student("C"));
            table.Add(new Student("D"));
            table.Insert(0, new Student("S"));
            table.ToString();
            //table.Delete(1);
            
            Console.ReadKey();
        }
    }

    /// <summary>
    /// 顺序表
    /// </summary>
    class OrderTable
    {
        public int maxlen;              
        public Student[] listData;      
        public int countLen;             //顺序表已存节点的数量

        public OrderTable(int count) 
        {
            listData = new Student[count];
            maxlen = count;
            countLen = 0;
        }

        public void OrderTableInit() 
        {
            countLen = 0;
        }

        //返回表的元素数量
        public int SLLength() 
        {
            return countLen;
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="index"></param>
        /// <param name="student"></param>
        public void Insert(int index,Student student) 
        {
            if(countLen >= maxlen)
            {
                throw new Exception("顺序表已满,不能插入节点!");
            }

            if (index < 0 || index > countLen - 1) 
            {
                throw new Exception("下标值不正确!");
            }

            //将顺序表的元素向后移动
            for (int i = countLen - 1; i >= index; i--)
            {
                listData[i + 1] = listData[i];
            }

            listData[index] = student;
            countLen++;                 //记录元素+1
        }

        /// <summary>
        /// 添加元素
        /// </summary>
        /// <param name="student"></param>
        public void Add(Student student) 
        {
            if(countLen >= maxlen){
                throw new Exception("顺序表已满,不能再添加结点了!");
            }
            listData[countLen++] = student;
        }


        /// <summary>
        /// 删除元素
        /// </summary>
        public void Delete(int index) 
        {
            if(index < 0 || index > countLen - 1)
            {
                throw new Exception("删除节点下标值出错");
            }

            for (int i = index; i < countLen - 1; i++)
            {
                listData[i] = listData[i + 1];
            }

            countLen--;
        }

        /// <summary>
        /// 寻找Student
        /// </summary>
        /// <param name="index">下标值</param>
        /// <returns>返回Student</returns>
        public Student FindByNum(int index) 
        {
            if(index < 0 && index >= countLen)
            {
                throw new Exception("下标值不正确");
            }
            return listData[index];
        }

        public override string ToString()
        {
            foreach (var i in listData)
            {
                if(i != null)
                    Console.WriteLine(i.key);
            }

            return base.ToString();
        }

    }


    class Student
    {
        public String key;
        public String name;
        public int age;

        public Student() { }
        public Student(String key) 
        {
            this.key = key;
        }

    }

}

顺序表

标签:

原文地址:http://www.cnblogs.com/plateFace/p/4671989.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!