(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<