1.栈
问题的提出:Read an integer n, which will be at most 25, then read a list of n numbers, and print the list in reverse order.
关键是要得到与输入次序颠倒的输出序列。
后进先出是这种数据组织的基本性质(LIFO)。
栈的逻辑结构:固定在一端输入与输出数据元素的表(list)。
表(list)与数组(array)的区别:
- 表是一种动态结构(dynamic data structure)
- 数组是一种静态结构(static data structure)
因此,我们这个小练习的要求是:先读取一个整数n,然后再读取n个数,最后按颠倒的次序把这n个数写出来。实际的做法是在读取n个数时按读取次序压入一个栈,读取完毕后,再把数据从栈中弹出。
利用C++的标准库STL,完全可予以实现。
Reverse the numbers
# include < stack>
int 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 */
{
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;
}
2、栈的抽象数据类型
ADT Stack is
Data
数据项的列表,并含有栈顶的位置信息。
Operations
Constructor // 初始化栈顶
Empty // 检查堆栈为空,空返回True,否则False
Pop // 对非空堆栈,返回栈顶元素
Push // 将数据项压入栈顶
Top // 对非空堆栈,检索栈顶元素的值
ClearStack // 删除堆栈中所有的数据项并重置栈顶
End ADT Stack
注: 栈满的检测运算要视具体的存储方式而定。
3、类栈的的定义与实现
- 类Stack的定义
class Stack
{
private:
Stack_entry entry[maxstack]; // 顺序存储的栈
int count; // 用于栈顶指针
public:
Stack( ); // 栈的初始化
Error_code push(const Stack_entry &item);// 入栈运算
Error_code pop( ); // 出栈运算
Error_code top(Stack_entry &item) const; // 访问栈顶元素
bool empty( ) const; // 检测栈为空
ClearStack( ); // 将栈清空
bool Full( ) const; // 检测栈为满
}
栈的实现(Implementation of Stack)
本节是采用邻接结构栈(contiguous Stack),即数据元素在一个数组中依次存放。
Constructors(初始化栈)
Stack∷Stack( )
/*Pre: None.
Post: The stack is initialized to be empty. */
{
count = 0;
}
事实上栈顶指针指向实际栈顶元素后一个位置。
入栈操作
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 outcome = success;
if ( count> = maxstack)
outcome = overflow;
else
entry[count+ +] = item;
return outcome;
}
出栈操作
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 */
{
Error_code outcome = success;
if (count = = 0)
outcome = underflow;
else
- - count;
return outcome;
}
检索栈顶元素
Error_code Stack∷ top(Stack_entry &item) const
/* Pre: None.
Post: If the Stack is not empty, the top of the Stack is returned in item. If the Stack is empty, an Error_code of underflow is returned.*/
{
Error_code outcome = success;
if (count = = 0)
outcome = underflow;
else
item = entry[count - 1];
return outcome;
}
栈空函数
bool Stack∷empty( ) const
/* Pre: None.
Post: If the Stack is empty, true is returned. Otherwise false is returned. */
{
bool outcome = true;
if (count > 0) outcome = false;
ruturn outcome;
}
栈满函数
bool Stack∷full( ) const
/* Pre: None.
Post: If the Stack is full, true is returned. Otherwise false is returned. */
{
bool outcome = true;
if (count < maxstack) outcome = false
return outcome;
}
4、栈的应用之一:括号的匹配
括号的匹配问题(Bracket Matching)
例:有输入串 {a = (1+v(b[3+c[4]])) { ( ( [ [ ] ] ) )
{a = ( b[0) + 1];} { ( [ ) ] }
{ ( ) [ ( ) ] }
分析:
- 1、开括号等待闭括号来进行匹配,而且后输入的先匹配;
- 2、一旦读取闭括号,立即要与一输入的开括号匹配比较;
- 3、如果等待匹配的开括号与刚读取的闭括号不配对;或者读取闭括号后无开括号可与其比较配对;或者输入结束,但仍有等待匹配的开括号;均说明有误,即匹配不成功。
- 4、如果输入结束,同时又无等待匹配的开括号,则匹配成功。
算法:
Step1 输入序列未结束,读取一个括号symbol,并转Step2;若输入序列为空,括号栈也为空,匹配成功,并结束,否则匹配失败,结束。
Step2 若symbol是开括号,压入括号栈;否则转Step3;
Step3 若symbol是闭括号,但括号栈为空,匹配失败,结束。否则转Step4;
Step4 括号栈出栈,与symbol比较,如果配对,则匹配成功,转Step1,否则匹配失败,结束。
流程:
持续更新中。。。
数据结构C++语言描述专题系列 (一) 绪论
数据结构C++语言描述专题系列 (二) 栈
数据结构C++语言描述专题系列 (三) 队列
数据结构C++语言描述专题系列 (四) 链式栈和队列
数据结构C++语言描述专题系列 (五) 递归
数据结构C++语言描述专题系列 (六) 表与串
数据结构C++语言描述专题系列 (七) 查找
数据结构C++语言描述专题系列 (八) 排序
数据结构C++语言描述专题系列 (九) 表与信息检索
数据结构C++语言描述专题系列 (十) 二叉树
数据结构C++语言描述专题系列 (十一) 多路数
数据结构C++语言描述专题系列 (十二) 集合及其表示
数据结构C++语言描述专题系列 (十三) 图
数据结构C++语言描述专题系列 (十四) 波兰表达式