-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
55 lines (49 loc) · 1 KB
/
Queue.java
File metadata and controls
55 lines (49 loc) · 1 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
/**
* Implement the Queue ADT using linked lists
*
* @mseskar
10/13/17
*/
public class Queue {
private Node[] a;
private static int count;
//default
public Queue() {
this.a = new Node[0];
}
//overwrite with specific Node
public Queue(Node[] arr) {
this.a = arr;
}
//enqueue the String s, add to the end of the queue
public void enqueue(String s) {
Node[] copy = new Node[a.length + 1];
for (int i = 0; i < a.length; i++) {
copy[i] = a[i];
}
Node n = new Node(s, null);
copy[copy.length - 1] = n;
if (a.length != 0)
copy[copy.length - 2].setNext(n);
a = copy;
}
//remove first element from queue, return first element from linked list
public String dequeue() {
String s = a[0].getItem();
Node[] copy = new Node[a.length - 1];
for (int i = 1; i < a.length; i++) {
copy[i - 1] = a[i];
}
a = copy;
return s;
}
//size of queue
public int size() {
return a.length;
}
//returns true if the queue is empty
public boolean isEmpty()
{
return a==null || a.length==0;
}
}