First Example: Reversing a List # nclude≤ iostream .h> #include <stack> using namespace std void main( / Pre: The user supplies an integer n and n decimal numbers Post: The numbers are printed in reverse order. Uses: the stl class stack and its methods
First Example: Reversing a List #include<iostream.h> #include <stack> using namespace std; void main( ) /* Pre: The user supplies an integer n and n decimal numbers. Post: The numbers are printed in reverse order. Uses: The STL class stack and its methods */
I int n; double item stack <double> numbers. cout<<" Type in an integer n followed by n decimal numbers. "< endl cout<<The numbers ill be printed in reverse order. < endl cIn > n; for(int i=0; i<n; i++) I cin>>nem. numbers. pus tem); 1 cout<<endk<<eIN while(!numbers. emp I cout<<numbers. top()num .pop(; b cout<<end; an element in top of stack is pop off
{ int n; double item; stack <double> numbers; cout<<" Type in an integer n followed by n decimal numbers. " << endl ; cout<<" The numbers will be printed in reverse order. " << endl ; cin >> n; for(int i=0; i<n; i++) { cin>>item; numbers.push(item); } cout<<endl<<endl; while(!numbers.empty( )) { cout<<numbers.top( )<<" "; numbers.pop( ); } cout<<endl; } declares and initializes a stack of numbers input an integer of push stack input an element push stack output an element in top of stack an element in top of stack is pop off
2.2 Implementation of Stacks Constructor The constructor is a function with the same name as the corresponding class and no return type. It is invoked automatically whenever an object of the class is declared Stack Stack Pre: none Post: The Stack exists and is initialized to be empty
Constructor The constructor is a function with the same name as the corresponding class and no return type. It is invoked automatically whenever an object of the class is declared. 2.2 Implementation of Stacks Stack :: Stack( ); Pre: None. Post: The Stack exists and is initialized to be empty
Entry Types, Generics For generality, we use Stack entry for the type of entries in a stack a client can specify this type with a definition such as typedef int Stack entry or typedef char Stack entry i The ability to use the same underlying data structure and operations for different entry types is called generics typedef provides simple generics. Starting in Chapter 6, we shall use C++ templates for greater generality
Entry Types, Generics For generality, we use Stack entry for the type of entries in a Stack. A client can specify this type with a definition such as typedef int Stack entry ; or typedef char Stack entry ; The ability to use the same underlying data structure and operations for different entry types is called generics. typedef provides simple generics. Starting in Chapter 6, we shall use C++ templatesfor greater generality
Error Processing We shall use a single enumerated type called Error code to report errors from all of our programs and functions The enumerated type Error code is part of the utility package in Appendix C Stacks use three values of an Error code success, overflow, underflow Later, we shall use several further values of an error code
Error Processing We shall use a single enumerated type called Error code to report errors from all of our programs and functions. The enumerated type Error code is part of the utility package in Appendix C. Stacks use three values of an Error_code: success, overflow, underflow Later, we shall use several further values of an Error code