欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > C++ day03

C++ day03

2024/10/26 13:33:45 来源:https://blog.csdn.net/yangh1231/article/details/142400348  浏览:    关键词:C++ day03

思维导图 

 

 

 

头文件

#ifndef SEQLIST_H
#define SEQLIST_Husing 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 insert(int n, datatype e); // 在指定位置插入元素void erase(int n);         // 删除指定位置的元素void pop_back();           // 删除末尾元素void getany(int n);        // 获取任意位置的元素void sort(int n);          // 排序,输入1为升序,输入0为降序
};#endif

源文件 

#include "seqlist.h"
#include <iostream>
using namespace std;// 初始化顺序表
void seqlist::init(int n)
{this->ptr = new datatype[n];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;}else{this->ptr[this->len++] = e;}
}// 显示顺序表中的元素
void seqlist::show()
{cout << "当前顺序表的元素是: ";for (int i = 0; i < this->len; i++){cout << this->ptr[i] << " ";}cout << endl;
}// 在指定位置插入元素
void seqlist::insert(int n, datatype e)
{if (this->full()){return;}else{for (int i = this->len; i >= n; i--){this->ptr[i] = this->ptr[i - 1];}this->ptr[n - 1] = e;this->len++;}
}// 删除指定位置的元素
void seqlist::erase(int n)
{if (this->empty()){return;}else{for (int i = n - 1; i < this->len - 1; i++){this->ptr[i] = this->ptr[i + 1];}this->len--;}
}// 删除末尾元素
void seqlist::pop_back()
{if (this->empty()){return;}else{this->ptr[this->len--] = 0;}
}// 获取任意位置的元素
void seqlist::getany(int n)
{if (n > 0 && n <= this->len){cout << "位置 " << n << " 的元素是: " << this->ptr[n - 1] << endl;}else{cout << "位置无效" << endl;}
}// 排序,输入1为升序,输入0为降序
void seqlist::sort(int n)
{if (n == 1) // 升序排序{for (int i = 0; i < this->len - 1; i++){for (int j = 0; j < this->len - i - 1; j++){if (this->ptr[j] > this->ptr[j + 1]){swap(this->ptr[j], this->ptr[j + 1]);}}}}else if (n == 0) // 降序排序{for (int i = 0; i < this->len - 1; i++){for (int j = 0; j < this->len - i - 1; j++){if (this->ptr[j] < this->ptr[j + 1]){swap(this->ptr[j], this->ptr[j + 1]);}}}}
}

主函数 

#include "seqlist.h"
#include <iostream>
using namespace std;int main()
{seqlist s1;s1.init(8);         // 初始化顺序表,容量为5s1.push_back(1);s1.push_back(2);s1.push_back(3);s1.push_back(5);s1.push_back(6);s1.push_back(7);s1.show();          // 显示顺序表s1.insert(2, 4);    // 在第2个位置插入元素4s1.show();          // 显示顺序表s1.erase(2);        // 删除第2个位置的元素s1.show();          // 显示顺序表s1.pop_back();      // 删除末尾元素s1.show();          // 显示顺序表s1.getany(2);       // 获取第2个位置的元素s1.sort(1);         // 升序排序s1.show();          // 显示顺序表s1.sort(0);         // 降序排序s1.show();          // 显示顺序表return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com