DEV Community

Leo
Leo

Posted on • Updated on

my code

Show more on: https://dev.to/imnotleo/en-la-fila-del-cine-47k8

Puedes ver mas en: https://dev.to/imnotleo/en-la-fila-del-cine-47k8

#ifndef cine_hpp
#define cine_hpp

#include <iostream>
#include <assert.h>
using namespace std;

class fila {

    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
  node *back() const { return _back;
  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:

  fila(int);
  ~fila();

  void push(int);
  int pop(void);
  int top(void);

  int capacity() const { return n; }
  int size() const { return s; }

  bool full() const { return s==n; }
  bool empty() const { return s==0; }

  void print();
};

#endif /* cine_hpp */
Enter fullscreen mode Exit fullscreen mode
#include "cine.hpp"

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

    fila::fila(int c){

        n = c;
        s = 0;
        init = nullptr;
    }

    fila::~fila(){

        while(init != nullptr) {

            node *p = init;
            init = init -> next();
            delete p;
        }    

    }

    void fila::push(int x){

        assert(!full());
        node *p = new node(x);
        p -> next(init);
        init = p;
        s++;
    }

    int fila::pop(){

        assert(!empty());
        while(!empty()){
            int x = init -> data();
            node *p = init;
            init = p -> next(); 
            delete p;
            s--;
            return x;
        }
    }

    int fila::top(){

        assert(!empty());
        return init -> data();
    }

    void fila::print(){

        node *p = init;
        cout<<"["
        while(p != nullptr){
        cout << p -> data() << " ";
        p = p -> next();
        }
    cout << "]" << endl;
    }

Enter fullscreen mode Exit fullscreen mode
#include <iostream>
#include "cine.hpp"

int main(){

    srand((unsigned) time(nullptr));

    fila fila(20);

}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)