forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queuell.java
99 lines (98 loc) · 2.07 KB
/
Queuell.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
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
99
//Queue implemetation Its readme file in wiki
import java.util.*;
import java.lang.*;
class myq<E> {
static class Node<E>{
E data;
Node<E> next;
Node(E a){
this.data=a;
next=null;
}
}
Node<E> front;
Node<E> rear;
int c=0;
boolean isempty(){
return front==null;
}
void Enqueue(E a){
Node<E> temp=new Node<E>(a);
if(isempty()){
front=temp;
rear=temp;
c++;
return;
}
else{
rear.next=temp;
rear=temp;
c++;
return;
}
}
E Dequeue() throws Exception{
Node<E> t;
if (isempty()){
throw new Exception("EEEEEmpty");
}
else if(c==1){
t=front;
front=null;
rear=null;
c--;
}
else{
t=front;
front=front.next;
c--;
}
return t.data;
}
E Front(){
if (isempty()){
return null;
}
else{
return front.data;
}
}
E Rear(){
if (isempty()){
return null;
}
else{
return rear.data;
}
}
}
public class Queuell {
public static void main(String[] args) throws Exception{
myq<Integer> qe = new myq<Integer>();
qe.Enqueue(10);
qe.Enqueue(20);
qe.Enqueue(30);
qe.Enqueue(40);
System.out.println(qe.Front());
System.out.println(qe.Rear());
System.out.println(qe.Dequeue());
System.out.println(qe.Dequeue());
System.out.println(qe.Front());
System.out.println(qe.Dequeue());
System.out.println(qe.Dequeue());
System.out.println(qe.Dequeue());
}
}
/*
output:
10
40
10
20
30
30
40
Exception in thread "main" java.lang.Exception: EEEEEmpty
at myq.remove(Queuell.java:37)
at Queuell.main(Queuell.java:83)
*/