DEV Community

Leo
Leo

Posted on

dynamic stack

#ifndef stack_hpp
#define stack_hpp

#include <iostream>
#include <assert.h>


class stack {

  class node {

  int _data;
  node *_next;

  public:

    node(int);

  int data() const { return _data; } //cual es el dato
  node *next() const { return _next; } // cual es el siguiente apuntador
  void next(node *p) { _next = p; } // cambia el siguiente ap
  };

  int n; // Capacity of stack
  int s; // Size of stack

  node *init; // Inicio de nodo

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 */
Enter fullscreen mode Exit fullscreen mode
#include "stack.hpp"

  stack::node::node(int x) {
    _data = x;
    _next = nullptr;
  }

  stack::stack(int m){

    n = m;
    s = 0;

    init = nullptr;
  }

  stack::~stack(){

    while(init != nullptr) {
      node *p = init;
      init = init->next();
      delete p;
    }

  }


  void stack::push(int x){

    assert(!full());

    node *p = new node(x);

    p -> next(init);
    init = p;

    s++;
  }

  int stack::pop(){

    assert(!empty());

    int x = init -> data();

    node *p = init;

    init = p -> next();//init = init -> next();

    delete p;

    s--;

    return x;
  }

  int stack::top(){
  assert(!empty());
    return init -> data();
  }

  void stack::print(){

    node *p = init;
  std::cout << "[";
    while(p != nullptr){
      std::cout << p -> data() << " ";
      p = p -> next();
    }
  std::cout << "]" << std::endl;
  }
Enter fullscreen mode Exit fullscreen mode
#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 < 100; 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;
}
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs