用C#写的数据结构类库(1)——顺序表

最近在复习数据结构,打算把书上的算法都实现一遍。
选择用C#写,而不是C++,主要是因为用C#封装成类库比较方便,我也可以顺便熟悉一下C#的语法。虽然这样执行效率可能会比较低,但我觉得数据结构主要是一种思想,和具体的实现语言无关。

一、顺序表
采用固定数组实现,初始时分配100个元素,当表长超过已分配的空间大小时自动扩充容量,每次扩充增加10个元素的空间。

文件Status.cs
ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataStructure_CS
{
    
public enum Status
    {
        OK,
        ERROR,
        INFEASIBLE,
        OVERFLOW
    }
}

文件SeqList.cs
ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataStructure_CS
{
    
//顺序表
    public class SeqList<T>
    {
        
#region 私有变量
        
private static int LIST_INIT_SIZE = 100;              //初始表长
        private static int LIST_INCREMENT = 10;               //每次扩充表的增量
        private T[] m_elements = new T[LIST_INIT_SIZE];       //存储元素的空间
        private int m_length = 0;                             //表长
        private int m_listsize = LIST_INIT_SIZE;              //当前分配空间容量
        #endregion

        
#region 公共函数
        
//初始化顺序表
        public Status Initialize()
        {
            
try
            {
                m_elements 
= new T[LIST_INIT_SIZE];
                m_length 
= 0;
                m_listsize 
= LIST_INIT_SIZE;
                
return Status.OK;
            }
            
catch
            {
                
return Status.ERROR;
            }
        }
        
//清空顺序表
        public Status Clear()
        {
            
return Initialize();
        }
        
//判断表是否为空
        public bool IsEmpty()
        {
            
return 0 == m_length ? true : false;
        }
        
//获得表的长度
        public int Length()
        {
            
return m_length;
        }
        
//获得第index个元素
        public Status GetElement(int index, ref T e)
        {
            
if (index < 1 || index > m_length)
            {
                
return Status.ERROR;
            }
            
else
            {
                e 
= m_elements[index - 1];
                
return Status.OK;
            }
        }
        
//获得值为e的元素的位序,若不存在则返回0
        public int LocateElement(T e)
        {
            
for (int i = 0; i < m_length; i++)
            {
                
if (m_elements[i].Equals(e))
                {
                    
return i + 1;
                }
            }
            
return 0;
        }
        
//获得元素e的前一个元素
        public Status PriorElement(T e, ref T pre_e)
        {
            
for (int i = 0; i < m_length; i++)
            {
                
if (m_elements[i].Equals(e))
                {
                    
if (i != 0)
                    {
                        pre_e 
= m_elements[i - 1];
                        
return Status.OK;
                    }
                }
            }
            
return Status.ERROR;
        }
        
//获得元素e的下一个元素
        public Status NextElement(T e, ref T next_e)
        {
            
for (int i = 0; i < m_length; i++)
            {
                
if (m_elements[i].Equals(e))
                {
                    
if (i != m_length - 1)
                    {
                        next_e 
= m_elements[i + 1];
                        
return Status.OK;
                    }
                }
            }
            
return Status.ERROR;
        }
        
//在第index个位置之前插入新的数据元素e,i应满足1<=index<=m_length+1
        public Status Insert(int index, T e)
        {
            
if (index < 1 || index > m_length + 1)
            {
                
return Status.ERROR;
            }
            
if (m_length >= m_listsize)
            {
                
if (Expand() != Status.OK)
                {
                    
return Status.OVERFLOW;
                }
            }
            
for (int i = m_length; i >= index; i--)
            {
                m_elements[i] 
= m_elements[i - 1];
            }
            m_elements[index 
- 1= e;
            m_length
++;
            
return Status.OK;
        }
        
//将一个元素插到表尾
        public Status Append(T e)
        {
            
return Insert(m_length + 1, e);
        }
        
//删除第index个数据元素,并用e返回其值,index应满足1<=index<=m_length
        public Status Delete(int index, ref T e)
        {
            
if (index < 1 || index > m_length)
            {
                
return Status.ERROR;
            }
            e 
= m_elements[index-1];
            
for (int i = index - 1; i < m_length - 1; i++)
            {
                m_elements[i] 
= m_elements[i + 1];
            }
            m_length
--;
            
return Status.OK;
        }
        
#endregion

        
#region 内部私有函数
        
//当已分配的空间不够时扩充空间
        private Status Expand()
        {
            
try
            {
                T[] newspace 
= new T[m_listsize + LIST_INCREMENT];
                
for (int i = 0; i < m_length; i++)
                {
                    newspace[i] 
= m_elements[i];
                }
                m_elements 
= new T[m_listsize + LIST_INCREMENT];
                
for (int i = 0; i < m_length; i++)
                {
                    m_elements[i] 
= newspace[i];
                }
            }
            
catch
            {
                
return Status.OVERFLOW;
            }
            m_listsize 
+= LIST_INCREMENT;
            
return Status.OK;
        }
        
#endregion
    }
}

转载于:https://www.cnblogs.com/waynecraig/archive/2009/09/08/1562219.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值