#ifndef stack_hpp
#define stack_hpp
#include <stdio.h>
#include <cassert>
class stack {
int n; // Capacity of stack
int s; // Size of stack
int *a; // Dynamic Array of stack
public:
stack(int);
~stack();
void push(int);
int pop(void);
int top(void);
int capacity() const { return n; }
int size() const { return s; }
/*
bool full() const {
if (s==n) return true;
else return false;
}
*/
bool full() const { return s==n; }
bool empty() const { return s==0; }
void print();
};
#endif /* stack_hpp */
#include "stack.hpp"
#include<iostream>
using namespace std;
stack::stack(int c){
n = c;
s = 0;
a = new int[n];
}
stack::~stack(){
delete [] a;
}
void stack::push(int x){
assert(!full());
a[s++] = x;
}
int stack::pop(){
assert(!empty());
return a[--s];
}
int stack::top(){
assert(!empty());
return a[s-1];
}
void stack::print(){
cout << "[ ";
for(int i = 0; i < s; i++) cout << a[i] << " ";
cout << "]\n";
}
#include <iostream>
#include "stack.hpp"
using namespace std;
int main(){
srand((unsigned) time(nullptr));
stack s(10);
while (!s.full()){
int x = rand() % 101;
cout << x << ": ";
s.push(x);
s.print();
}
cout << "La pila está llena\n\n";
while (!s.empty()) {
int x = s.pop();
cout<< x << ": ";
s.print();
}
cout << "La pila está vacia\n\n";
for (int i = 0; i < 20; i++){
// inserción
if (rand () % 2 == 1){
if (s.full()) cout << "Pila llena\n";
else {
int x = rand() % 101;
cout << "ins " << x << ": ";
s.push(x);
s.print();
}
//extraccion
} else {
if (s.empty()) cout << "Pila vacia\n";
else {
int x = s.pop();
cout << "out " << x << ": ";
s.print();
}
}
}
return 0;
}
Top comments (0)