Stack类实现。模板

1// Head.h 2#include <iostream> 3using namespace std; 4 5#ifndef DEFAULT_STACK_SIZE 6#define DEFAULT_STACK_SIZE 1000 7#endif 8 9// end 10// iCoding@CodeLab 11// 12 13// Stack.h 14#include "Head.h" 15 16 17template <typename ElemType> 18class Stack 19{ 20 private: 21 ElemType *data; 22 int size; 23 int bottom; 24 int top; 25 public: 26 Stack (); 27 void push (ElemType elem); 28 ElemType pop (); 29 bool is_empty (); 30 bool is_full (); 31 int get_size (); 32 void expand_size (); 33}; 34 35 36// end 37// iCoding@CodeLab 38// 39 40#include "Stack.h" 41 42/////////////////////////////////////////////////////////// 43// Stack 44template <typename ElemType> 45Stack<ElemType>::Stack () 46{ 47 this->size = DEFAULT_STACK_SIZE; 48 this->data = new ElemType[this->size+1]; 49 this->bottom = 0; 50 this->top = 0; 51} 52 53/////////////////////////////////////////////////////////// 54// push 55template <typename ElemType> 56void Stack<ElemType>::push (ElemType elem) 57{ 58 if (is_full()) 59 { 60 expand_size (); 61 } 62 this->top++; 63 this->data[this->top] = elem; 64} 65/////////////////////////////////////////////////////////// 66// pop 67template <typename ElemType> 68ElemType Stack<ElemType>::pop () 69{ 70 ElemType elem_top; 71 elem_top = this->data[this->top]; 72 this->top--; 73 return elem_top; 74} 75/////////////////////////////////////////////////////////// 76// is empty 77template <typename ElemType> 78bool Stack<ElemType>::is_empty () 79{ 80 return (this->bottom >= this->top); 81} 82 83/////////////////////////////////////////////////////////// 84// is full 85template <typename ElemType> 86bool Stack<ElemType>::is_full () 87{ 88 return (this->size <= this->top); 89} 90 91/////////////////////////////////////////////////////////// 92// get size of Stack 93template <typename ElemType> 94int Stack<ElemType>::get_size () 95{ 96 return (this->top - this->bottom); 97} 98 99/////////////////////////////////////////////////////////// 100// expand_size 101template <typename ElemType> 102void Stack<ElemType>::expand_size () 103{ 104 ElemType* elem_data_tmp; 105 elem_data_tmp = new ElemType[this->size+1]; 106 for (int i = this->bottom + 1; i <= this->top; i++) 107 { 108 elem_data_tmp[i] = this->data[i]; 109 } 110 delete[] this->data; 111 this->size += DEFAULT_STACK_SIZE; 112 this->data = new ElemType[this->size+1]; 113 for (int i = this->bottom + 1; i <= this->top; i++) 114 { 115 this->data[i] = elem_data_tmp[i]; 116 } 117} 118 119// end 120// iCoding@CodeLab 121//
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang

thinkphp3.2.3模板渲染支持三元表达式

thinkphp3.2.3模板渲染支持三元表达式{$status?'正常':'错误'}{$info'status'?$info'msg':$info'error'}注意:三元运算符中暂时不支持点语法。如下:           <divclass"modalhidefade"id'myModa