(001) C ++ Implementation of sequential Stack

Source: Internet
Author: User

(001) C ++ Implementation of sequential Stack
(001) the C ++ Implementation of the ordered stack is 2014/12/4 jxlijunhao. Here we use the C ++ template class to implement an ordered Stack: 1) initialization 2) inbound stack 3) outbound stack 4) top element of stack
In the ordered stack (in an array), operations always occur at the end of the array.

Define a header file

# Ifndef SQ_STACK_H # define SQ_STACK_H # include
 
  
Using namespace std; template
  
   
Class sq_stack {private: int cap; // total capacity int top; // stack top T * s; // point to the ordered stack (stack bottom) public: sq_stack (int ); // initialize void print_sq_stack (); // print int flag_sq_stack (); // check the status of the sequential stack void push_sq_stack (T); // The inbound stack T delete_sq_stack (); // output stack T read_sq_stack (); // read stack top element}; // initialize template
   
    
Sq_stack
    
     
: Sq_stack (int m) {cap = m; s = new T [cap]; top = 0;} // print template
     
      
Void sq_stack
      
        : Print_sq_stack () {int I; for (I = top; I> 0; I --) {cout <
       
         Int sq_stack
        
          : Flag_sq_stack () {if (top = cap) return-1; // if (top = 0) return 0 when the stack is full; // empty return 1 ;} // stack-based template
         
           Void sq_stack
          
            : Push_sq_stack (T t) {if (top = cap) {cout <"stack overflow" <
           
             T sq_stack
            
              : Delete_sq_stack () {T y; if (top = 0) {cout <"Empty stack" <
             
               T sq_stack
              
                : Read_sq_stack () {if (top = 0) {cout <"Empty stack" <
               
                
#include "sq_stack.h"int main(){sq_stack
                 
                  s(10);s.push_sq_stack(50);s.push_sq_stack(60);s.push_sq_stack(70);s.push_sq_stack(80);s.push_sq_stack(90);s.push_sq_stack(100);s.print_sq_stack();cout<
                  
                   


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.