设为首页 加入收藏

TOP

C++堆栈的类模板
2017-07-12 10:23:15 】 浏览:7625
Tags:堆栈 模板
#include 
  
   
#include 
   
     #define maxsize 100//默认堆栈的最大长度为100 using namespace std; template
    
      class Stack { private: T arr[maxsize]; int currentSize;//保存栈顶的位置 public: Stack():currentSize(0) {}//初始化栈顶为0 void push(T temp)//新元素入栈,该元素为栈顶元素(压栈) { arr[currentSize++]=temp; } T pop()//栈顶元素出栈 { T temp=arr[currentSize-1]; arr[currentSize-1]=NULL; currentSize--; return temp; } bool empty()//判断该堆栈是否为空 { return (currentSize==0)?true:false; } bool full()//判断该堆栈是否为满 { return (currentSize==100)?true:false; } int size()//获得该堆栈的当前长度 { return currentSize; } int top()//获得该堆栈当前的栈顶元素 { return currentSize; } }; int main() { //在主函数中分别测试int、char和double型数据 string type=""; int length,tempInt,n; double tempDouble; char tempChar; while(cin>>type&&type!="STOP") { cin>>length; if(type=="Int") { Stack
     
       Int; for(int i=0; i
      
       >tempInt; if(Int.full()) cout<<"full!! "; else Int.push(tempInt); } cin>>n; for(int i=0; i
       
         Char; for(int i=0; i
        
         >tempChar; if(Char.full()) cout<<"full!! "; Char.push(tempChar); } cin>>n; for(int i=0; i
         
          Sample input
          

 

Int
10 1 2 3 4 5 6 7 8 9 10
5
Double
5 0.8 4.5 6.2 5.4 12.9
7
Char
8 g h s a f o i p
6
STOP

Sample output

10 9 8 7 6
12.9 5.4 6.2 4.5 0.8 empty!! empty!!
p i o f a s
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++中引用(&)的用法和应用实例 下一篇c++基础知识学习

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目