-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
65 lines (57 loc) · 976 Bytes
/
Queue.java
File metadata and controls
65 lines (57 loc) · 976 Bytes
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
// AUTHOR: Soel Micheletti
// Each element of the queue is represented as a Node object
class Node{
Node next;
Node prev;
int value;
public Node(int v) {
value = v;
}
}
// Class to manage all Node objects
public class Queue {
Node first;
Node last;
int size;
public boolean isEmpty() {
return size == 0;
}
public void enqueue(int x) {
Node n = new Node(x);
if(isEmpty()) {
first = last = n;
size = 1;
}
else {
last.next = n;
n.prev = last;
last = n;
size++;
}
}
public Node front() {
if(isEmpty())
throw new RuntimeException("Empty Stack");
else {
Node n = first;
return n;
}
}
public Node dequeue() {
if(isEmpty())
throw new RuntimeException("Empty Stack");
else if(size == 1) {
Node n = first;
first = last = null;
size = 0;
return n;
}
else {
Node n = first;
first.next.prev = null;
first = first.next;
size--;
return n;
}
}
}