Customized version of Stack C ++ (custom version)

Source: Internet
Author: User

[Cpp] # ifndef stack _ # define stack _ # include "stdafx. h "// # include <iostream> // using namespace std; // exception class OutOfBounds {public: OutOfBounds (){}}; // change array's size template <class T> void Changesize1D (T * & arr, const int & size, const int & ToSize) {T * tmp = new T [size]; tmp = arr; arr = new T [ToSize]; for (int I = 0; I <size; ++ I) {arr [I] = tmp [I];} delete [] tmp;} template <class T> linoleic Ss Stack {// friend ostream & operator <(ostream &, const Stack <T> &); public: Stack ();~ Stack () {delete [] stack ;}; T Top () const; void Pop (); void Push (const T & x); int Size () const; bool Empty (); private: int top; int Maxtop; T * stack ;}; template <class T> Stack <T >:: Stack () {stack = new T [1]; top =-1; Maxtop = 0;} template <class T> T Stack <T >:: Top () const {if (top =-1) {throw OutOfBounds () ;}else {return stack [top] ;}} template <class T> void Stack <T> :: pop () {-- top; // change ar Ray capacity when the list size drops to // one-fourth of the array capacity if (top + 1 <= (Maxtop + 1)/4) & Maxtop> 0) {Maxtop = (Maxtop-1)/2; Changesize1D (stack, top + 1, Maxtop + 1) ;}} template <class T> void Stack <T> :: push (const T & x) {// if there is not enough space to accommodate the new element, // double the array capacity if (top = Maxtop) {Maxtop = Maxtop * 2 + 1; Changesize1D (stack, P + 1, Maxtop + 1);} stack [++ top] = x;} template <class T> int Stack <T >:: Size () const {return top + 1 ;}; template <class T> bool Stack <T >:: Empty () {return top ==-1 ;} /* template <class T> ostream & operator <(ostream & out, const Stack <T> & s) {out <"The stack has" <s. size () <"element (s)" <endl; return out;} */# endif Test it [cpp] // Stack. cpp: Defines the entry point for the console Application. // # include "stdafx. h "# include" Stack1.h "# include <iostream> using namespace std; int main (int argc, char * argv []) {Stack <int> t; try {t. top () ;}catch (OutOfBounds e) {cout <"no element" <endl ;}t. push (1); cout <"after push 1, the top:" <t. top () <endl; cout <"after push 1, is empty?: "<T. Empty () <endl; t. Pop (); cout <" after pop, is empty? "<T. Empty () <endl; for (int I = 0; I <10; ++ I) {t. Push (I) ;}while (! T. Empty () {cout <t. Top () <endl; t. Pop () ;}return 0 ;}

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.