栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

基于c++的线性顺序表代码实现,类实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于c++的线性顺序表代码实现,类实现

#include
#include
#include
#include

using namespace std;

template
class SqureList
{
private:
    int m_length;   //元素数量
    int m_capicity;  //容量(开辟的内存)
    T *m_List;  //存储元素
public:
    SqureList();
    ~SqureList();
    int GetCapicity();  //获取线性表的容量
    int SetCapicity(int capicity);  //设置线性表的容量
    int Getlength();         //获取线性表的当前长度
    bool push_back(T elemnum);     //尾插法插入元素
    bool GetElem(int index,T *e);    //获取线性表的相应下标的元素
    bool For_each();//遍历输出线性顺序表
    bool IsEmpty();  //查询线性表是否为空
    bool ClearSq();  //清空顺序表
    bool Insert(T elemnum, int i);  //向指定位置插入元素
    bool Remove(int i,T*e);//删除并返回线性表的第i个下标位置的元素。
    int find_elem(T elemnum);//查找指定元素返回线性表中元素首次出现的下标位置
    
};
int main(int agrc, char* agrv[])
{
    SqureList kk;
    string k[5];
    kk.push_back("asd");
    kk.push_back("dsdd");
    kk.Insert("sasa",1);
    kk.Insert("kkk", 1);
    kk.Insert("jjjj", 1);
    kk.Remove(2, &k[0]);
    //kk.Remove(2, &k[0]);
    //kk.Remove(2, &k[0]);
    kk.Remove(1, &k[0]);
    
    kk.For_each();
    cout << kk.find_elem("sasa") << endl;
    cout << kk.GetCapicity() << endl;
    return 0;
}
//构造函数
template
SqureList::SqureList()
{
    this->m_capicity = 4;
    this->m_List = new T[this->m_capicity];
    this->m_length = 0;
}
//析构函数
template
SqureList::~SqureList()
{
    delete[] this->m_List; // 释放数组内存
    this->m_List = NULL;
}
//设置顺序表的容量
template 
int SqureList::SetCapicity(int capicity)
{
    if (this->m_length == 0)
    {
        this->m_List = new T[capicity];
        this->m_capicity = capicity;
        return this->m_capicity;
    }
    T* temp = this->m_List;
    this->m_List = new T[capicity];
    this->m_capicity = capicity;
    for (int i = 0; i < this->m_length; i++)
    {
        this->m_List[i] = temp[i];                   
    }
    delete[] temp;
    temp = NULL;
    return this->m_capicity;
}
//获取当前顺序表的容量
template
int SqureList::GetCapicity()
{
    return this->m_capicity;
}   
  //尾插法插入元素,定容,应修改为自动变容
template
bool SqureList::push_back(T elemnum)  
{
    if ((this->m_length) == (this->m_capicity))
    {
        (this->m_capicity) *= 2;
        this->SetCapicity(this->m_capicity);
    }
    this->m_List[m_length] = elemnum;
    this->m_length++;
    return true;
}
//向指定位置插入元素(变容修改)
template
bool SqureList::Insert(T elemnum, int i)
{
    if (i<0 || i>this->m_length)
    {
        return false;  //下标越界(小于零,或比最后元素的下标+1 还大)
    }
    if ((this->m_length) == (this->m_capicity))  //容量扩展
    {
        (this->m_capicity) *= 2;
        this->SetCapicity(this->m_capicity);
    }
    for (int j = this->m_length; j >i;j--)  //将下标i在内的后续所有元素后移一位
    {
        this->m_List[j] = this->m_List[j-1];
    }
    this->m_List[i] = elemnum;
    this->m_length++;
    return true;
}
//获取指定下标元素,从零开始,失败返回false
template
bool SqureList::GetElem(int index,T *e)  
{
    if (index<0||index >= m_length)
    {
        return false;
    }
    else
    {
        *e = this->m_List[index];
        return true;
    }
}
//遍历输出线性顺序表
template
bool SqureList::For_each()  //遍历输出线性顺序表
{
    if (this->m_length == 0)return false;
    T temp;
    for (int i = 0; i < this->m_length-1; i++)
    {
        this->GetElem(i, &temp);
        cout << temp << ",";
    }
    this->GetElem(this->m_length - 1, &temp);
    cout << temp << endl;
    return true;
}
//获取线性表的当前长度
template
int SqureList::Getlength()         //获取线性表的当前长度
{
    return this->m_length;
}
//判断线性表是否为空
template
bool SqureList::IsEmpty()
{
    if (this->Getlength() == 0)
        return true;
    else
    {
        return false;
    }
}
//清空顺序表
template
bool SqureList::ClearSq()
{
    this->m_length = 0;
}
//删除并返回线性表的第i个下标位置的元素。
template
bool SqureList::Remove(int i,T *e)  //删除并返回线性表的第i个下标位置的元素。
{
    if (i < 0 || i >= this->m_length)
    {
        return false;  //下标越界
    }
    *e = this->m_List[i];
    for (int j = i; j < this->m_length-1; j++)
    {
        this->m_List[j] = this->m_List[j + 1];
    }
    this->m_length--;
    if (this->m_length < (this->m_capicity) / 4)
    {
        this->SetCapicity(this->m_capicity / 2);
    }
    return true;
}
//查找指定元素返回线性表中元素首次出现的下标位置
template
int SqureList::find_elem(T elemnum)//查找指定元素返回线性表中元素首次出现的下标位置
{
    int index = -1;
    for (int i = 0; i < this->m_length; i++)
    {
        if (this->m_List[i] == elemnum)
        {
            index = i;
            break;
        }
    }
    return index;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/997362.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号