-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.java
More file actions
80 lines (71 loc) · 1.89 KB
/
Copy pathqueue.java
File metadata and controls
80 lines (71 loc) · 1.89 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
import java.util.*;
public class queue{
public int front = -1;
public int rear = -1;
static int number;
public int [] queue=new int[20];
//ENQUEUE TO A QUEUE
public void enqueue()
{
if(rear==19){
System.out.println("Queue is empty");
}
else{
rear++;
queue[rear]=number;
if(front ==-1){
front=0;
}
}
}
public int dequeue() {
if(front==-1){
System.out.println("Queue is empty");
}
else{
if(front == rear)
front=rear=-1;
}
front++;
return queue[front-1];
}
public void display()
{
if(front==-1) {
System.out.println("Queue is empty");
}
else{
for(int i=front ;i<=rear ;i++)
{
System.out.println(queue[i]);
}
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
queue obj1 =new queue();
System.out.println("Enter Quit for exit");
String c=sc.nextLine();
while(c !="Quit"){
System.out.println("press 1 for enqueue");
System.out.println("press 2 for dequeue");
System.out.println("press 3 for display");
int choic =sc.nextInt();
switch(choic){
case 1:
System.out.println("enter number for enqueue");
number =sc.nextInt();
obj1.enqueue();
System.out.println("enqueued successfully");
break;
case 2:
obj1.dequeue();
System.out.println("dequeued successfully");
break;
case 3:
obj1.display();
break;
}
}
}
}