CHAPTER 2 INTRODUCTION TO STACKS 【 Problem 】 Read an integer n,which will be at most 25,then read a list of n numbers,and print the list in reverse order.

Slides:



Advertisements
Similar presentations
首 页 首 页 上一页 下一页 本讲内容 投影法概述三视图形成及其投影规律平面立体三视图、尺寸标注 本讲内容 复习: P25~P31 、 P84~P85 作业: P7, P8, P14[2-32(2) A3 (1:1)]
Advertisements

第十二章 常微分方程 返回. 一、主要内容 基本概念 一阶方程 类 型 1. 直接积分法 2. 可分离变量 3. 齐次方程 4. 可化为齐次 方程 5. 全微分方程 6. 线性方程 类 型 1. 直接积分法 2. 可分离变量 3. 齐次方程 4. 可化为齐次 方程 5. 全微分方程 6. 线性方程.
Chapter 2 INTRODUCTION TO STACKS. Outline 1. Stack Specifications 2. Implementation of Stacks 3. Application: A Desk Calculator 4. Application: Bracket.
退 出退 出退 出退 出 上一页 下一页 仪器使用 §1-2 尺规绘图工具和仪器的使用方法 图板丁字尺三角板 比例尺圆规分规铅笔曲线板。 要提高绘图的准确度和绘图效率,必须正确地使用各种绘图工 具和仪器。常用的手工绘图工具和仪器有图板、丁字尺、三角板、 比例尺、圆规、分规、铅笔、曲线板等。 提示:将光标放在仪器上,
第 12 章位运算 C 语言兼具高级语言及低级语言的特性,因此 适合编写系统软件。 C 语言具备低级语言的特性 就在于它能直接对硬件进行操作,即位运算。 所谓位运算是指,按二进制位进行的运算。 例如,将一个存储单元中各二进位左移或右移一 位等。
数据挖掘实验 1 Apriori 算法编程实现. 数据挖掘实验一 (20’) 实验目的:了解关联规则在数据挖掘中的 应用,理解和掌握关联挖掘的经典算法 Apriori 算法的基本原理和执行过程并完成程 序设计。 实验内容:对给定数据集用 Apriori 算法进行 挖掘,找出其中的频繁集并生成关联规则。
一、拟合优度检验 二、变量的显著性检验 三、参数的置信区间
第二章 质点组力学 质点组:许多(有限或无限)相 互联系的质点组成的系统 研究方法: 1. 分离体法 2. 从整体考虑 把质点的三个定理推广到质点组.
主要内容  LR(0) 分析. 0 S→  E # E→  E+T E→  T T→  id T→  ( E ) 1 S→E  # E→E  +T 5 T→id  3 E→E+  T T→  id T→  (E) 4 E→E+T  9 E→T  6 T→(  E) E→
第 4 章 抽象解释 内容概述 以一种独立于编程语言的方式,介绍抽象解释的 一些本质概念 – 将 “ 程序分析对语言语义是正确的 ” 这个概念公式 化 – 用 “ 加宽和收缩技术 ” 来获得最小不动点的较好的 近似,并使所需计算步数得到限制 – 用 “ 伽罗瓦连接和伽罗瓦插入 ” 来把代价较大的属 性空间用代价较小的属性空间来代替.
第 2 章 数据流分析 内容概述 数据流分析推导的是数据沿着程序执行路 径流动的信息 – 过程内的分析:可用表达式分析、到达-定值分 析等 – 过程间分析 –Shape 分析 – 理论基础 – 数据流方程的求解.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第六十二讲 ) 离散数学. 最后,我们构造能识别 A 的 Kleene 闭包 A* 的自动机 M A* =(S A* , I , f A* , s A* , F A* ) , 令 S A* 包括所有的 S A 的状态以及一个 附加的状态 s.
1 为了更好的揭示随机现象的规律性并 利用数学工具描述其规律, 有必要引入随 机变量来描述随机试验的不同结果 例 电话总机某段时间内接到的电话次数, 可用一个变量 X 来描述 例 检测一件产品可能出现的两个结果, 也可以用一个变量来描述 第五章 随机变量及其分布函数.
数 学 系 University of Science and Technology of China DEPARTMENT OF MATHEMATICS 第 3 章 曲线拟合的最小二乘法 给出一组离散点,确定一个函数逼近原函数,插值是这样 的一种手段。在实际中,数据不可避免的会有误差,插值函 数会将这些误差也包括在内。
有限自动机 (Finite Automata) 描述程序设计语言中的单词的识别过程。 主要内容: 确定有限自动机 DFA(Deterninistic FA) 确定有限自动机 DFA 的实现 非确定有限自动机 NFA(Nondeterninistic FA) NFA 到 DFA 的转换 DFA 的化简.
例9:例9: 第 n-1 行( -1 )倍加到第 n 行上,第( n-2 ) 行( -1 )倍加到第 n-1 行上,以此类推, 直到第 1 行( -1 )倍加到第 2 行上。
主讲教师:陈殿友 总课时: 124 第八讲 函数的极限. 第一章 机动 目录 上页 下页 返回 结束 § 3 函数的极限 在上一节我们学习数列的极限,数列 {x n } 可看作自变量 为 n 的函数: x n =f(n),n ∈ N +, 所以,数列 {x n } 的极限为 a, 就是 当自变量 n.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第三十八讲 ) 离散数学. 第八章 格与布尔代数 §8.1 引 言 在第一章中我们介绍了关于集 合的理论。如果将 ρ ( S )看做 是集合 S 的所有子集组成的集合, 于是, ρ ( S )中两个集合的并 集 A ∪ B ,两个集合的交集.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第四十八讲 ) 离散数学. 例 设 S 是一个非空集合, ρ ( s )是 S 的幂集合。 不难证明 :(ρ(S),∩, ∪,ˉ, ,S) 是一个布尔代数。 其中: A∩B 表示 A , B 的交集; A ∪ B 表示 A ,
第十一章 曲线回归 第一节 曲线的类型与特点 第二节 曲线方程的配置 第三节 多项式回归.
2.4 基本设计表达式 随机变量的统计特征值 结构的可靠性与可靠 基本设计表达式.
线性代数习题课 吉林大学 术洪亮 第一讲 行 列 式 前面我们已经学习了关 于行列式的概念和一些基本 理论,其主要内容可概括为:
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第二十五讲 ) 离散数学. 定理 群定义中的条件 ( 1 )和( 2 )可以减弱如下: ( 1 ) ’ G 中有一个元素左壹适合 1 · a=a; ( 2 ) ’ 对于任意 a ,有一个元素左逆 a -1 适 合 a -1 ·
第二章 随机变量及其分布 第一节 随机变量及其分布函数 一、随机变量 用数量来表示试验的基本事件 定义 1 设试验 的基本空间为 , ,如果对试验 的每一个基 本事件 ,规定一个实数记作 与之对应,这样就得到一个定义在基本空 间 上的一个单值实函数 ,称变量 为随机变量. 随机变量常用字母 、 、 等表示.或用.
数 学 系 University of Science and Technology of China DEPARTMENT OF MATHEMATICS 第 3 章 曲线拟合的最小二乘法 给出一组离散点,确定一个函数逼近原函数,插值是这样的一种手段。 在实际中,数据不可避免的会有误差,插值函数会将这些误差也包括在内。
自顶向下分析 —— 递归下降法 递归下降法 (Recursive-Descent Parsing) 对每个非终极符按其产生式结构产生相应语 法分析子程序. 终极符产生匹配命令 非终极符则产生调用命令 文法递归相应子程序也递归,所以称这种方 法为递归子程序方法或递归下降法。
语义分析 主要内容:  语义分析概述 ( 必要性、功能、描述方法 )  符号表  类型表达式  声明和程序体的语义分析.
Chapter 10 m-way 搜尋樹與B-Tree
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第四十五讲 ) 离散数学 模 格 定义 设( L , ≤ ) 是一个格,对任意 a , b , c ∈ L , 如果 a≤b ,都有 a  ( b×c ) = b× ( a  c ) 则称( L , ≤ )为模格。
OS 进程调度模拟演示 制作人: 钱晶 高上上. OS 进程调度模拟-实验原理 静态优先级原理 在这种方式下,系统一旦把处理机分配给就绪队 列中的优先权最高的进程后,该进程便一直执行下去, 直至完成。或因为发生某事件使该进程放弃处理机,系 统方可再将处理机分配给另一优先级最高的进程。这些 事件包括有优先级更高的进程进入,或是因为某些原因.
第二章 贝叶斯决策理论 3学时.
流态化 概述 一、固体流态化:颗粒物料与流动的流体接触,使颗粒物料呈类 似于流体的状态。 二、流态化技术的应用:流化催化裂化、吸附、干燥、冷凝等。 三、流态化技术的优点:连续化操作;温度均匀,易调节和维持; 气、固间传质、传热速率高等。 四、本章基本内容: 1. 流态化基本概念 2. 流体力学特性 3.
《 数据结构》课程 中国科学技术大学网络学院 数据结构 第四章字符串. 本章内容 4.1 串的基本概念 4.2 串的存储结构 4.3 串的基本运算的实现 习题 4.
量子化学 第四章 角动量与自旋 (Angular momentum and spin) 4.1 动量算符 4.2 角动量阶梯算符方法
数 学 系 University of Science and Technology of China DEPARTMENT OF MATHEMATICS 第 5 章 解线性方程组的直接法 实际中,存在大量的解线性方程组的问题。很多数值方 法到最后也会涉及到线性方程组的求解问题:如样条插值的 M 和.
主讲教师:陈殿友 总课时: 124 第十一讲 极限的运算法则. 第一章 二、 极限的四则运算法则 三、 复合函数的极限运算法则 一 、无穷小运算法则 机动 目录 上页 下页 返回 结束 §5 极限运算法则.
在发明中学习 线性代数 概念的引入 李尚志 中国科学技术大学. 随风潜入夜 : 知识的引入 之一、线性方程组的解法 加减消去法  方程的线性组合  原方程组的解是新方程的解 是否有 “ 增根 ” ?  互为线性组合 : 等价变形  初等变换  高斯消去法.
数 学 系 University of Science and Technology of China DEPARTMENT OF MATHEMATICS 第 7 章 矩阵的特征值和特征向量 很多工程计算中,会遇到特征值和特征向量的计算,如: 机械、结构或电磁振动中的固有值问题;物理学中的各种临界 值等。这些特征值的计算往往意义重大。
9的乘法口诀 1 .把口诀说完全。 二八( ) 四六( ) 五八( ) 六八( ) 三七( ) 三八( ) 六七( ) 五七( ) 五六( ) 十六 四十八 四十二 二十四 二十一 三十五 四十 二十四 三十 2 .口算, 并说出用的是哪句口诀。 8×8= 4×6= 7×5= 6×8= 5×8=
量子力学教程 ( 第二版 ) 3.4 连 续 谱 本 征 函 数 的 归 一 化 连续谱本征函数是不能归一化的 一维粒子的动量本征值为的本征函数 ( 平面波 ) 为 可以取 中连续变化的一切实数值. 不难看出,只要则 在量子力学中, 坐标和动量的取值是连续变化 的 ; 角动量的取值是离散的.
最 小 公 倍 数最 小 公 倍 数 最 小 公 倍 数最 小 公 倍 数. 例题 顺次写出 4 的几个倍数和 6 的几个倍数,它们 公有的倍数是哪几个?其中最小的是多少? 4 的倍数有 : 4 , 8 , 12 , 16 , 20 , 24 , 28 , 32 , 36 , … 6 的倍数有 :
第 3 章 控制流分析 内容概述 – 定义一个函数式编程语言,变量可以指称函数 – 以 dynamic dispatch problem 为例(作为参数的 函数被调用时,究竟执行的是哪个函数) – 规范该控制流分析问题,定义什么是可接受的控 制流分析 – 定义可接受分析在语义模型上的可靠性 – 讨论分析算法.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第五十三讲 ) 离散数学. 定义 设 G= ( V , T , S , P ) 是一个语法结构,由 G 产生的语言 (或者说 G 的语言)是由初始状态 S 演绎出来的所有终止符的集合, 记为 L ( G ) ={w  T *
平行线的平行公理与判定 九年制义务教育七年级几何 制作者:赵宁睿. 平行线的平行公理与判定 要点回顾 课堂练习 例题解析 课业小结 平行公理 平行判定.
编译原理总结. 基本概念  编译器 、解释器  编译过程 、各过程的功能  编译器在程序执行过程中的作用  编译器的实现途径.
周期信号的傅里叶变换. 典型非周期信号 ( 如指数信号, 矩形信号等 ) 都是满足绝对可 积(或绝对可和)条件的能量信号,其傅里叶变换都存在, 但绝对可积(或绝对可和)条件仅是充分条件, 而不是必 要条件。引入了广义函数的概念,在允许傅里叶变换采用 冲激函数的前提下, 使许多并不满足绝对可积条件的功率.
 符号表  标识符的作用: 声明部分:定义了各种对象及对应的属性和 使用规则。 程序体:对所定义的对象进行各种操作。 $ididname IdnameAttributeIR  必要性 Token : 新表-符号表(种类、类型等信息):
初中几何第三册 弦切角 授课人: 董清玲. 弦切角 一、引入新课: 什么是圆心角、圆周角、圆周角定理的内容是什么? 顶点在圆心的角叫圆心角。 顶点在圆上,并且两边都和圆相交的角叫做圆周角。 定理:一条弧所对的圆周角等于它所对的圆心角的一半。 A B′ C B O.
Department of Mathematics 第二章 解析函数 第一节 解析函数的概念 与 C-R 条件 第二节 初等解析函数 第三节 初等多值函数.
Data Structures Chapter 2 Stacks Andreas Savva. 2 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one.
Object Oriented Data Structures
首 页 首 页 上一页 下一页 本讲内容本讲内容 视图,剖视图(Ⅰ) 复习: P107 ~ P115 作业: P48(6-2,6-4), P49( 去 6-6) P50, P51(6-13), P52 P50, P51(6-13), P52 P53 (6-18,6-20) P53 (6-18,6-20)
《 UML 分析与设计》 交互概述图 授课人:唐一韬. 知 识 图 谱知 识 图 谱知 识 图 谱知 识 图 谱.
1 、如果 x + 5 > 4 ,那么两边都 可得 x >- 1 2 、在- 3y >- 4 的两边都乘以 7 可得 3 、在不等式 — x≤5 的两边都乘以- 1 可得 4 、将- 7x — 6 < 8 移项可得 。 5 、将 5 + a >- 2 a 移项可得 。 6 、将- 8x < 0.
Introduction to Stacks Chapter 2. Objectives Introduce abstract data types. Discuss implementation types. – Static – Dynamic – Contiguous Introduce the.
名探柯南在侦查一个特大盗窃集团过程 中,获得藏有宝物的密码箱,密码究竟 是什么呢?请看信息: ABCDEF( 每个字 母表示一个数字 ) A :是所有自然数的因数 B :既有因数 5 ,又是 5 的倍数 C :既是偶数又是质数 D :既是奇数又是合数 EF :是 2 、 3 、 5 的最小公倍数.
项目七: PLC 功能指令应用 带进位循环左移指令 XXXXX. 项目七: PLC 功能指令应用 FX2 系列可编程控制器移位控制指令有移位、循环移位、字移位 及先进先出 FIFO 指令等 10 条指令。 带进位循环右移指令 RCR 带进位循环左移指令 RCL 字右移位指令 WSFR 先入先出读出指令.
§10.2 对偶空间 一、对偶空间与对偶基 二、对偶空间的有关结果 三、例题讲析.
7 生产费用在完工产品与在产 品之间分配的核算. 2 第七章 生产费用在完工产品与在产品之 间的分配  知识点 :  理解在产品的概念  掌握生产费用在完工产品与在产品之间的分 配.
Chapter 2 INTRODUCTION TO STACKS 1. Stack Specifications 2. Implementation of Stacks 3. Application: A Desk Calculator 4. Application: Bracket Matching.
力的合成 力的合成 一、力的合成 二、力的平行四边形 上一页下一页 目 录 退 出. 一、力的合成 O. O. 1. 合力与分力 我们常常用 一个力来代替几个力。如果这个 力单独作用在物体上的效果与原 来几个力共同作用在物体上的效 果完全一样,那么,这一个力就 叫做那几个力的合力,而那几个 力就是这个力的分力。
逻辑设计基础 1 第 7 章 多级与(或)非门电路 逻辑设计基础 多级门电路.
1 第 4 章 速度瞬心及其应用 ● 利用速度瞬心进行机构的速度分析 ● 将低副机构转变为高副机构 ( 瞬心线机 构,共轭曲线机构) ● 用低副机构的分析方法对高副机构进 行结构和运动分析 ( 高副低代 )
人 有 悲 欢 离 合, 月有阴晴圆缺。月有阴晴圆缺。 华师大版七年级数学第二册 海口市第十中学 数学组 吴锐.
3D 仿真机房建模 哈尔滨工业大学 指导教师:吴勃英、张达治 蒋灿、杜科材、魏世银 机房尺寸介绍.
1 第三章 数列 数列的概念 考点 搜索 ●数列的概念 ●数列通项公式的求解方法 ●用函数的观点理解数列 高考 猜想 以递推数列、新情境下的 数列为载体, 重点考查数列的通 项及性质, 是近年来高考的热点, 也是考题难点之所在.
目录 上页 下页 返回 结束 二、无界函数反常积分的审敛法 * 第五节 反常积分 无穷限的反常积分 无界函数的反常积分 一、无穷限反常积分的审敛法 反常积分的审敛法  函数 第五章 第五章.
本章讨论有限自由度结构系统,在给定载荷和初始条件激励下的系统动力响应计算方法。 第 六 章
Session 03 - Templates Outline 03.1Introduction 03.2Function Templates 03.3Overloading Template Functions 03.4Class Templates 03.5Class Templates and Non-type.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Presentation transcript:

CHAPTER 2 INTRODUCTION TO STACKS 【 Problem 】 Read an integer n,which will be at most 25,then read a list of n numbers,and print the list in reverse order. Read an integer n,which will be at most 25,then read a list of n numbers,and print the list in 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

Stacks  A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. The last entry which is inserted is the first one that will be removed.

Last in,first out (LIFO) Push and Pop

§2.1 The Stack Abstract Data Type A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are made at the top only 〖 Example 〗 System Stack Return Address Stack Frame s p Local Variables Return Address s p Old Frame Pointer s p f p s p f p

 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  and then we can then initially empty stack objects and apply methods called push, pop, top, and empty. Standard Template Library (STL)

 In C++, a template construction allows us to create data structures whose entries have different types. Example: stack numbers  The STL provides several implementations of various data structures such as stacks.  A second, optional template parameter selects the implementation. Standard Template Library (STL)

First Example: Reversing a List

2.2 IMPLEMENTATION OF STACKS  顺序栈 Contiguous Stack  链栈 Linked Stack Use classes to implement the data structures. Thus,we shall now develop a class Stack whose data members represent the entries of a stack.

2.2.1 Specification of Method for Stacks  Stack methods:  empty()  top()  push()  pop()

1.Constructor

 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.Entry Types, Generics

 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. 3.Error Processing

4. Specification for Methods structure Stack is objects: A finite ordered list with zero or more elements. Error code Stack :: pop( ); Pre: None. Post: 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. Error code Stack :: push(const Stack entry &item); Pre: None. Post: 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. Error code Stack :: top(Stack entry &item) const; Pre: None. Post: The top of a nonempty Stack is copied to item. A code of fail is returned if the Stack is empty. bool Stack :: empty( ) const; Pre: None. Post: A result of true is returned if the Stack is empty, otherwise false is returned.

2.2.2 Class Specification Contiguous Stack

Representation of Data in a Contiguous Stack

Stack Methods

Further Stack Methods

#include template class Stack { private: int top; // 栈顶指针 Type *elements; // 栈元素数组 int maxSize; // 栈最大容量 无计算器的栈的数组存储表示 maxSize-1 栈空 top ( 栈空 ) elements

public: Stack (int sz = 10); // 构造函数 ~Stack ( ) { delete [ ] elements; } void Push (Type x); // 进栈 int Pop (Type& x); // 出栈 int GetTop (Type& x); // 取栈顶 void MakeEmpty ( ) { top = - 1; } // 置空栈 int IsEmpty ( ) const { return top == - 1; } int IsFull ( ) const { return top == maxSize - 1; } }

top 空栈 top a 进栈 b 进栈 aa b a b c d e e 进栈 a b c d e f 进栈溢出 a b d e e 退栈 c

top c 退栈 b 退栈 a b a a 退栈 空栈空栈 top a b d d 退栈 c top a b c

Reverse Polish Calculator In a Reverse Polish calculator, the operands (numbers, usually) are entered before an operation (like addition, subtraction, multiplication, or division) is specified. The operands are pushed onto a stack. When an operation is performed, it pops its operands from the stack and pushes its result back onto the stack.

表达式求值 一个表达式由操作数 ( 亦称运算对象 ) 、操作符 ( 亦称运算符 ) 和分界符组成。 一个表达式由操作数 ( 亦称运算对象 ) 、操作符 ( 亦称运算符 ) 和分界符组成。 算术表达式有三种表示: 算术表达式有三种表示:  中缀 (infix) 表示 , ,如 A+B ;  前缀 (prefix) 表示 ,如 +AB ;  后缀 (postfix) 表示 ,如 AB+ ;

例如 : Exp = a  b + (c  d / e)  f 前缀式 : +  a b   c / d e f 中缀式 : a  b + c  d / e  f 后缀式 : a b  c d e /  f  + 结论 : 1 )操作数之间的相对次序不变 ; 2 )运算符的相对次序不同 ; 3 )中缀式丢失了括弧信息, 致使运算的次序不确定 ; 4) 前缀式的运算规则为 : 连续出现的两个操作数和在它们 之前且紧靠它们的运算符构成一 个最小表达式 ; 5) 后缀式的运算规则为 : 运算符在式中出现的顺序恰为 表达式的运算顺序 ; 每个运算符和在它之前出现 且 紧靠它的两个操作数构成一个最小 表达式 ;

如何从后缀式求值? 先找运算符, 再找操作数 例如: a b  c d e /  f  + abab d/ed/e c-d/e (c-d/e)  f

 ? : denote an instruction to read an operand and push it onto the stack;  +,-,*,/: represent arithmetic operations;  = :is an instruction to print the top of the stack(but not pop it off) 

Obtaining a Command The auxiliary function get command obtains a command from the user, checking that it is valid and converting it to lowercase by using the string function tolower( ) that is declared in the standard header le cctype.

 Programs written in C++ contain several different types of brackets.For example , brackets are used to enclose expressions,function arguments,array indices,and blocks of code.  The brackets used within a program must pair off.  For example,the following string  {a=(1+v(b[3+c[4]]))  {a=(b[0)+1];} Application: Bracket Matching

括号匹配的检验 假设在表达式中 ([]())或[([ ][ ])] 等为正确的格式, [( ])或([( ))或 (( )]) 均为不正确的格式。 则 检验括号是否匹配的方法可用 “ 期待的急迫程度 ” 这个概念来描述。

分析可能出现的不匹配的情况 :  到来的右括弧非是所 “ 期待 ” 的 ; 例如:考虑下列括号序列: [ ( [ ] [ ] ) ]  到来的是 “ 不速之客 ”;  直到结束,也没有到来所 “ 期待 ” 的括弧 ;

Bracket Matching  We develop a program to check that brackets are correctly matched in an input text file.  We limit our attention to the brackets {, }, (, ), [, and ].  We read a single line of characters and ignore all input other than bracket characters.  Algorithm: Read the program file character by character. Each opening bracket (, [, or { that is encountered is considered as unmatched and is stored until a matching bracket can be found. Any closing bracket ), ], or } must correspond, in bracket style, to the last unmatched opening bracket, which should now be retrieved and removed from storage. Finally, at the end of the program, we must check that no unmatched opening brackets are left over.

算法的设计思想: 1 )凡出现左括弧,则进栈; 2 )凡出现右括弧,首先检查栈是否空 若栈空,则表明该 “ 右括弧 ” 多余 否则和栈顶元素比较, 若相匹配,则 “ 左括弧出栈 ” 否则表明不匹配 3 )表达式检验结束时, 若栈空,则表明表达式中匹配正确 否则表明 “ 左括弧 ” 有余

Bracket Matching Program

 DEFINITION A type is a set, and the elements of the set are called the values of the type.  DEFINITION A sequence of length 0 is empty. A sequence of length n>=1of elements from a set T is an ordered pair ( S n− 1, t) where S n− 1 is a sequence of length n−1 of elements from T, and t is an element of T.  We shall always draw a careful distinction between the word sequential, meaning that the elements form a sequence, and the word contiguous, which we take to mean that the elements have adjacent addresses in memory. Hence we shall be able to speak of a sequential list in a contiguous implementation. Abstract data type

 DEFINITION A stack of elements of type T is a nite sequence of elements of T, together with the following operations: 1. Create the stack, leaving it empty. 2. Test whether the stack is Empty. 3. Push a new entry onto the top of the stack, provided the stack is not full. 4. Pop the entry off the top of the stack, provided the stack is not empty. 5. Retrieve the Top entry from the stack, provided the stack is not empty. Abstract Stack

1. On the abstract level we decide how the data are related to each other and what operations are needed, but we decide nothing concerning how the data will actually be stored or how the operations will actually be done. 2. On the data structures level we specify enough detail so that we can analyze the behavior of the methods and make appropriate choices as dictated by our problem. 3. On the implementation level we decide the details of how the data structures will be represented in computer memory. 4. On the application level we settle all details required for our particular application. The first two levels are often called conceptual because at these levels we are more concerned with problem solving than with programming. The middle two levels can be called algorithmic because they concern precise methods for representing data and operating with it. The last two levels are specifically concerned with programming. Refinement of Data Specification

Homework  P56 Exercises 2.1 E1(a)(d), E2,E3, E4(b)  P2.2 Exercises 2.2 top E1(int top=>count), E2, E3(a)(b)(d), E4  P69 Programming project 2.3 P2, P4