-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRingBuffer.java
More file actions
59 lines (52 loc) · 1.4 KB
/
RingBuffer.java
File metadata and controls
59 lines (52 loc) · 1.4 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
/*************************************************************************
* @mseskar
* 5/1/18
*
* Your first task is to create a data type to model the ring buffer.
*
*************************************************************************/
public class RingBuffer{
private double[] rb;
private int size = 0;
private int first = 0;
private int last = 0;
private int capacity=0;
public RingBuffer(int capacity) // create an empty ring buffer, with given max capacity
{
rb = new double[capacity];
capacity=this.capacity;
}
public int size() // return number of items currently in the buffer
{
return size;
}
public boolean isEmpty() // is the buffer empty (size equals zero)?
{
return size==0;
}
public boolean isFull() // is the buffer full (size equals capacity)?
{
return size==capacity;
}
public void enqueue(double x) // add item x to the end
{
if (size<rb.length)
{
size++;
}
rb[last] = x;
last = (last+1) % rb.length;
}
public double dequeue() // delete and return item from the front
{
double item = rb[first];
rb[first] = 0;
size--;
first = (first+1) % rb.length;
return item;
}
public double peek() // return (but do not delete) item from the front
{
return rb[first];
}
}