欣乐

The eagles are coming!

导航

第18章 stack堆栈容器

 

 

 

 

 

/*

  第18章 stack堆栈容器
   18.1 stack技术原理
   18.2 stack应用基础
   18.3 本章小结
*/


//  第18章 stack堆栈容器
//   18.1 stack技术原理 ------------

//   18.2 stack应用基础 ---------------------------------------------------------------------------------


//268
#include <stack>
#include <iostream>
int main(void)
{
  using namespace std;
  //创建堆栈对象
  stack < int > s;
  //元素入栈
  s.push(3);
  s.push(19);
  s.push(23);
  s.push(36);
  s.push(50);
  //元素依次出栈
  while(!s.empty())
  {
    cout << s.top() << endl; //打印栈顶元素
    s.pop(); //出栈
  }
  return 0;
}


//268-269
#include <stack>
#include <list>
#include <iostream>
#define STACK_SIZE 100  //堆栈最大容量
int main(void)
{
  using namespace std;
  stack < int, list < int >  > s; //用双向链表做堆栈的底层结构
  if(s.size() < STACK_SIZE)
    s.push(68);
  if(s.size() < STACK_SIZE)
    s.push(1);
  if(s.size() < STACK_SIZE)
    s.push(17);
  //
  while(!s.empty())
  {
    cout << s.top() << endl;
    s.pop();
  }
  return 0;
}


//   18.3 本章小结 --------------------------------------------------------------------------------------

 

 

 

 

 

 

 TOP

 

posted on 2014-11-20 23:22  欣乐  阅读(129)  评论(0编辑  收藏  举报