-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoundedQueue.java
More file actions
150 lines (142 loc) · 3.89 KB
/
Copy pathBoundedQueue.java
File metadata and controls
150 lines (142 loc) · 3.89 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Fixed-capacity collection that supports adding to one end and
* removing from the other.
*/
public class BoundedQueue<T> {
int capacity;
boolean dropOldest;
Item head;
static int boundedQueues;
static int droppedItems;
/**
* Constructs an empty BoundedQueue with the given capacity.
*
* The parameter dropOldest specifies what will happen when put()
* is called and the queue is full. If dropOldest is true, the
* oldest item (which would be returned by get()) is dropped. If
* dropOldest is false, the new item is dropped.
*/
public BoundedQueue(int capacity, boolean dropOldest) {
this.capacity = capacity;
this.dropOldest = dropOldest;
boundedQueues++;
}
/**
* Returns the number of items in the queue.
*/
public int count() {
return head.count();
}
/**
* Returns true if the queue does not contain any items.
*/
public boolean empty() {
if(head == null){ return true; }
else { return false; }
}
/**
* Returns true if the queue has reached its capacity.
*/
public boolean full() {
if(head == null){
if(capacity == 0){ return true;}
else if(capacity != 0){ return false; }
}
if(head.count() >= capacity){
return true;
}
else{ return false; }
}
/**
* Adds the item to the queue.
*
* If the queue is full, an item is dropped as described in the
* constructor.
*/
public void put(T item) {
if(full() && dropOldest){
add(item);
get();
return;
}
if(full() && !dropOldest){
//system prntln ddnt add coz full
return;
}
if(!full()){
add(item);
return;
}
}
/**
* Adds an item to the linked list
*/
public void add(T item){
Item temp = new Item(item);
temp.setNext(head);
head = temp;
}
/**
* Retrieves and removes the oldest item in the queue.
*
* If the queue is empty, returns null.
*/
public T get() {
if(head == null) { return null; }
if(head.getNext() == null) {
Item temp = head;
head = null;
droppedItems++;
return temp.getItem();
}
Item temp;
temp = head;
while(temp.getNext().getNext() != null){
temp = temp.getNext();
}
Item temp2 = temp.getNext();
temp.setNext(null);
droppedItems++;
return temp2.getItem();
}
public static void reportStats(){
System.out.println("Total Bound Queues created: " + boundedQueues);
System.out.println("Total Items dropped: " + droppedItems);
}
/**
* Helper for creating an array with T as the element type. The
* elements are initialized to null.
private T[] makeArray(int size) {
// We would normally use "new T[size]", but we can't because T
// is a type parameter and Java does not support generic array
// creation. The following is an imperfect workaround.
@SuppressWarnings("unchecked")
T[] array = (T[]) new Object[size];
return array;
}
*/
/**
* Implements a item in a linked list.
* Contains the item T and the next item in the list (or null if it is the tail)
*/
public class Item{
T item;
Item next = null;
public Item(T o)
{
item = o;
}
public Item getNext(){ return next; }
public void setNext(Item x){ next = x; }
public int count() {
if (getNext() != null) {
return (1 + getNext().count());
} else {
return 1;
}
}
public T getItem() {
return item;
}
}
}