if (!newBase)
{
return false;
}
sqList.data = newBase;
sqList.iAllocatedSpace += LIST_INCREMENT;
}
for(i = sqList.iLength; i > index; i--)
{
sqList.data[i] = sqList.data[i - 1];
}
sqList.data[index] = elem;
sqList.iLength++;
return true;
}
/************************************************************************
函数名: Delete
作 者: 谭友亮(Charles Tan)
日 期: 2013-4-12
作 用: 删除顺序表中指定位置的元素
形参数: index 从0开始的索引
返回值: 成功:true 失败: false
************************************************************************/
BOOL CSequenceList::Delete(int index)
{
int i;
if (index < 0 || index > sqList.iLength - 1 || sqList.iLength == 0)
{
return false;
}
for(i = index; i < sqList.iLength - 1; i++)
{
sqList.data[i] = sqList.data[i + 1];
}
sqList.data[sqList.iLength - 1] = '\0';
sqList.iLength--;
return true;
}
/************************************************************************
函数名: GetAt
作 者: 谭友亮(Charles Tan)
日 期: 2013-4-12
作 用: 返回顺序表中指定位置的元素
形参数: index 从0开始的索引
返回值:
************************************************************************/
DataType CSequenceList::GetAt(int index)
{
if (index < 0 || index > sqList.iLength - 1)
{
return false;
}
return sqList.data[index];
}
/************************************************************************
函数名: DestroyList
日 期: 2013-4-12
作 用: 摧毁顺序表
形参数:
返回值:
************************************************************************/
BOOL CSequenceList::DestroyList()
{
if (sqList.data)
{
free(sqList.data);
}
sqList.iLength = 0;
sqList.iAllocatedSpace = 0;
return true;
}
/************************************************************************
函数名: IsEmpty
作 者: 谭友亮(Charles Tan)
日 期: 2013-4-12
作 用: 判断顺序表是否为空
形参数:
返回值:
************************************************************************/
BOOL CSequenceList::IsEmpty()
{
if (sqList.iLength == 0)
{
return false;
}
return true;
}
/************************************************************************
函数名: GetLength
作 者: 谭友亮(Charles Tan)
日 期: 2013-4-12
作 用: 返回顺序表的实际长度
形参数:
返回值:
************************************************************************/
int CSequenceList::GetLength()
{
return sqList.iLength;
}
/************************************************************************
函数名: Find
作 者: 谭友亮(Charles Tan)
日 期: 2013-4-12
作 用: 在顺序表中从from开始查找元素elem,找到则返回从0开始的元素索引
形参数: from 开始查找位置
elem 要查找的元素
返回值: 没有找到则返回-1,找到则返回从0开始的元素索引
************************************************************************/
int CSequenceList::Find(int from, DataType& elem)
{
int i;
i