DEV Community

Leo
Leo

Posted on • Updated on

En la fila del cine

Image description

Image description

Image description

Here we have the code in C++ to resolve problem.

main.cpp

/*
AUTOR:    Leobardo Vázquez 
CURSO:    Estructura de Datos
PROGRAMA: En la fila del cine
FECHA:    22 de Marzo del 2024
*/
#include <iostream>
#include "cine.hpp"

int main(){

  fila fila(20);
  int x;
  std::cin >> x;

  while(x >= 0){
    if(x == 0){
        while(fila.size() > 0){
          std::cout<< fila.pop() << " ";
         fila.print();
        }
    }else if(x > 0){
    fila.push(x);
    fila.print();
    }
    std::cin >> x;
  }
    return 0;
}
//https://dev.to/imnotleo/en-la-fila-del-cine-47k8
Enter fullscreen mode Exit fullscreen mode

cine.cpp

#include "cine.hpp"

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

  fila::fila(int c){

      n = c;
      s = 0;
      head = nullptr;
      tail = nullptr;
  }

  fila::~fila(){

      while(head != nullptr) {

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

  void fila::push(int x){

      assert(!full());
    node *p = new node(x);
    if(empty()) {
        head = p;
        tail = p;
      }else{
        tail -> next(p);
        tail = p;
    }
    s++;
  }

  int fila::pop(){

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

  void fila::print(){

      node *p = head;
      std::cout<<"[ ";
      while(p != nullptr){
      std::cout << p -> data() << " ";
      p = p -> next();
      }
  std::cout << "]" << std::endl;
}
//https://dev.to/imnotleo/en-la-fila-del-cine-47k8
Enter fullscreen mode Exit fullscreen mode

cine.hpp

#ifndef cine_hpp
#define cine_hpp

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

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
  void next(node *p) { _next = p; } // cambia el siguiente ap
  };

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

  node *head; // Primer elemento de la fila
  node *tail; // Ultimo elemento de la fila

public:

  fila(int);
  ~fila();

  void push(int);
  int pop(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 */
//https://dev.to/imnotleo/en-la-fila-del-cine-47k8
Enter fullscreen mode Exit fullscreen mode

Top comments (0)