-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue1.java
More file actions
98 lines (91 loc) · 1.82 KB
/
Queue1.java
File metadata and controls
98 lines (91 loc) · 1.82 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
import java.util.Scanner;
public class Queue1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the queue:");
int n=sc.nextInt();
new Queue();
new Queue(n);
}
}
class Queue{
int front=-1,rear=-1,item;
int[]arr=new int[15];
Queue()
{
System.out.println("\nPERFORMING QUEUE OPERATIONS\n------------------------------\n");
}
Queue(int n)
{
int choice;
do {
Scanner sc1=new Scanner(System.in);
System.out.println("\nOPERATIONS\n-----------\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n");
System.out.println("Enter choice:");
choice=sc1.nextInt();
switch(choice)
{
case 1:
enqueue(n);
break;
case 2:
dequeue(n);
break;
case 3:
display(rear);
break;
case 4:
System.exit(0);
default:
break;
}
}while(choice<5);
}
void enqueue(int n)
{
if(rear>=n-1)
{
System.out.println("\n Overflow");
}
else
{
if(front==-1&&rear==-1)
{
front++;
}
System.out.println("\nEnter the element to be inserted: ");
Scanner sc2=new Scanner(System.in);
item=sc2.nextInt();
rear++;
arr[rear]=item;
}
}
void dequeue(int n)
{
if(front==-1)
{
System.out.println("\n Underflow");
}
else {
int del=arr[front];
System.out.printf("Deleted element is: %d",del);
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front++;
}
}
}
void display(int rear)
{
System.out.println("Displaying elements");
for(int i=front;i<=rear;i++)
{
System.out.printf("%d ",arr[i]);
}
}
}