Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdexcept>
- #include <fstream>
- using namespace std;
- class Stack{
- private:
- static const int MIN_CAPACITY=10;
- int *data; //declaracion
- int stack_top=-1;
- int capacity;
- public:
- Stack();
- Stack(int MaxCapacity);
- ~Stack();
- virtual int size ();
- virtual bool isEmpty();
- virtual int top();
- virtual void push(int value);
- virtual int pop();
- virtual void print();
- };
- //Definiciones Stack
- Stack::Stack():Stack(MIN_CAPACITY){}
- Stack::Stack(int MaxCapacity) {data=new int [MaxCapacity] ;capacity=MaxCapacity;}
- Stack::~Stack(){ delete[] data; }
- bool Stack::isEmpty(){ return (stack_top==-1);}//Retorna 1 si Stack empty 0 en otros casos
- int Stack::size (){return stack_top+1;};
- void Stack::print(){
- for (int i=stack_top;i>-1;i--){
- cout <<"stack["<<i<<"] = "<<data[i]<<endl;
- };
- }
- void Stack::push(int value){
- //if (size()==data.size()){throw std::exception ("Stack Overflow Exception");}
- stack_top++;
- data[stack_top]=value;
- }
- int Stack::pop(){
- //if (isEmpty()){throw std::exception ("Stack Empty Exception");}
- int topVal=data[stack_top];
- stack_top--;
- return topVal;
- }
- int Stack::top(){
- //if (isEmpty()) {throw std::exception ("StackEmptyException");}
- return data[stack_top];
- }
- class Pila :public Stack{
- public:
- Pila();
- Pila(int MaxCapacity);
- ~Pila();
- private:
- static const int MIN_CAPACITY=20;
- int *data; //declaracion
- int capacity;
- };
- Pila::Pila():Pila(MIN_CAPACITY){}
- Pila::Pila(int MaxCapacity) {data=new int [MaxCapacity] ;capacity=MaxCapacity;}
- Pila::~Pila(){ delete[] data; }
- int main() {
- Stack s(6);
- for (int i=1;i<=6;i++){s.push(i);}
- cout <<endl<<"Size="<<s.size()<<endl;
- cout <<endl<<"Top Stack="<<s.top()<<endl;
- s.print();
- for (int i=1;i<=6;i++){cout<<s.pop()<<endl;}
- Pila p(5);
- for (int i=1;i<=5;i++){p.push(i);}
- cout <<endl<<"Size="<<p.size()<<endl;
- cout <<endl<<"Top Stack="<<p.top()<<endl;
- p.print();
- for (int i=1;i<=5;i++){cout<<p.pop()<<endl;}
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement