9,20作业

text_1.c

#include "text_1.h"
#include <iostream>


using namespace std;

//typedef int datatype;        //类型重命名
using datatype = int;

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<<this->ptr[i]<<" ";
    }
    cout<<endl;
}

// 任意位置删除
void SeqList::erase()
{
    //判空
    if(this->empty())
    {
        return;
    }
    int n;
    cout<<"请输入要删除的位置"<<endl;
    cin>>n;
    if(this->empty())
    {
        return;
    }
    for(int i=n-1;i<this->len;i++)
    {
        this->ptr[i]=this->ptr[i+1];
    }
    cout<<"删除成功!"<<endl;
    this->len--;

}
//尾删
void SeqList::pop_back()
{
    //判空
    if(this->empty())
    {
        return;
    }
    this->ptr[len-1]=0;
    this->len--;
    cout<<"尾删除成功!"<<endl;
}

// 获取任意位置
void SeqList::my_at()
{
    //判空
    if(this->empty())
    {
        return;
    }
    int n;
    cout<<"请输入要获取的位置"<<endl;
    cin>>n;
    if(this->empty())
    {
        return;
    }
    cout<<this->ptr[n-1]<<endl<<"获取成功"<<endl;
}
//插入
void SeqList::insert()
{
    //判空
    if(this->empty()||this->full())
    {
        return;
    }
    cout<<"请输入要插入的位置"<<endl;
    int n,m;
    cin>>n;
     cout<<"请输入要要插入的数"<<endl;
     cin>>m;
    for(int i=this->len-1;i>=n-1;i--)
    {
        this->ptr[i+1]=this->ptr[i];
    }
    this->ptr[n-1]=m;
    this->len++;
}
//排序
void SeqList:: sort()
{
    //判空
    if(this->empty())
    {
        return;
    }
    cout<<"请输入一个数(非0则升序排序):"<<endl;
    int n;
    cin>>n;
    bool a=n;

    for(int i=1;i<this->len;i++)
    {
        for(int j=0;j<this->len-i;j++)
        {
            if(a!=0) // 升序排序
            {
                if(this->ptr[j]>this->ptr[j+1])
                {
                    int tem=this->ptr[j];
                    this->ptr[j]=this->ptr[j+1];
                    this->ptr[j+1]=tem;
                }
            }
            else  //降序排序
            {
                if(this->ptr[j]<this->ptr[j+1])
                {
                    int tem=this->ptr[j];
                    this->ptr[j]=this->ptr[j+1];
                    this->ptr[j+1]=tem;
                }
            }
        }
    }
    cout<<"排序成功!"<<endl;
}


text_!.h

#ifndef TEXT_1_H
#define TEXT_1_H

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 show();
    void erase();
    int location();
    void pop_back();
    void my_at();
    void  sort();
    void insert();
};

#endif // TEXT_1_H

main.c

#include <iostream>
#include "text_1.h"

using namespace std;




int main()
{
    SeqList sl;           //实例化一个顺序表对象
    sl.init(5);            //申请空间
    sl.push_back(1); //
    sl.push_back(3);
    sl.push_back(5);
    sl.push_back(7);
    sl.push_back(9);
    sl.show();  //输出显示

    sl.erase();  //任意位置删除
    sl.show();

    sl.pop_back();  //尾删
    sl.show();

    sl.my_at();   //获取任意位置元素
    sl.insert();  //任意位置插入
    sl.show();

    sl.sort();  //排序
    sl.show();


    return 0;
}
运行结果

思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值