-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifo.cc
More file actions
71 lines (45 loc) · 1.64 KB
/
fifo.cc
File metadata and controls
71 lines (45 loc) · 1.64 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
// Time-stamp: <2016-01-29 16:11:08 daniel>
//
// fifo.cc: Source for bit9::fifo object
//
// required header files
//-----------------------------------------------------------------------------
#include <iostream> // std::cerr
#include <stdexcept> // std::runtime_error
#include "fifo.hh" // bit9::fifo
#include "node.hh" // bit9::node
// NS short hand
//-----------------------------------------------------------------------------
using namespace std; // standard library
using namespace bit9; // exercise NS
// Constructor
//-----------------------------------------------------------------------------
fifo::fifo() : head( nullptr ), tail( nullptr ) {
}; // end constructor
// Destructor
//-----------------------------------------------------------------------------
fifo::~fifo( ) {
// FIFO should be empty at this point but if not a loop should be
// added to clear the object
}; // end destructor
// Add a value to the fifo
// value: int
//-----------------------------------------------------------------------------
void fifo::queue( const int value ) {
node* local = new node( value );
if ( head == nullptr ) head = local; // fifo was empty
if ( tail != nullptr ) tail->pointer = local;
tail = local;
cerr << "adding " << value << " to the fifo" << endl;
}; // end queue
// Remove a value from the fifo
//-----------------------------------------------------------------------------
int fifo::dequeue( ) {
int result = 0x0;
node* local = head;
if ( local == nullptr ) throw runtime_error( "FIFO is empty!" );
result = local->value;
head = local->pointer;
delete local;
return result;
}; // end dequeue