forked from lahiruroot/Java-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.java
71 lines (59 loc) · 1.34 KB
/
Queue.java
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
package Queue;
import Linked_List.LinkedList;
public class Queue {
private Node first;
private Node last;
private int length;
class Node{
int value;
Node next;
Node(int value){
this.value=value;
}
}
public Queue(int value){
Node newNode = new Node(value);
first=newNode;
last=newNode;
length=1;
}
public void enqueue(int value){
Node newNode = new Node(value);
if (length==0){
first=newNode;
last=newNode;
}else{
last.next=newNode;
last=newNode;
}
length++;
}
public Node dequeue(){
if (length==0) return null;
Node temp = first;
first=first.next;
temp.next=null;
length--;
if (length==0){
first=null;
last=null;
}
return temp;
}
public void printQueue() {
Node temp = first;
while (temp != null) {
System.out.println(temp.value);
temp = temp.next;
}
}
public void getFirst() {
System.out.println("First: " + first.value);
}
public void getLast() {
System.out.println("Last: " + last.value);
}
public void getLength() {
System.out.println("Length: " + length);
}
}