DEV Community

Leo
Leo

Posted on • Updated on

My code to circular buffer (linked list)

main.cpp

#include <iostream>
#include "cbuffer.hpp"


int main(){

  srand((unsigned) time(nullptr));
  int n, t, w, Tpkt = 0;
  std::cin >> n >> t >> w;  //n: buffer size, t: wait time, w: atention time.

  cbuffer Dthru(n);

  for (int i = 0; i < 360/t; i++){

    int pkt = rand() % 5 + 1;
    if (Dthru.in((float)w/t)){
          //In
          if((Tpkt+pkt) > (n*4)){
              std::cout<< "Sobrepedido de paquetes\nEl Drive-thru colapsó"<<std::endl;
              return 0;
          }else if(Dthru.full()){
              std::cout<< "Sobrecupo en la fila\nEl Drive-thru colapsó"<<std::endl;
              return 0;
          }
          Dthru.push(pkt);
          Tpkt += pkt;
          std::cout<< "  >   "<< pkt<<(Dthru.size() < 10 ? "   " : "  ")<< Dthru.size()<<(Tpkt < 10 ? "   " : "  ")<< Tpkt<< "  ";
            Dthru.print();
    }else{
          //Out
          int pop = Dthru.pop();
          Tpkt -= pop;
          std::cout<< "  <   "<< pop<<(Dthru.size() < 10 ? "   " : "  ")<< Dthru.size()<<(Tpkt < 10 ? "   " : "  ")<< Tpkt<< "  ";
            Dthru.print();
    }
  }
    std::cout<< "Buen trabajo, hasta mañana"<<std::endl;
  return 0;
}

Enter fullscreen mode Exit fullscreen mode

cbuffer.cpp

#include "cbuffer.hpp"

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

  cbuffer::cbuffer(int c){

    n = c;
    s = 0;
    front = nullptr;
    rear = nullptr;
  }

  cbuffer::~cbuffer(){

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

  }

  void cbuffer::push(int x){

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

  int cbuffer::pop(){

    if(empty()){
    return 0;
    }else{
    int x = front -> data();
    node *p = front;
    front = p -> next();
    delete p;
    s--;
    return x;
    }
  }

  void cbuffer::print(){

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

  bool cbuffer::in(float x){
      return (rand() / (float)RAND_MAX) < x;
  }

Enter fullscreen mode Exit fullscreen mode

cbuffer.hpp

#ifndef cbuffer_hpp
#define cbuffer_hpp

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


class cbuffer {

  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 apuntador
  };

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

  node *front; // Older data in the buffer, provided s > 0
  node *rear; // First aviable box, provided s < n

public:

  cbuffer(int);
  ~cbuffer();

  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();
  bool in(float);
};

#endif /* cbuffer_hpp */
Enter fullscreen mode Exit fullscreen mode

Top comments (0)