-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
98 lines (86 loc) · 1.66 KB
/
Queue.java
File metadata and controls
98 lines (86 loc) · 1.66 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package technical;
public class Queue {
private Listnode rear;
private Listnode front;
private int length;
Queue() {
this.rear = null;
this.front = null;
this.length = 0;
}
private class Listnode {
int data;
Listnode next;
public Listnode(int data) {
this.data = data;
this.next = null;
}
}
public int length() {
return length;
}
public boolean isEmpty() {
return length == 0;
}
public void enqeue(int data) {
Listnode newnode = new Listnode(data);
if (isEmpty()) {
front = newnode;
} else {
rear.next = newnode;
}
rear = newnode;
length++;
}
public void print() {
if (isEmpty()) {
return;
}
Listnode temp = front;
while (temp != null) {
System.out.print(temp.data + "-->");
temp = temp.next;
}
System.out.print("null");
}
public int dequeue() {
if(isEmpty()) {
throw new NoSuchElementException();
}
int result=front.data;
front=front.next;
if(front==null) {
rear=null;
}
length--;
return result;
}
String [] generatebinarynumber(int n) {
Queue<String> q=new LinkedList<>();
String [] result=new String[n];
q.offer("1");
for(int i=0;i<n;i++) {
result[i]=q.poll();
String n1=result[i]+"0";
String n2=result[i]+"1";
q.offer(n1);
q.offer(n2);
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Queue q = new Queue();
q.enqeue(12);
q.enqeue(32);
q.enqeue(55);
q.enqeue(64);
q.enqeue(22);
q.print();
System.out.println();
q.dequeue();
q.dequeue();
q.dequeue();
q.print();
}
}