c++9月20日

1.思维导图

2.顺序表

头文件

复制代码
#ifndef RECTANGLE_H
#define RECTANGLE_H

#include <iostream>

using namespace std;

using datatype = int ;//类型重定义

class Seqlist
{
private://私有权限
    datatype *ptr; //指向堆区申请空间的起始地址
    int size;//堆区空间的长度
    int len = 0;//顺序表的实际长度
public://公共权限
    void init(int );
    void menu();
    bool menu1();
    bool empty();
    bool full();
    void push_back(datatype);
    void show();
    void insert();
    void erase();
    void pop_back();
    void Size();
    void sort(bool);
    void at();
};


#endif // RECTANGLE_H

功能函数

复制代码
#include"rectangle.h"
void Seqlist::init(int n)
{
    this->ptr = new datatype[n];//在堆区申请n个datatype类型的空间
    this->len = 0;//对Seqlist的实际长度初始化
    this->size = n;//确定Seqlist的最大长度
}
void Seqlist::menu()
{
    cout<<"****1.插入***************"<<endl;
    cout<<"****2.删除***************"<<endl;
    cout<<"****3.尾删***************"<<endl;
    cout<<"****4.求长度*************"<<endl;
    cout<<"****5.获取任意位置元素***"<<endl;
    cout<<"****6.排序***************"<<endl;
    cout<<"****7.查看所有元素*******"<<endl;
    cout<<"****8.退出***************"<<endl;
    int n;
    cout<<"请输入:";
    cin>>n;
    bool flag;
    switch(n)
    {
    case 1:insert();
        break;
    case 2:erase();
        break;
    case 3:pop_back();
        break;
    case 4:Size();
        break;
    case 5:at();
        break;
    case 6:flag = menu1();sort(flag);
        break;
    case 7:show();
        break;
    case 8:exit(0);
        break;
    }
}
bool Seqlist::empty()//判空
{
    return this->len == 0;//Seqlistd的实际长度为0时返回一个false(假)
}
bool Seqlist::full()//判满
{
    return this->len == this->size;//Seqlist的实际长度为size时返回一个true(真)
}
void Seqlist::push_back(datatype e)//尾插
{
    if(this->full())//判满
    {
        cout<<"顺序表已满"<<endl;
        return;
    }
    this->ptr[len++] = e;
}
void Seqlist::show()//查看顺序表内的数据
{
    if(this->empty())// 判空
    {
        cout<<"顺序表为空"<<endl;
        return;
    }
    for(int i = 0;i<this->len;i++)
    {
        cout<<this->ptr[i]<<" ";
    }
    cout<<endl;
}
void Seqlist::insert()//任意位置插入
{
    if(this->full())//判满
    {
        cout<<"顺序表已满"<<endl;
        return;
    }
    cout<<"输入插入位置:";
    int index;
    cin>>index;
    cout<<"输入插入数字:";
    datatype e;
    cin>>e;
    this->len++;
    for(int i=this->len;i>=index;i--)
    {
        this->ptr[i]=this->ptr[i-1];
    }
    this->ptr[index-1] = e;
}
void Seqlist::erase()//任意位置删除
{
    if(this->empty())// 判空
    {
        cout<<"顺序表为空"<<endl;
        return;
    }
    cout<<"输入删除位置:";
    int index;
    cin>>index;
    for(int i=index-1;i<this->len;i++)
    {
        this->ptr[i]=this->ptr[i+1];
    }
    this->len--;
}
void Seqlist::pop_back()//尾删
{
    if(this->empty())// 判空
    {
        cout<<"顺序表为空"<<endl;
        return;
    }
    this->ptr[len]=0;
    this->len--;
}
void Seqlist::Size()//顺序表长度
{
    if(this->empty())// 判空
    {
        cout<<"顺序表为空"<<endl;
        return;
    }
    cout<<"长度为:"<<this->len<<endl;
}
bool Seqlist::menu1()
{
    cout<<"1.升序"<<endl;
    cout<<"2.降序"<<endl;
    bool flag;
    int n;
    cin>>n;
    if(n==1)
    {
        flag=true;
    }
    else
    {
        flag=false;
    }
    return flag;
}
void Seqlist::sort(bool flag)//排序
{
    if(this->empty())// 判空
    {
        cout<<"顺序表为空"<<endl;
        return;
    }
    int i,j;
    for(i=1;i<this->len;i++)
    {
        for(j=0;j<this->len-i;j++)
        {
            if(flag!=0&&this->ptr[j]>this->ptr[j+1])
            {
                int temp = this->ptr[j];
                this->ptr[j]=this->ptr[j+1];
                this->ptr[j+1]=temp;
            }
            else if(flag==0&&this->ptr[j]<this->ptr[j+1])
            {
                int temp = this->ptr[j];
                this->ptr[j]=this->ptr[j+1];
                this->ptr[j+1]=temp;
            }
        }
    }
}
void Seqlist::at()//获得数据
{
    if(this->empty())// 判空
    {
        cout<<"顺序表为空"<<endl;
        return;
    }
    int inex;
    cout<<"输入获取位置:";
    cin>>inex;
    if(inex>this->len)
    {
        cout<<"该位置为空";
    }
    else
    {
    cout<<"该位置为"<<this->ptr[inex-1]<<endl;
    }
}

主函数

复制代码
#include"rectangle.h"
int main()
{
    Seqlist s1;
    s1.init(5);
    int arr[3]={1,3,5};
    for(int i=0;i<3;i++)
    {
        s1.push_back(arr[i]);
    }
    while(1)
    {
        s1.menu();
    }
    return 0;
}
相关推荐
爱coding的橙子1 小时前
每日算法刷题 Day3 5.11:leetcode数组2道题,用时1h(有点慢)
算法·leetcode
Chase_Mos2 小时前
Spring 必会之微服务篇(1)
java·spring·微服务
虾球xz3 小时前
游戏引擎学习第268天:合并调试链表与分组
c++·学习·链表·游戏引擎
fpcc3 小时前
跟我学c++高级篇——模板元编程之十三处理逻辑
c++
格林威4 小时前
Baumer工业相机堡盟工业相机的工业视觉中为什么偏爱“黑白相机”
开发语言·c++·人工智能·数码相机·计算机视觉
小林学习编程4 小时前
SpringBoot校园失物招领信息平台
java·spring boot·后端
撸码到无法自拔4 小时前
docker常见命令
java·spring cloud·docker·容器·eureka
heart000_14 小时前
IDEA 插件推荐:提升编程效率
java·ide·intellij-idea
Dream it possible!5 小时前
LeetCode 热题 100_只出现一次的数字(96_136_简单_C++)(哈希表;哈希集合;排序+遍历;位运算)
c++·leetcode·位运算·哈希表·哈希集合
ŧ榕树先生5 小时前
查看jdk是否安装并且配置成功?(Android studio安装前的准备)
java·jdk