选择用C#写,而不是C++,主要是因为用C#封装成类库比较方便,我也可以顺便熟悉一下C#的语法。虽然这样执行效率可能会比较低,但我觉得数据结构主要是一种思想,和具体的实现语言无关。
一、顺序表
采用固定数组实现,初始时分配100个元素,当表长超过已分配的空间大小时自动扩充容量,每次扩充增加10个元素的空间。
文件Status.cs
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DataStructure_CS
{
public enum Status
{
OK,
ERROR,
INFEASIBLE,
OVERFLOW
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DataStructure_CS
{
public enum Status
{
OK,
ERROR,
INFEASIBLE,
OVERFLOW
}
}
文件SeqList.cs
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
}
}
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
}
}