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");
    }//------------------------------------
  • 相关阅读:
    Cable master--hdu1551(二分法)
    Pie--hdu1969(二分法)
    Ice_cream's world I--hdu2120
    How Many Tables--hdu1213(并查集)
    畅通工程--hdu1232(并查集)
    小希的迷宫--hdu1272(并查集)
    More is better--hdu1856(并查集)
    Windows Message Queue--hdu1509
    期末考试--nyoj-757
    网络开发之使用Web Service和使用WCF服务
  • 原文地址:https://www.cnblogs.com/herizai/p/3096747.html
Copyright © 2011-2022 走看看