Stack sequence Storage Structure-Implementation of sequence Stack

Source: Internet
Author: User

 

# Include <iostream> using namespace std; const int SeqStackSize = 100; template <class DataType> class SeqStack {private: int data [SeqStackSize]; /// the int top pointer of the array that stores the stack elements; // The top pointer of the stack, which is the subscript public: SeqStack () {top =-1;} of the stack top element in the array ;} /// constructor, initialize an empty stack ~ SeqStack () {}/// The Destructor is empty void Push (int x); // in the stack operation, add element x into the stack int Pop (); /// stack exit operation. int GetTop () {if (top! =-1) return data [top];} // get the top element of the stack (not deleted) int Empty () {if (top =-1) return 1; else return 0 ;}/// determine whether the stack is empty}; template <class DataType> void SeqStack <DataType >:: Push (int x) // The inbound stack operation, add element x to the stack {if (top = SeqStackSize-1) throw "overflow"; data [++ top] = x ;} template <class DataType> int SeqStack <DataType>: Pop () // stack exit operation. The top element of the stack is displayed {if (top =-1) throw "underflow"; return data [top --];} int main () {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.