Reference Link: Data structure adventure-stacks
After learning the queue, the stack is very simple, the same.
Course Notes:
The model of Stack stack
Programming implementation of the stack
Basic elements of the stack
Basic operation of the stack
Implementation of basic operations
C + + Implementation steps
Standard C + + code
//MyStack.h#pragmaOnceclassmystack{ Public: Mystack (intsize); ~Mystack (); BOOLStackempty (); BOOLStackfull (); voidClearstack (); intstacklength (); BOOLPushCharelem); BOOLPopChar&elem); voidstacktraverse ();Private: Char*M_pbuffer; intm_isize; intm_itop;};
//MyStack.cpp#include"MyStack.h"#include<iostream>using namespacestd; Mystack::mystack (intsize) {M_isize=size; M_pbuffer=New Char[size]; M_itop=0;} Mystack::~Mystack () {Delete[]m_pbuffer;}BOOLMystack::stackempty () {if(0==m_itop) { return true; } return false;}BOOLMystack::stackfull () {if(M_itop = =m_isize) { return true; } return false;}voidMystack::clearstack () {m_itop=0;}intmystack::stacklength () {returnm_itop;}BOOLMystack::p Ush (Charelem) { if(Stackfull ()) {return false; } M_pbuffer[m_itop]=Elem; M_itop++; return true;}BOOLMystack::p op (Char&elem) { if(Stackempty ()) {return false; } m_itop--; Elem=M_pbuffer[m_itop]; return true;}voidMystack::stacktraverse () { for(inti =0; i < m_itop; i++) {cout<< M_pbuffer[i] <<","; }}
//Demo.cpp#include <iostream>#include"MyStack.h"using namespacestd;intMain () {Mystack*p =NewMystack (5); P->push ('h'); P->push ('e'); P->push ('L'); P->push ('I'); P->push ('Z'); P-Stacktraverse (); CharTMP =0; P-pop (TMP); cout<< Endl <<"Ding is"<< tmp <<Endl; P-Clearstack (); cout<< p->stacklength () <<Endl; if(p->Stackempty ()) {cout<<"stack is empty"<<Endl; } if(p->Stackfull ()) {cout<<"Stack is full"<<Endl; } Deletep; P=nullptr; return 0;}
Practical application
Stack-Data structure C + + implementation