2011栈与队列.pdfVIP

  • 0
  • 0
  • 约1.41万字
  • 约 67页
  • 2017-08-23 发布于浙江
  • 举报
2011栈与队列

ACM选修课 栈与队列 •栈 stack • 队列 queue •优先队列 priority_queue 问题 • 表达式的计算 • 问题的简化: 加法器:2+2345+913+3 • 加法器的扩展 加法、乘法器 • 问题的解决思路 (1)直接解决 (2)问题求解的变通 数字符号串整数 数字符号串存于Exp字符数组中 Int factor=0; do { // 合并出运算对象的值 factor=factor*10+Exp[i]-48; i=i+1; } while ( isdigit( Exp[i] ) ); 问题 • 数制的转换 问题:括号匹配的检验 假设表达式中充许括号嵌套,则检验括号是 否匹配的方法可用 “期待的急迫程度”这个 概念来描述。例: { ( [ ……]…) …{ ( ……) ……} } 问题 • 森林探险的模拟 新型结构的引入 栈(stack) 定义: 只允许在一端插入和删除的线性表 定义: 只允许在一端插入和删除的线性表 栈顶( 退栈 进栈 top): 栈顶( top): 允许插入和删除的一端 允许插入和删除的一端 特点 特点 后进先出 (LIFO) 后进先出 (LIFO) 使用方法 1.参考网站: /en-us/library/h9sh48d5(VS.80).aspx 2.引用头文件 #include stack #include iostream using namespace std; stack class 成员函数: empty Tests if the stack is empty. pop Removes the element from the top of the stack. push Adds an element to the top of the stack. size Returns the number of elements in the stack. top Returns a reference to an element at the top of the stack. 容器适配器:stack • stack 上可以进行以下操作: – push: 插入元素 – pop: 弹出元素 – top: 返回栈顶元素的引用 使用例子1 #include stack s1.pop( ); #include iostream using namespace std; i = s1.size( ); cout After a pop, the stack length is int main( ) i . endl; { stack int s1, s2;

文档评论(0)

1亿VIP精品文档

相关文档