自学内容网 自学内容网

c++day3 手动封装一个顺序表(SeqList),分文件编译实现

要求:

有私有成员:顺序表数组的起始地址 ptr、 顺序表的总长度:size、顺序表的实际长度:len

成员函数:初始化 init(int n)

判空:empty

判满:full

尾插:push_back

插入:insert(int index)

任意位置删除:erase(int index)

尾删: pop_back

求长度:size()

获取任意位置元素:& at(int inex)

将顺序表进行排序:sort(bool flag) //flag 为真,表示升序,否则是降序

seqlist.h

#ifndef SEQLIST_H
#define SEQLIST_H
 
 
//typedef int datatype;        //类型重命名
using datatype = int;
 
 
 
 
//封装一个顺序表
class SeqList
{
private:
    datatype *ptr;        //指向堆区空间的起始地址
    int size;             //总长度
    int len = 0;          //当前顺序表实际长度
 
 
public:
    void init(int n);
 
    bool empty();
 
    bool full();
 
    void push_back(datatype e);
 
    void insert(int index);//插入
 
    void erase(int index); //任意位置删除
 
    void pop_back();       //尾删
 
    void size_1();
 
    int & at(int index); //获取任意位置元素
 
    void sort(bool flag);   //真升序,否则降序
 
    void show();
 
 
};
#endif // SEQLIST_H

seqlist.cpp

#include <iostream>
 
using namespace std;
#include "seqlist.h"
 
void SeqList::init(int n)
{
       //在堆区申请出一个长度为n的空间,将其实地址赋值给ptr
       this->ptr = new datatype[n];
 
 
       //给len进行初始化
       this->len = 0;
       this->size = n;
}
 
bool SeqList::empty()
{
    return this->len == 0;
}
 
bool SeqList::full()
{
    return this->len == this->size;
}
 
void SeqList::push_back(datatype e)
{
    //判断是否满了
    if(this->full())
    {
        return ;
    }
 
 
    this->ptr[len++] = e;
 
}
 
 
void SeqList::show()
{
    //判空
    if(this->empty())
    {
        return ;
    }
 
    cout<<"当前顺序表中的元素分别是:";
    for(int i=0; i<this->len; i++)
    {
        cout<<*(ptr+i)<<" ";
    }
    cout<<endl;
}
void SeqList::insert(int index)
{
    if(this->empty()||this->full()||index<0||index>this->len)
    {
        return ;
    }
    int key;
    cout<<"第"<<index+1<<"个你想插入的值:";
    cin>>key;
 
    int n = this->len;
    int i = index;
    while(i<=n)
    {
        this->ptr[n+1]=this->ptr[n];
        n--;
    }
    this->ptr[index]=key;
 
    this->len++;
}
 
void SeqList::erase(int index)
{
    if(this->empty()||index<0||index>this->len)
    {
        return ;
    }
    int n = this->len;
    int i = index;
    while (i<=n)
    {
        this->ptr[i]=this->ptr[i+1];
        i++;
    }
 
    this->len--;
 
}
 
void SeqList::pop_back()
{
    if(this->empty())
    {
        return ;
    }
 
    this->len--;
}
 
void SeqList::size_1()
{
    if(this->empty())
    {
        return ;
    }
 
    cout<<"顺序表元素个数:"<<this->len+1<<endl;
 
}
 
int error =-1;
int & SeqList::at(int index)
{
 
    if(this->empty()||index<0||index>=this->len)
    {
 
        cout<<"越界"<<endl;
        return error;
    }
 
    return this->ptr[index];
 
}
 
void SeqList::sort(bool flag)
{
    if(this->empty())
    {
        return ;
    }
    int i = 0,j,index;
    int n = this->len;
 
 
    while(i<n)
    {
        index = i;
        j=i;
        while(j<n)
        {
            if(flag)
            {
                if(this->ptr[index]>this->ptr[j])
                {
                    index = j;
                }
            }
            else{
                if(this->ptr[index]<this->ptr[j])
                {
                    index = j;
                }
 
            }
            j++;
        }
        if(index!=i)
        {
            int temp = this->ptr[index];
            this->ptr[index] = this->ptr[i];
            this->ptr[i] = temp;
 
        }
        i++;
 
 
 
    }
}

main.cpp

#include <iostream>
#include "seqlist.h"
using namespace std;
 
int main()
{
 
    SeqList sl;           //实例化一个顺序表对象
    sl.init(5);            //申请空间
    sl.push_back(1);
    sl.push_back(3);
    sl.push_back(5);
 
    sl.show();
 
    sl.insert(1);
    sl.insert(1);
    cout<<"插入后数组:"<<endl;
    sl.show();
 
//    sl.erase(1);
//    cout<<"删除后数组:"<<endl;
//    sl.show();
 
//    sl.pop_back();
//    cout<<"尾删后数组:"<<endl;
//    sl.show();
 
    int pos = sl.at(2);
    cout<<"该位置的值:"<<pos<<endl;
 
    sl.sort(1);
    cout<<"升序排序后:"<<endl;
    sl.show();
 
    sl.sort(0);
    cout<<"降序排序后:"<<endl;
    sl.show();
 
    return 0;
}
 


原文地址:https://blog.csdn.net/qy3333/article/details/142411405

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!