Stack class templates

Source: Internet
Author: User

Stacks are linear groups that can only be accessed from one end, which is called the top of the stack, and the other end is called the bottom of the stack. A stack is a last-in-first-out data structure.

Stack

Application examples of stacks--expression processing

The basic state of the stack
    • Stack empty
    • Stack full
    • General status
Stack empty
    • There are no elements in the stack (for example, an array holds the stack)

Stack full
    • The number of elements in the stack reaches the upper limit (in the case of stacks accommodated in arrays)

General status
    • There are elements in the stack, but not up to the full state (as an example of a stack accommodated in an array)

Basic operation of the stack
    • Initialization
    • Into the stack
    • Out of the stack
    • Empty stack
    • Access top of Stack element
    • Check the status of the stack (full, empty)
Example 9-8 Stack class template
//Stack.h#ifndef Stack_h#defineStack_h#include<cassert>Template<classTintSIZE = ->classStack {Private: T list[size]; inttop; Public: Stack (); voidPushConstT &item);    T pop (); voidClear (); ConstT &peek ()Const; BOOLIsEmpty ()Const; BOOLIsfull ()Const;};//implementation of the templateTemplate <classTintSize>Stack<t, Size>::stack (): Top (-1) {} template<classTintSize>voidStack<t, Size>::p Ush (ConstT &Item) {Assert (!isfull ()); list[++top] =item;} Template<classTintSize>T Stack<t, size>::p op () {Assert (!isEmpty ()); returnlist[top--]; }template<classTintSize>ConstT &stack<t, Size>::p eek ()Const{assert (!isEmpty ()); returnList[top];//returns the top element of the stack}template<classTintSize>BOOLStack<t, Size>::isempty ()Const {    returntop = =-1;} Template<classTintSize>BOOLStack<t, Size>::isfull ()Const {       returntop = = SIZE-1;} Template<classTintSize>voidStack<t, size>:: Clear () {Top= -1;}#endif  //Stack_h

Stack class template

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.