-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
81 lines (81 loc) · 1.88 KB
/
Queue.java
File metadata and controls
81 lines (81 loc) · 1.88 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
import java.util.Scanner;
class Queue
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the queue");
int s=sc.nextInt();
Queue obj=new Queue(s);
System.out.println("\n1. insert \t 2. delete \t 3. display \t 4.Exit");
boolean flag=true;
while(flag)
{
System.out.println("\nEnter your choice:");
int c=sc.nextInt();
switch(c)
{
case 1:
System.out.println("Enter an element for insertion");
int x=sc.nextInt();
obj.insert(x);
break;
case 2:
obj.delete();
break;
case 3:
obj.display();
break;
case 4:
flag=false;
break;
default:
System.out.println("Please enter a valid choice");
break;
}
}
}
int a[],front,rear,size;
Queue(int n)
{
size=n;
a=new int[size];
front=rear=-1;
}
void insert(int x)
{
if(rear==size-1)
{
System.out.println("Queue Overflow");
return;
}
a[++rear]=x;
if(front==-1)
front=0;
}
void delete()
{
int x;
if(front==-1)
{
System.out.println("Queue Underflow");
return ;
}
x=a[front];
System.out.println("Element deleted is="+x);
if(front==rear)
front=rear=-1;
else
front++;
}
void display()
{
if(front==-1)
{
System.out.println("Queue empty");
return ;
}
for(int i=front;i<=rear;i++)
System.out.print(a[i]+" ");
}
}