数据结构 栈 英文版
Error Processing——error code
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_code Stack :: push(const Stack_entry &item); precondition: None. postcondition: If the Stack is not full, item is added to the top of the Stack. If the Stack is full, an Error_code of overflow is returned and the Stack is left unchanged.
top an-1 an-2
maxstack
bottom
a0
Understand stack
You can imagine stack as this barrel, when you want to put the black pie into the barrel, you just put it on the top of the orange one. Meanwhile, when you want to get the red pie, you should first popup the other two pies, and then you can get the red one.
Stack methods The methods of our class stack must include the following fundamental operations.
empty( ) top( ) push( )
pop( )
Constructor
Entry Types, Generics
2.2 IMPLEMENTATION OF STACKS
contiguous stacks In this Chapter, we will learn contiguous stacks. linked stacks We will learn in Chapter 4.
2.2.1 Specification of Methods for Stacks
Pushing and popping a stack
S M D R A Q
Standard Template Library (STL)
The standard template library (abbreviated STL) in C++ contains much useful information, many functions, and many classes. We can include the STL stack implementation into our programs with the directive #include <stack> and then we can define and initialize empty stack objects and apply methods called push, pop, top, and empty.
a1
a2
a3
a4
a5
a6
question
Read an integer n, which will be at most 25, then read a list of n numbers, and print the list in reverse order.
2.1.2 Stacks
Definition: A stack is a last in-first out (LIFO) data structure in which all insertion and deletion are restrict to one end, called top. Pop Stack Push Stack
Hale Waihona Puke Standard Template Library (STL)
In C++, a template construction allows us to create data structures whose entries have different types. Example: stack<double> numbers stack<int> numbers The STL provides several implementations of various data structures such as stacks. A second, optional template parameter selects the implementation.
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++ templates for greater generality.
2.1.3 First Example: Reversing a List
As a simple example of the use of stacks, let us write a program to solve the problem of Section 2.1.1. Our program must read an integer n, followed by n floating-point numbers. It then writes them out in reverse order. We can accomplish this task by pushing each number into a stack as it is read. When the input is finished, we pop numbers off the stack, and they will come off in the reverse order.
CHAPTER 2 INTRODUCTION TO STACKS
【学习内容】 BASIC CONCEPTS 1. Stack Specifications 2. Implementation of Stacks 3. Application: A Desk Calculator 4. Application: Bracket Matching 5. Abstract Data Types and Their Implementations
Error Processing—exception handling
an exception is produced when a error is detected in a data structure; client can capture the exception and decide how to response. return an error code from implementation.
Specification for Methods
Error_code Stack :: pop( ); precondition: None. postcondition: If the Stack is not empty, the top of the Stack is removed. If the Stack is empty, an Error_code of underflow is returned and the Stack is left unchanged.
#include <stack> #include <iostream> using namespace std; int main( ) { int n; double item; stack<double> numbers; // declares and initializes a stack of numbers cout << " Type in an integer n followed by n decimal numbers." << endl << " 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; return 0;}