-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
64 lines (56 loc) · 1.95 KB
/
Queue.java
File metadata and controls
64 lines (56 loc) · 1.95 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
public class Queue {
private Node front;
private Node back;
//creating an inner Node class for Queue
private static class Node {
Object item; //the item stored in the node
Node next; //reference to the next node
//constructor for Node class
Node(Object item) {
this.item = item; //initialize the item
this.next = null; //next is initially null
}
}
//creating consutructor for Queue, setting the back and front to null (empty)
public Queue(){
this.front = null;
this.back = null;
}
/**
* enqueues an item to an item to the back of the queue
* @param item
*/
public void enqueue(Object item) {
Node newNode = new Node(item); //creating a new node item
if (front == null) { //if the front is null, set the front to the new node
front = newNode;
}
if (back != null) { //if the back is not null, set the back node to the new node
back.next = newNode;
}
back = newNode;
}
/**
* dequeues the first item in the queue (FIFO)
* @return
*/
public Object dequeue() {
Object toReturn = null; //the dequeued item to be returned
if (front != null) { //if the front is not null then dequeue
toReturn = front.item; //store the item that is in the front, in the toReturn variable
front = front.next; //set the front to the next node
}
if (front == null) { //if the front is null then set the back to null
back = null;
}
return toReturn;
}
public void display(){
Node current = front; //using a pointer to point to the front of the queue
while(current != null){
System.out.print(current.item + " -> ");
current = current.next;
}
System.out.println("null");
}
}