【C++】简易栈的实现
创始人
2025-05-30 23:26:10
0

【C++】简易栈的实现

  • 程序说明
    • 一、博客日期
    • 二、引言
  • 版权声明
  • 开发环境
    • 一、开发配置
    • 二、开发工具
  • 效果演示
  • 核心代码
    • 一、构造类
      • (一)默认长度构造
      • (二)带自扩容构造
      • (三)带初始长度构造
      • (四)析构
    • 二、重载运算符
      • (一)加法重载运算符
      • (二)复合加法重载运算符
    • 三、基本操作
      • (一)入栈
      • (二)出栈
      • (三)读栈
      • (四)获长
      • (五)打印
      • (六)允扩
    • 四、测试
      • (一)测试代码
      • (二)应示效果
  • 全部代码
  • 结语
  • 参考文献

程序说明

一、博客日期

本文第一次发布
2023年03月19日22点30分
Authors
THDMI

二、引言

简单记录下,模板的使用,目前尚且还有一个问题就是,在重载加法运算符的过程中,使用malloc产生的要怎么手动释放,目前在主函数好像不能直接free了,不确定它会不会自动析构。

代码主要描述了一个简单的,实现的过程,太久没用C++了,算是复习一下,另外模板也是第二次用,重载运算符是第一次用,嗯,之前都完全没用到,虽然看过教程,这次算是动手实践,但是内存泄露还是存在。

实际上想加入根据传入的数组初始化栈的,但是想了很久没想出要怎么在未知数组长度情况下,计算一个数组类型的一级指针参数的长度。

版权声明

本工程作品均为本文作者撰写,无其他参考,允许使用在任何场景,作品遵循GPLv3.0开源协议。转载本文请标注出处。

开发环境

一、开发配置

操作系统
Microsoft Windows10 家庭版 20H2

二、开发工具

软件依赖版本类型版本号
LightlyPerson0.7.9

效果演示

具体测试理论效果参考测试部分。
效果

核心代码

一、构造类

(一)默认长度构造

template  Stack::Stack() {this->stack_array = (T*)malloc(sizeof(T) * this->default_stack_len);this->len_max = this->default_stack_len;this->len = 0;
}

(二)带自扩容构造

template  Stack::Stack(bool is_auto_expand) {this->stack_array = (T*)malloc(sizeof(T) * this->default_stack_len);this->len_max = this->default_stack_len;this->len = 0;this->is_auto_expand = true;
}

(三)带初始长度构造

template  Stack::Stack(int stack_len) {this->stack_array = (T*)malloc(sizeof(T) * stack_len);this->len_max = stack_len;this->len = 0;
}

(四)析构

template  Stack::~Stack() {if (nullptr == this->stack_array) {return;}free(this->stack_array);this->stack_array = nullptr;
}

二、重载运算符

(一)加法重载运算符

template  Stack& Stack::operator+(Stack& stack_add) {Stack* new_stack = (Stack*)malloc(sizeof(Stack));new_stack->len = 0;new_stack->len_max = this->len + stack_add.get_len();if (nullptr != new_stack->stack_array) {free(new_stack->stack_array);}new_stack->stack_array = (T*)malloc(sizeof(T) * new_stack->len_max);for (int i = 0; i < this->len; i++) {new_stack->push(this->stack_array[i]);}for (int i = 0; i < stack_add.get_len(); i++) {new_stack->push(stack_add.stack_array[i]);}return *new_stack;
}

(二)复合加法重载运算符

template  Stack& Stack::operator+=(Stack& st) {if (nullptr == this->stack_array) {return *this;}if (this->len_max < this->len + st.len) {this->len_max += st.len;}T* temp = (T*)malloc(sizeof(T) * this->len_max);for (int i = 0; i < this->len; i++) {temp[i] = this->stack_array[i];}free(this->stack_array);this->stack_array = temp;temp = nullptr;for (int i = 0; i < st.len; i++) {this->push(st.stack_array[i]);}return *this;
}

三、基本操作

(一)入栈

template  Rt Stack::push(T ele) {Rt rt;if (this->len_max <= this->len) {if (this->is_auto_expand) {T *tmp = (T*)malloc(sizeof(T) * (this->len_max + this->default_stack_len));this->len_max += this->default_stack_len;memcpy(tmp, this->stack_array, this->len * sizeof(T));free(this->stack_array);this->stack_array = tmp;} else {rt.flag = false;return rt;}}++this->len;this->stack_array[this->len - 1] = ele;rt.flag = true;return rt;
}

(二)出栈

template  Rt Stack::pull() {Rt rt;if (this->len <= 0) {rt.flag = false;return rt;}rt.data = this->stack_array[this->len - 1];--this->len;rt.flag = true;return rt;
}

(三)读栈

template  Rt Stack::read() {Rt rt;if (this->len <= 0) {rt.flag = false;return rt;}rt.data = this->stack_array[len - 1];rt.flag = true;return rt;
}

(四)获长

template  int Stack::get_len() {return this->len; 
}

(五)打印

template  void Stack::print() {for (int i = 0; i < this->len; i++) {std::cout << this->stack_array[i] << "\t";}std::cout << std::endl;
}

(六)允扩

template  void Stack::auto_expand_set(bool option) {this->is_auto_expand = option;
}

四、测试

(一)测试代码

    Stack st1 = Stack();Stack st2 = Stack();Stack st3;for (int i = 0; i < 8; i++) {st1.push("abc");}for (int i = 0; i < 12; i++) {st2.push("bcd");}std::cout << "Stack 01: ";st1.print();std::cout << "\nStack 02: ";st2.print();st1 += st2;std::cout << "\nStack 01: ";st1.print();st3 = st1 + st2;std::cout << "\nStack 03: ";st3.print();

(二)应示效果

  1. st1会入栈8个元素,st2会入栈8个元素
  2. 因为st2允许自动扩容,所以还会再入4个元素
  3. st1st2组合,st1会扩容,st2全部元素会追加在st1末尾
  4. st3将拥有实例,装载有组合后的st1,再次与st2组合

全部代码

#include 
#include 
#include 
#include class None {};template  class Rt {public:bool flag;T data;
};template  class Stack {private:int default_stack_len = 8;bool is_auto_expand = false;T* stack_array;int len;int len_max;public:Stack();Stack(bool is_auto_expand);Stack(int stack_len);~Stack();Stack& operator+(Stack& stack_add);Stack& operator+=(Stack& st);void auto_expand_set(bool option);int get_len();void print();Rt push(T ele);Rt pull();Rt read();
};template  Stack& Stack::operator+(Stack& stack_add) {Stack* new_stack = (Stack*)malloc(sizeof(Stack));new_stack->len = 0;new_stack->len_max = this->len + stack_add.get_len();if (nullptr != new_stack->stack_array) {free(new_stack->stack_array);}new_stack->stack_array = (T*)malloc(sizeof(T) * new_stack->len_max);for (int i = 0; i < this->len; i++) {new_stack->push(this->stack_array[i]);}for (int i = 0; i < stack_add.get_len(); i++) {new_stack->push(stack_add.stack_array[i]);}return *new_stack;
}template  Stack& Stack::operator+=(Stack& st) {if (nullptr == this->stack_array) {return *this;}if (this->len_max < this->len + st.len) {this->len_max += st.len;}T* temp = (T*)malloc(sizeof(T) * this->len_max);for (int i = 0; i < this->len; i++) {temp[i] = this->stack_array[i];}free(this->stack_array);this->stack_array = temp;temp = nullptr;for (int i = 0; i < st.len; i++) {this->push(st.stack_array[i]);}return *this;
}template  Stack::Stack() {this->stack_array = (T*)malloc(sizeof(T) * this->default_stack_len);this->len_max = this->default_stack_len;this->len = 0;
}template  Stack::Stack(bool is_auto_expand) {this->stack_array = (T*)malloc(sizeof(T) * this->default_stack_len);this->len_max = this->default_stack_len;this->len = 0;this->is_auto_expand = true;
}template  Stack::Stack(int stack_len) {this->stack_array = (T*)malloc(sizeof(T) * stack_len);this->len_max = stack_len;this->len = 0;
}template  Stack::~Stack() {if (nullptr == this->stack_array) {return;}free(this->stack_array);this->stack_array = nullptr;
}template  void Stack::auto_expand_set(bool option) {this->is_auto_expand = option;
}template  int Stack::get_len() {return this->len;
}template  void Stack::print() {for (int i = 0; i < this->len; i++) {std::cout << this->stack_array[i] << "\t";}std::cout << std::endl;
}template  Rt Stack::push(T ele) {Rt rt;if (this->len_max <= this->len) {if (this->is_auto_expand) {T* tmp = (T*)malloc(sizeof(T) *(this->len_max + this->default_stack_len));this->len_max += this->default_stack_len;memcpy(tmp, this->stack_array, this->len * sizeof(T));// for (int i = 0; i < this->len; i ++) {//     tmp[i] = this->stack_array[i];// }free(this->stack_array);this->stack_array = tmp;} else {rt.flag = false;return rt;}}++this->len;this->stack_array[this->len - 1] = ele;rt.flag = true;return rt;
}template  Rt Stack::pull() {Rt rt;if (this->len <= 0) {rt.flag = false;return rt;}rt.data = this->stack_array[this->len - 1];--this->len;rt.flag = true;return rt;
}template  Rt Stack::read() {Rt rt;if (this->len <= 0) {rt.flag = false;return rt;}rt.data = this->stack_array[len - 1];rt.flag = true;return rt;
}int main() {Stack st1 = Stack();Stack st2 = Stack(true);Stack st3;for (int i = 0; i < 8; i++) {st1.push("abc");}for (int i = 0; i < 12; i++) {st2.push("bcd");}std::cout << "Stack 01: ";st1.print();std::cout << "\nStack 02: ";st2.print();st1 += st2;std::cout << "\nStack 01: ";st1.print();st3 = st1 + st2;std::cout << "\nStack 03: ";st3.print();
}

结语

多看、多查、多练

参考文献

[1] C++ Primer 第5版 中文版 (Stanley B. Lippman等著,王刚等译)


END

相关内容

热门资讯

《GreenPlum系列-部署... GreenPlum单机节点快速安装教程 一、创建用户及用户组 [root@zxy_maste...
《海蒂》读后感 《海蒂》优秀读后感(精选13篇)  当品读完一部作品后,想必你有不少可以分享的东西,这时最关键的读后...
读骆驼祥子有感 读骆驼祥子有感(精选51篇)  细细品味一本名著后,相信大家都积累了属于自己的读书感悟,不妨坐下来好...
开学有感作文 开学有感作文450字(通用80篇)  在日常学习、工作和生活中,大家都不可避免地要接触到作文吧,作文...
《无常》读后感 《无常》读后感  当细细品完一本名著后,想必你有不少可以分享的东西,此时需要认真地做好记录,写写读后...
Django 之Logging 1. logging 1.1 什么是 logging logging 模块是 Python 内置的日...
Jetpack:DataBin... 文章目录DataBinding 的使用DataBinding 的布局绑定过程DataBinding ...
ThingsBoard开源物联... ThingsBoard部署教程文档 文章目录ThingsBoard部署教程文档1. JDK环境安装2...
爱情宣言个性签名 爱情宣言个性签名(精选140句)  随着移动互联网和社交网络的发展,越来越多人钟情于在线上设置自己的...
读《三顾茅庐》有感作文 读《三顾茅庐》有感作文刘备是个成功的人,在时局混乱,群雄争霸的东汉后期,经过多年征战,最后与孙权、曹...
大数据简介 大数据概论和职业规划Linux服务器系统Hadoop概论HDFS分布式文件系统Hive数据仓库Spa...
銮铃 读吴伯萧《马》有感作文... 銮铃 (读吴伯萧《马》有感)作文 -读后感作文三岁时槐树阴下唱马咿咿呀呀旧桃树新满园姐姐已出嫁夕阳下...
傅雷家书的读后感 傅雷家书的读后感(精选20篇)  品味完一本名著后,相信大家的收获肯定不少,需要好好地就所收获的东西...
Hyper-V 备份软件? 推... Microsoft Hyper-V 虚拟化因其显著降低运营成本而广受欢迎。Hyper-V 虚拟机每天...
《只有偏执狂才能生存》读后感 《只有偏执狂才能生存》读后感  当品读完一部作品后,相信大家有很多值得分享的东西吧,这时就有必须要写...
Github学生包申请秒过经验... 写在前面 前提是在校学生,且有学校邮箱,当然你也得有Github账户前往...
淘气包埃米尔读后感 淘气包埃米尔读后感(精选31篇)  认真读完一本著作后,相信大家一定领会了不少东西,不能光会读哦,写...
《医林改错》读后感 《医林改错》读后感  一直听老师上时提到《医林改错》,寒假有空便阅读了一遍,在我看,它确实是中医史上...
读遇见未知的自己有感 读遇见未知的自己有感(精选19篇)  认真读完一本名著后,大家对人生或者事物一定产生了许多感想,写一...
C++初阶——入门(2)引用 目录 6. 引用 6.1 引用概念 6.2 引用特性 6.3 常引用 6.4 使用场景 1. 做参数...