"Data Structure" in C + + writing stacks and basic operations (including into the stack, out of the stack, get stack top, destroy, empty, etc.)//header file #ifndef _seq_stack_#define _seq_stack_#include <iostream> Using namespace Std;template <class type>class seqstack{public:seqstack (size_t sz=init_size) {capacity = SZ > Init_size? Sz:init_size;base = new type[capacity];top = 0;} ~seqstack () {destory ();} Public:bool empty () const//Determines whether NULL {return (top = = 0);} BOOL Full () const//determines if the {return (top >= capacity) has been filled;} void push (const Type &X)//Enter stack {if (full ()) {cout << "stack is filled, cannot be inserted. "<< Endl;return;} base[top++] = x;} void pop ()//out stack {top--;} BOOL GetTop (Type &x) const//Get stack top {if (top = = 0) Return false;x = Base[top-1];return true;} int length () const//Seek size {return top;} void clear ()//clear {top = 0;} void Destory ()//Destroy {Delete[]base;base = null;capacity = top = 0;} void Show () const//Display {if (empty () = = 1) {cout << "stack is empty" << endl;reTurn;} for (int i=top-1; I >=0; i--) {cout << Base[i]<<endl;}} private:enum {init_size = 8}; Type *base;int capacity;int top;}; #endif//Main function # include "SeqStack.h" void Main () {seqstack<int> mystack;int select = 1;int item;while (select) {cout << "**************************************" << endl;cout << "* [1] show [2] push *" < ;< endl;cout << "* [3] pop [4] length *" << endl;cout << "* [5] clear [6] D Estory * "<< endl;cout <<" ************************************** "<< endl;cout <<" Please select:> "; c In >> select;switch (select) {case 1:mystack.show (); Break;case 2:cout << "Enter the value to be inserted (-1 end):>" while (CIN >> Item, Item! =-1) {Mystack.push (item);} Break;case 3:mystack.pop (); Break;case 4:cout << "size:" << mystack.length () <<endl;break;case 5: Mystack.clear (); break;case 6:mystack.destory (); break;default:break;}} return;}
"Data Structure" in C + + writing stacks and basic operations (including stack, stack, get stack top, destroy, empty, etc.)