Repository navigation
Expand file tree
/
Copy pathAbstractQueue.java
More file actions
95 lines (74 loc) · 2.12 KB
/
Copy pathAbstractQueue.java
File metadata and controls
95 lines (74 loc) · 2.12 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
package aima.util;
import java.util.LinkedList;
import java.util.List;
/**
* @author Ravi Mohan
*
*/
/**
* Artificial Intelligence A Modern Approach (2nd Edition): page 71.
*
* The operations on a queue are as follows: MAKE-QUEUE(element,...) creates a
* queue with the given element(s). EMPTY?(queue) returns true only if there are
* no more elements in the queue. FIRST(queue) returns the first element of the
* queue. REMOVE-FIRST(queue) returns FIRST(queue) and removes it from the
* queue. INSERT(element, queue) inserts an element into the queue and returns
* the resulting queue. (We will see that different types of queues insert
* elements in different orders.) INSERT-ALL(elements, queue) inserts a set of
* elements into the queue and returns the resulting queue.
*/
public class AbstractQueue implements Queue {
protected LinkedList<Object> linkedList;
public AbstractQueue() {
linkedList = new LinkedList<Object>();
}
public void addToFront(Object n) {
linkedList.addFirst(n);
}
public void addToBack(Object n) {
linkedList.addLast(n);
}
public void addToFront(List list) {
for (int i = 0; i < list.size(); i++) {
addToFront(list.get(list.size() - 1 - i));
}
}
public void addToBack(List list) {
for (int i = 0; i < list.size(); i++) {
addToBack(list.get(i));
}
}
public Object removeFirst() {
return (linkedList.removeFirst());
}
public Object removeLast() {
return (linkedList.removeLast());
}
public Object getFirst() {
return (linkedList.getFirst());
}
public Object getLast() {
return (linkedList.getLast());
}
public boolean isEmpty() {
return linkedList.isEmpty();
}
public int size() {
return linkedList.size();
}
public List asList() {
return linkedList;
}
public void add(Object anItem) {
throw new RuntimeException("must be implemented by subclasses");
}
public void add(List items) {
throw new RuntimeException("must be implemented by subclasses");
}
public Object remove() {
throw new RuntimeException("must be implemented by subclasses");
}
public Object get() {
throw new RuntimeException("must be implemented by subclasses");
}
}