当前位置: 首页 > article >正文

9.20哈好

函数体

#include"SeqList.h"

void SeqList::init(int n)
{
    this->ptr=new data[n];
    
    this->len=0;
    this->size=n;
}

bool SeqList::empty()
{
    return this->len=0;
}

bool SeqList::full()
{
    return this->size==this->len;
}

void SeqList::push_back(data e)
{
    if(this->full())
    {
        return ;
    }
    this->ptr[len++]=e;
}

void SeqList::show()
{
    for(int i=0;i<this->len;i++)
    {
        cout<<"元素"<<len<<this->ptr[i]<<" ";
    }
    cout<<endl;
}

void SeqList::insert(int index)
{
    if(this->full())
    {
        return ;
    }
    if(index>this->len+1||index<=0||index>this->size)
    {
        cout<<"插入错误"<<endl;
        return;
    }
    data e;
    cout<<"输入要插入的元素";
    cin>>e;
    for(int i=len-1;i>=index-1;i--)
    {
        this->ptr[i+1]=this->ptr[i];
    }
    this->ptr[index-1]=e;
    this->len++;
}

void SeqList::delete_s(int index)
{
    if(index>this->len+1||index<=0||index>this->size)
    {
        cout<<"删除位置不合理"<<endl;
        return;
    }
    for(int i=index;i<this->len;i++)
    {
        this->ptr[i-1]=this->ptr[i];
    }
    this->len--;
}

void SeqList::pop_back()
{
     if(this->len==0)
     {
             cout<<"顺序表为空"<<endl;
                               return;
                           }
      this->len--;
                         }
                       

data SeqList::get_len()
{
    return this->len;
}

data SeqList::get_index(int index)
{
    if(index>this->len||index<0||index>=this->size)
    {
        cout<<"位置不合理";
     return -1;    
    }
    return this->ptr[index-1];
}

void SeqList::sort(bool flag)
{
    if(flag){
           for(int i=1;i<this->len;i++){
               for(int j=0;j<this->len-i;j++){
                   if(this->ptr[j]>this->ptr[j+1]){
                       data t = this->ptr[j];
                       this->ptr[j] = this->ptr[j+1];
                       this->ptr[j+1] = t;
                   }
               }
           }
       }
else{
           for(int i=1;i<this->len;i++){
               for(int j=0;j<this->len-i;j++){
                   if(this->ptr[j]<this->ptr[j+1]){
                       data t = this->ptr[j];
                       this->ptr[j] = this->ptr[j+1];
                       this->ptr[j+1] = t;
                   }
               }
           }
       }
   }

头文件

#ifndef SEQLIST_H
#define SEQLIST_H
#include<iostream>
#include<memory.h>
#include<stdlib.h>
#include<string.h>

using data=int;
using namespace std;

class SeqList
{
private:
    data *ptr;
    int size;
    int len=0;
public:
    void init(int n);
    bool empty();
    bool full();
    void push_back(data e);
    void show();
    void insert(int index);
    void delete_s(int index);
    void pop_back();
    data get_len();
    data get_index(int index);
    void sort(bool flag);
};
#endif // SEQLISH_H



主函数

#include"SeqList.h"

using namespace std;

int main()
{
    SeqList s1;
    int n;
    cout << "输入顺序表的大小";
    cin>>n;
    s1.init(5);
    cout<<"输入要插入的数据"<<endl;
    for(int i=0;i<5;i++)
    {
        string s;
        cin>>s;
        int e=stoi(s);
        s1.push_back(e);
    }
    s1.show();
    int add;
    cout<<"输入要插入的位置" ;
    getchar();
    cin>>add;
    s1.insert(add);
    s1.show();
    cout<<"输入要删除的位置";
    cin>>add;
    s1.delete_s(add);
    s1.show();
    int c;
    cout<<"是否尾删(1为yes/2为no)";
    cin>>c;
    if(c==1)
    {
        s1.pop_back();
    }
    s1.show();
    int changdu=s1.get_len();
    cout<<"顺序表长度为"<<changdu<<endl;
    int index;
    cout<<"输入要查找位置的元素";
    cin>>index;
    cout<<index<<"元素"<<s1.get_index(index)<<endl;
    cout<<"(输入1为升序排序/2为降序排序)";
    cin>>c;
    s1.sort(c);
    s1.show();
    return 0;
}

在这里插入图片描述


http://www.kler.cn/news/312466.html

相关文章:

  • 算法【双向广搜】
  • QT Layout布局,隐藏其中的某些部件后,不影响原来的布局
  • 【数据结构】5——哈夫曼树(Huffman Tree)
  • Linux网络——手撕TCP服务器,制定应用层协议,实现网络版计算器
  • websocketpp服务器搭建
  • 使用knn算法对iris数据集进行分类
  • 人力资源数据集分析(一)_t-test、卡方检验和描述性统计
  • Spring Cloud常见面试题
  • 电子电气架构---智能汽车应该是怎么样的架构?
  • 24.9.18学习笔记
  • opengl-redbook环境搭建(静态库)
  • 『功能项目』事件中心处理怪物死亡【55】
  • Vue3:props实现组件通信
  • react 创建react项目
  • 高级java每日一道面试题-2024年9月14日-基础篇-如何处理事务中的性能问题?
  • 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线
  • Bio-Linux-shell详解-1-从0开始
  • 【Ubuntu】虚拟机安装USB摄像头ROS驱动 usb_cam(最新方法)
  • ES5 在 Web 上的现状
  • [ffmpeg] packet
  • element-plus的菜单组件el-menu
  • 7--SpringBoot-后端开发、原理详解(面试高频提问点)
  • Web开发:ABP框架3——入门级别的接口增删改查实现原理
  • 基于SpringBoot的自习室预订系统
  • 莱卡相机sd内存卡格式化了怎么恢复数据
  • Volta无障碍的 JavaScript 工具管理器
  • Java 中使用 Redis 的几种方式优缺点对比
  • Linux 生成 git ssh 公钥
  • 站群服务器适用于哪些场景当中?
  • Linux服务器及应用环境快速部署、调试、迁移、维护、监控