Header file:
#pragma once#include <iostream> #include <assert.h>using namespace Std;template<class type>class Seqstack{public:seqstack (size_t sz = INIT_SZ) ~seqstack ();p ublic:bool empty () Const;bool full () const;void show () const ; bool Push (const Type &x); bool Pop (); void GetTop (Type &x); int length () const;void clear (); void Destory (); void Quit_system (Type &x);p rivate:enum{INIT_SZ = 64}; Type *base;int capacity;int top;}; Template<class type>seqstack<type>::seqstack (size_t sz = INIT_SZ) {capacity = SZ > INIT_SZ? sz:init_sz;ba SE = new Type[capacity];assert (base! = NULL); top = 0;} Template<class Type>seqstack<type>::~seqstack () {destory ();} Determine if the stack is full template<class type>bool seqstack<type>::full () Const{return (top >= capacity);} Determine if the empty stack template<class type>bool seqstack<type>::empty () const{return (top = = 0);} Show Template<class type>void seqstack<type>::show () const{if (top = = 0) {cout << "the stack is empty!" << Endl;return;} for (int i = top-1; I >= 0; i.) {cout << base[i] << Endl;}} Into the stack template<class type>bool seqstack<type>::p ush (const Type &x) {if (full ()) {cout << "the stack is Full,can not enter! "<< Endl;return false;} Else{base[top] = X;top++;return true;}} Out of Stack template<class type>bool seqstack<type>::p op () {if (empty ()) {cout << "the stack is Empty,can not PO p! "<< Endl;return false;} Else{top--;return true;}} Get stack top element template<class type>void seqstack<type>::gettop (Type &x) {x = base[top-1];} Find the stack length template<class type>int seqstack<type>::length () Const{return top;} Empty stack template<class type>void seqstack<type>::clear () {top = 0;} Destroy Stack Template<class type>void seqstack<type>::d estory () {delete[]base;base = null;capacity = top = 0;} Exit system Template<class type>void Seqstack<type>::quit_system (Type &x) {x = 0;}
Main function:
#include "Edlin.h" int main () {seqstack<char> Mystack;char ch;cout << "Please enter:" << endl;cout <& Lt "* @:d elete all $:d elete One *" << endl;while ((ch = getchar ())! = EOF) {switch (ch) {case ' @ ': mystack.cl Ear (); cout << "Please enter:" << endl;break;case ' $ ': Mystack.pop (), Break;default:mystack.push (ch); break;}} Mystack.show (); return 0;}
Application of "Data structure" stack--line editing program (c + +)