-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemiConstantTimeQueue.java
More file actions
172 lines (146 loc) · 4.15 KB
/
Copy pathSemiConstantTimeQueue.java
File metadata and controls
172 lines (146 loc) · 4.15 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package edu.cofc.cs.csci230;
import java.util.NoSuchElementException;
/**
* A semi-constant time FIFO queue. The time complexity for
* the interface methods are:
* ----------------------------------
* 1) add: O(1) - not considering capacity resize operations
* 2) remove: O(n) - not considering capacity resize operations
* 3) peek: O(1)
*
* Please note: the above time complexities do not factor in
* capacity resize (grow and shrink) operations. Even though
* capacity resize operations will occur, for this assignment
* you may assume the are negligible.
*
* This data structure was discussed in class along with the
* operations, please review your notes.
*
* @author CSCI 230: Data Structures and Algorithms Fall 2016
*
* @param <AnyType>
*/
public class SemiConstantTimeQueue<AnyType extends Comparable> implements Queue<AnyType> {
/**
* private instance variables
*/
private ArrayList<AnyType> list = new ArrayList<AnyType>();
/**
* Inserts the specified element at the end of the queue in
* constant time O(1)
*
* @param t element to add
* @throws NullPointerException- if the specified element is null
* (queue does not permit null elements)
*/
public void add(AnyType t) throws NullPointerException {
if (t == null){
throw new NullPointerException();
}
else{
list.add(t);
}
/**
* -------------------------------------------
* TODO: You fully implement this method
*
* Note: Your add solution must be a constant
* time O(1) operation (not considering capacity
* resize operations)
*
*/
} // end add() method
/**
* Retrieves and removes the head of the queue in
* linear time O(n).
*
* Hint: shift operations will make this a linear time
* operation.
*
* @return the head of the queue
* @throws NoSuchElementException - if this queue is empty
*/
public AnyType remove() throws NoSuchElementException {
if(!list.isEmpty()){
return list.remove(0);
}
else{
throw new NoSuchElementException();
}
/**
* -------------------------------------------
* TODO: You fully implement this method
*
* Note: Your push solution must be a linear
* time O(n) operation. See hint above.
*
*
*/
} // end remove() method
/**
* Retrieves, but does not remove, the head of the queue,
* or returns null if this queue is empty.
*
* @return the head of this queue, or null if this queue is empty
*/
public AnyType peek() {
if(list.get(0)==null){
return null;
}
else{
return list.get(0);
}
/**
* -------------------------------------------
* TODO: You fully implement this method
*
* Note: Your add solution must be a constant
* time O(1) operation
*
*/
} // end peek() method
/**
*
* @param args
*/
public static void main(String[] args) {
SemiConstantTimeQueue<Integer> Queue = new SemiConstantTimeQueue<Integer>();
////////////peek on empty list
System.out.println(Queue.peek());
////////////remove on empty list
try{
Queue.remove();
}catch(NoSuchElementException ex){
System.out.println(ex);
}
///////////add a null value
try{
Queue.add(null);
}catch(NullPointerException ex){
System.out.println(ex);
}
/////////////add 25 elements
System.out.println("add() : 0 - 25");
for(int i = 0; i <= 25; i++){
Queue.add(new Integer(i));
}
System.out.println("about to remove elements");
//////////////////////////////////remove all 25 and peek at next element
for(int i = 0; i <= 25; i++){
Integer numb = Queue.remove();
System.out.println("Just removed : " + numb.toString());
System.out.println("Peeked : " + Queue.peek());
}
///////////remove one that is not there
try{
Queue.remove();
}catch(NoSuchElementException ex){
System.out.println(ex);
}
/**
* -------------------------------------------
* TODO: You put your test cases here
*
*/
} // end main() method
} // end ConstantTimeQueue class definition