example28.cpp

来自「Advanced Internet Programming Lecture 2 」· C++ 代码 · 共 69 行

CPP
69
字号
#include <iostream>using namespace std;const int SIZE = 10;// Create a generic stack classtemplate <class StackType> class stack {  StackType stck[SIZE]; // holds the stack  int tos; // index of top-of-stackpublic:  stack() { tos = 0; } // initialize stack  void push(StackType ob); // push object on stack  StackType pop(); // pop object from stack};// Push an object.template <class StackType> void stack<StackType>::push(StackType ob){  if(tos==SIZE) {    cout << "Stack is full.\n";    return;  }  stck[tos] = ob;  tos++;}// Pop an object.template <class StackType> StackType stack<StackType>::pop(){  if(tos==0) {    cout << "Stack is empty.\n";    return 0; // return null on empty stack  }  tos--;  return stck[tos];}int main(){  // Demonstrate character stacks.  stack<char> s1, s2;  // create two character stacks  int i;  s1.push('a');  s2.push('x');  s1.push('b');  s2.push('y');  s1.push('c');  s2.push('z');  for(i=0; i<3; i++) cout << "Pop s1: " << s1.pop() << "\n";  for(i=0; i<3; i++) cout << "Pop s2: " << s2.pop() << "\n";  // demonstrate double stacks  stack<double> ds1, ds2;  // create two double stacks  ds1.push(1.1);  ds2.push(2.2);  ds1.push(3.3);  ds2.push(4.4);  ds1.push(5.5);  ds2.push(6.6);  for(i=0; i<3; i++) cout << "Pop ds1: " << ds1.pop() << "\n";  for(i=0; i<3; i++) cout << "Pop ds2: " << ds2.pop() << "\n";  return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?