-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
81 lines (68 loc) · 1.7 KB
/
Copy pathQueue.java
File metadata and controls
81 lines (68 loc) · 1.7 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
package study.datastructure;
public class Queue {
public static void main(String[] args) {
Queue myQ = new Queue(7);
myQ.enqueue(5);
myQ.printQ();
myQ.getFirst();
System.out.println(myQ.dequeue().value);
System.out.println(myQ.dequeue().value);
System.out.println(myQ.dequeue().value);
}
Node first;
Node last;
int length;
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;
if (length == 1) {
first = null;
last = null;
}else {
first = first.next;
temp.next = null;
}
length --;
return temp;
}
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);
}
public void printQ() {
Node temp = first;
for (int i = 0; i < length; i++) {
System.out.println("value : " + first.value);
temp = first.next;
}
}
static class Node {
int value;
Node next;
public Node(int value) {
this.value = value;
}
}
}