beautypg.com

The stack data abstraction, Include files, Declaration and initialization of stack – HP Integrity NonStop H-Series User Manual

Page 111: Example program - a rpn calculator

background image

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

The Stack Data Abstraction

As a data abstraction, a stack is traditionally defined as any object that implements the following
operations:

empty()

return true if the collection is empty

size()

return number of elements in collection

top()

return (but do not remove) the topmost element in the stack

push(newElement) push a new element onto the stack

pop()

remove (but do not return) the topmost element from the stack

Include Files

Note that accessing the front element and removing the front element are separate operations.
Programs that use the stack data abstraction should include the file stack, as well as the include file
for the container type (e.g., vector).

# include
# include

Right Angle Brackets

Declaration and Initialization of stack

A declaration for a stack must specify two arguments; the underlying element type, and the
container that will hold the elements. For a stack, the most common container is a

vector

or a

deque

,

however a

list

can also be used. The vector version is generally smaller, while the deque version

may be slightly faster. The following are sample declarations for a stack.

stack< int, vector > stackOne;
stack< double, deque > stackTwo;
stack< Part *, list > stackThree;
stack< Customer, list > stackFour;

The last example creates a stack of a programmer-defined type named Customer.

This manual is related to the following products: