-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
executable file
·88 lines (60 loc) · 1.44 KB
/
Queue.java
File metadata and controls
executable file
·88 lines (60 loc) · 1.44 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
import java.util.*;
class Queue{
static int MAX_SIZE=40;
int[] elements;
int headIndex=-1;
int tailIndex=-1;
public boolean isEmpty(){
return this.headIndex==-1;
}
public boolean isFull(){
int nextIndex=-8;
try{
nextIndex = (this.tailIndex+1) % this.elements.length;
}catch(NullPointerException nullPonter){
System.out.println(nullPonter+" yes");
}
return nextIndex==this.headIndex;
}
public void enqueue(int data){
if(this.isFull()){
System.out.println("its full");
}
tailIndex=(tailIndex+1)%elements.length;
elements[tailIndex] = data;
if(headIndex==-1)
{
headIndex=tailIndex;
}
}
public int dequeue(){
if(this.isEmpty())
{
System.out.println("Its empty");
}
int data = elements[headIndex];
headIndex=(headIndex+1)%elements.length;
return data;
}
public static void main(String[] arg)
{
int ch,d,n;
Queue q = new Queue();
System.out.println("Please enter the choice \n 1: insert \n 2:remove");
Scanner s=new Scanner(System.in);
ch=s.nextInt();
switch(ch){
case 1:
System.out.println("Please enter the size");
n=s.nextInt();
for(int i=0;i<n;i++)
{
q.enqueue(s.nextInt());
}
break;
case 2:System.out.println(q.dequeue());
break;
default:System.out.println("Wrong choice");
}
}
}