代码改变世界

stack

  Virus-BeautyCode  阅读(416)  评论(0编辑  收藏  举报
typedef   int   stack_entry;    
  enum   Error_Code1{success1,overflow1,underflow1};    
  const   int   maxstack=10;  
  #include<iostream>  
  using   namespace   std;  
   
  class   stack  
  {  
        public:  
          stack();  
          bool   empty()const;  
          void   clear();  
          bool   full()const;  
          int   size()const;  
          Error_Code1   pop();  
          Error_Code1   top(stack_entry   &item)const;  
          Error_Code1   push(const   stack_entry   &item);  
          stack&   operator=(stack   &source);  
          friend   void   copy_stack(stack   &dest,stack   &source);  
  private:  
                  int   count;  
                  stack_entry     entry[maxstack];          
  };  
  stack   tstack;  
   
  stack&   stack::operator=(   stack   &source)  
  {  
           
        for   (int   i   =   0;   i<count;   i++)  
        {  
              tstack.entry[i]   =   entry[i];  
              tstack.count         =   count;  
        }  
        return   tstack;              
  }    
   
  void     copy_stack(stack   &dest,stack   &source)  
  {  
          dest.count   =   source.count;  
          for   (   int   i   =   0;   i<source.count;   i++)  
          {  
                  dest.entry[i]   =   source.entry[i];        
          }    
  }      
   
   
  Error_Code1   stack::push(const   stack_entry   &item)  
   
  {  
      Error_Code1   outcome   =   success1;  
      if(   count>maxstack)  
         
          outcome   =   overflow1;  
         
      else  
         
        entry[count++]   =   item;  
         
        return   outcome;          
   
  }  
   
  Error_Code1   stack::pop()  
  {  
          Error_Code1   outcome   =   success1;  
          if   (count   ==   0)  
              outcome   =   underflow1;  
          else  
              --count;  
              return   outcome;  
  }  
   
  Error_Code1   stack::top(stack_entry   &item)const  
  {  
          Error_Code1   outcome=success1;  
          if   (count   ==   0)  
             
                  outcome   =   underflow1;  
          else  
                 
            item   =   entry[count-1];  
           
          return   outcome;  
               
  }  
   
  void   stack::clear()  
  {  
          count   =   0;  
          for   (int   i   =   0;   i<maxstack;   i++)  
                {  
                        entry[i]   =   0;  
                }          
  }  
   
   
  bool   stack::full()const  
  {        
  bool   outcome   =   true;  
          if   (count   ==   maxstack)  
                outcome   =   false;  
          return   outcome;          
  }  
   
  int   stack::size()const  
  {  
          return   count-1;  
  }  
           
  bool   stack::empty()const  
  {  
          bool   outcome   =   true;  
          if   (count   ==   0)  
                outcome   =   false;  
          return   outcome;  
                   
  }  
   
  stack::stack()  
  {  
          count   =   0;  
  }  
   
  int   main()  
  {  
           
          int   n;  
          stack   istack,teststack;  
          int   i;  
          int   item[maxstack];  
   
          cout<<"請輸入入棧整數個數(<10):"<<endl;  
          cin>>n;  
          for   (   i   =   0;   i<n;   i++   )  
            {  
                        cin>>item[i];  
                        istack.push(item[i]);  
                       
            }  
            cout<<endl<<endl;  
             
            while   (istack.empty())  
              {    
                      for   (int   j=0;j<n;j++)  
                      {  
                          Error_Code1   Errcode;  
                            Errcode   =   istack.top(item[j]);  
                          cout<<item[j]<<endl;  
                          istack.pop();  
                      }          
              }  
              system("pause");  
              return   0;  
  }   
 
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示