-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fila.cpp
59 lines (52 loc) · 1.11 KB
/
Fila.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "Fila.h"
#include <stdexcept>
#include <iostream>
using namespace std;
Fila::Fila(int tamanho) :
tamanho(tamanho)
{
this->cabeca = 0;
this->calda = 0;
this->numeroDeElementos = 0;
vetor = new Datagrama*[tamanho];
}
Fila::~Fila()
{
delete[] vetor;
}
void Fila::enqueue(Datagrama* d)
{
if (numeroDeElementos == tamanho) throw new overflow_error ("Fila cheia: nao rola colocar datagrama");
else
{
vetor[calda] = d;
numeroDeElementos++;
calda = (calda + 1) % tamanho;
}
}
Datagrama* Fila::dequeue()
{
if (numeroDeElementos == 0)
{
throw new underflow_error ("Fila vazia: nao rola tirar datagrama");
} else
{
numeroDeElementos--;
int retirar = cabeca;
cabeca = (cabeca + 1) % tamanho;
return vetor[retirar];
}
}
bool Fila::isEmpty()
{
return !(numeroDeElementos);
}
void Fila::imprimir()
{
cout << "Ha " << numeroDeElementos << " datagramas na fila" << endl;
int j = cabeca;
for (int i = 0; i < numeroDeElementos; i++) {
vetor[j]->imprimir();
j = (j + 1) % tamanho;
}
}