-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.java
executable file
·87 lines (78 loc) · 1.67 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
Lab 1 : implementation of Queue
*/
import java.util.*;
public class Queue {
private int[] q;
private int rear,front,maxsize;
public Queue(int maxsize){
this.maxsize=maxsize+1;
this.rear=0;
this.front=0;
q=new int[maxsize+1];
}
public boolean isEmpty(){
return this.rear==this.front;
}
public void enqueue(int element) throws Exception {
if(this.front == (this.rear+1)%maxsize){
Exception e= new Exception(" \t\t\t Queue Overflow ");
throw e;
}
else{
this.q[rear]=element;
this.rear= (this.rear+1)%maxsize;
}
}
public int dequeue() throws Exception {
if(isEmpty()){
Exception e= new Exception(" \t\t\t Queue Underflow ");
throw e;
}
else{
int temp= this.q[this.front];
this.front= (this.front+1)%maxsize;
return temp;
}
}
public static void main(String[] args) {
int choice,temp;
Scanner in = new Scanner(System.in);
System.out.print(" Enter max queue size : ");
temp=in.nextInt();
Queue a = new Queue(temp);
System.out.print("\n 1. Enqueue\n 2. Dequeue\n 3. Isempty\n 4. Exit \n\n ");
do{
System.out.print("\n Enter Choice : ");
choice=in.nextInt();
if(choice==1){
System.out.print(" \t\t\t Enter element : ");
temp = in.nextInt();
try{
a.enqueue(temp);
}
catch(Exception e){
e.printStackTrace();
}
}
else if (choice==2){
try{
System.out.println(" \t\t\t Removed "+ a.dequeue());
}
catch(Exception e){
e.printStackTrace();
}
}
else if(choice==3)
{
if(a.isEmpty())
System.out.println(" \t\t\t Yes ");
else
System.out.println(" \t\t\t No ");
}
else
break;
}
while(choice!=4);
}
}