zoukankan      html  css  js  c++  java
  • 18次试验有关栈的程序

    //-------------------------------------
    // Stack.h
    //-------------------------------------
    const int SIZE = 10;//size初始化为10
    //-------------------------------------
    class Stack{
      int stck[SIZE];      //holds the stack
      int tos;              //index of top-of-stack
    public:
      Stack():tos(0){}//tos即栈的顶部初始化为0
      int top();
      void push(int ch);   //push integer to stack
      int pop();           //pop integer from stack.
    };//-----------------------------------
     //-------------------------------------
    // Stack.cpp
    //-------------------------------------
    #include"Stack.h"
    #include<iostream>
    //-------------------------------------
    int Stack::top()//top(),push(),pop()
    {
    return stck[tos-1];//无需判断 }//------------------------------------ void Stack::push(int ch){ if(tos==SIZE)//只判断是否已满,进行判断
    { std::cout
    <<"\nStack is full\n"; return; } stck[tos++]=ch;//stck[tos++]=ch; }//------------------------------------ int Stack::pop()
    {
    if(tos==0){ std::cout<<"\nStack is empty\n"; return 0; //return null on empty stack. } return stck[--tos]; //top of stack,如果最高一层为0,输出empty,如果不为0,返回他的下一个,使下一个数值为顶部 }//------------------------------------
    //-------------------------------------
    // EX0805.cpp
    // 栈应用
    //-------------------------------------
    #include"Stack.h"
    #include<iostream>
    //-------------------------------------
    int main(){
      Stack s;
      s.push(10);
      s.push(12);
      s.push(14);
      std::cout<<s.top()<<"\n";
      s.pop();
      std::cout<<s.top()<<"\n";
      system("pause");
    }//------------------------------------
  • 相关阅读:
    Material Design系列第八篇——Creating Lists and Cards
    Androidの解决自动旋转导致activity重启问题
    cnBlogs博客推荐
    yii中sphinx,Ajax搜索分页
    yii框架中应用jquery表单验证插件
    百度一键分享
    yii框架中邮箱激活(数字签名)
    yii框架中保存第三方登录信息
    yii添加行的增删改查
    yii遍历行下的每列数据(小1月考)
  • 原文地址:https://www.cnblogs.com/herizai/p/3096747.html
Copyright © 2011-2022 走看看