-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular_Queue.java
More file actions
103 lines (103 loc) · 2.42 KB
/
Copy pathCircular_Queue.java
File metadata and controls
103 lines (103 loc) · 2.42 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
99
100
101
102
103
import java.util.Scanner;
class Circular_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();
Circular_Queue obj=new Circular_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 q[],front,rear,size;
Circular_Queue(int n)
{
size=n;
q=new int[size];
front=rear=-1;
}
void insert(int x)
{
if((rear==size-1 && front==0) || (front==rear+1))
System.out.println("Queue Overflow");
else if (front==-1)
{
front=rear=0;
q[rear]=x;
}
else if(rear==size-1 && front>0)
{
rear=0;
q[rear]=x;
}
else
{
rear++;
q[rear]=x;
}
}
void delete()
{
int x;
if(front==-1)
{
System.out.println("Queue Underflow");
return;
}
x=q[front];
System.out.println("Element deleted is="+x);
q[front]=-1;
if(front==rear)
front=rear=-1;
else if(front==size-1)
front=0;
else
front++;
}
void display()
{
if(front==-1)
{
System.out.println("Queue empty");
return;
}
if(rear>=front)
{
for(int i=front;i<=rear;i++)
System.out.print(q[i]+" ");
}
else
{
for(int i=front;i<size;i++)
System.out.print(q[i]+" ");
for(int i=0;i<=rear;i++)
System.out.print(q[i]+" ");
}
}
}