博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Stack的c++实现(不完整)
阅读量:6785 次
发布时间:2019-06-26

本文共 3769 字,大约阅读时间需要 12 分钟。

hot3.png

Stack.h

#ifndef _STACK_H_#define _STACK_H_namespace Stack{	class Stack_error{		private:			char* error_msg;		public:			Stack_error(char* msg):error_msg(msg){}			const char* what(){				return error_msg;			}	};	template
class Stack_static_array{ private: T arr[MAXLEN]; unsigned count; public: Stack_static_array(); Stack_static_array(unsigned,T); Stack_static_array(const Stack_static_array
&); bool empty() const; bool full() const; void push(const T&) throw(Stack_error); void pop() throw(Stack_error); T get_top() const throw(Stack_error); void print() const; }; template
class Stack_dynamic_array{ private: T *arr; unsigned count; unsigned size; void enlarge(); void lessen(); public: Stack_dynamic_array(); Stack_dynamic_array(unsigned,T); Stack_dynamic_array(const Stack_dynamic_array
&); ~Stack_dynamic_array(); bool empty() const; void push(const T&); void pop() throw(Stack_error); T get_top() const throw(Stack_error); void print() const; }; }#endif#include"Stack.cpp"

Stack.cpp

#include
template
Stack::Stack_static_array
::Stack_static_array():count(0){}template
Stack::Stack_static_array
::Stack_static_array(unsigned len,T element){ count=len; if(len){ for(unsigned i=0;i
Stack::Stack_static_array
::Stack_static_array(const Stack_static_array
& oth_obj){ count=oth_obj.count; for(unsigned i=0;i
bool Stack::Stack_static_array
::empty() const{ return count==0;}template
bool Stack::Stack_static_array
::full() const{ return count==MAXLEN;}template
void Stack::Stack_static_array
::push(const T& element) throw(Stack_error){ if(count==MAXLEN) throw Stack_error("ERROR: FULL Stack!\n"); else{ arr[count]=element; ++count; }}template
void Stack::Stack_static_array
::pop() throw(Stack_error){ if(!count) throw Stack_error("ERROR: EMPTY Stack!\n"); else --count;}template
T Stack::Stack_static_array
::get_top() const throw(Stack_error){ if(!count) throw Stack_error("ERROR: EMPTY Stack!\n"); else return arr[count-1];}template
void Stack::Stack_static_array
::print() const{ using namespace std; for(unsigned i=0;i
Stack::Stack_dynamic_array
::Stack_dynamic_array():count(0),size(1){ arr=new T[size];}template
Stack::Stack_dynamic_array
::Stack_dynamic_array(unsigned mcount,T element):count(mcount){ if(mcount){ if(count<1024){ size=1; while(count>size) size*=2; } else{ for(size=1024;count>size;size+=1024){} } arr=new T[size]; for(unsigned i=0;i
Stack::Stack_dynamic_array
::Stack_dynamic_array(const Stack_dynamic_array
& oth_obj){ count=oth_obj.count; size=oth_obj.size; arr=new T[size]; for(unsigned i=0;i
Stack::Stack_dynamic_array
::~Stack_dynamic_array(){ delete [] arr;}template
void Stack::Stack_dynamic_array
::enlarge(){ if(size<1024) size*=2; else size+=1024; T* temp=new T[size]; for(unsigned i=0;i
void Stack::Stack_dynamic_array
::lessen(){ if(size!=1){ if(size>1024) size-=1024; else size/=2; T* temp=new T[size]; for(unsigned i=0;i
bool Stack::Stack_dynamic_array
::empty() const{ return count==0;}template
void Stack::Stack_dynamic_array
::push(const T& element){ if(count==size) enlarge(); arr[count]=element; ++count;}template
void Stack::Stack_dynamic_array
::pop() throw(Stack_error){ if(!count) throw Stack_error("ERROR: EMPTY Stack!\n"); --count; if(count==size/2||count==size-1024) lessen();}template
T Stack::Stack_dynamic_array
::get_top() const throw(Stack_error){ if(!count) throw Stack_error("ERROR: EMPTY Stack!\n"); return arr[count-1];}template
void Stack::Stack_dynamic_array
::print() const{ using namespace std; for(unsigned i=0;i

有何不妥之处还望斧正 

转载于:https://my.oschina.net/codesun/blog/79949

你可能感兴趣的文章
排序算法-快速排序
查看>>
CSS3 Background 属性介绍
查看>>
frameset 的一些小应用
查看>>
eclipse自动换行
查看>>
Android PDF 阅读器源码
查看>>
我的友情链接
查看>>
silverlight渐隐效果
查看>>
使用Docker实现php代码在线测试执行工具-toolfk.com
查看>>
簡單範例 mergecap,wireshark 付屬程式
查看>>
网络文件传输学习
查看>>
Installation Oracle11gR2 RAC One Node ---创建数据库
查看>>
spring 通过EsClientFactory注入elasticsearch
查看>>
打造中国第一品牌安全网关
查看>>
Android定位功能(二)
查看>>
tomcat的安装及配置
查看>>
用Jquery处理PHP返回的JSON格式数据的三种方法
查看>>
servlet基础
查看>>
云盾防Ddos文献之敌情篇 ——DDoS***原理
查看>>
detached entity passed to persist异常解决
查看>>
批量递归转换目录下pdf文件到swf
查看>>