-
Notifications
You must be signed in to change notification settings - Fork 25
/
circularqueue.java
169 lines (168 loc) · 4.44 KB
/
circularqueue.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package Data_Structures;
import java.util.*;
class circularqueue
{
private static int queue[] ;
private static int front, rear, size, len, ele, f;
circularqueue(int n)
{
size = n;
len = 0;
queue = new int[size];
front = -1;
rear = -1;
}
public boolean isEmpty()
{
return front == -1;
}
public boolean isFull()
{
return (front==0 && rear == size-1 || front == (rear+1 % size));
}
public int getSize()
{
return len ;
}
public void peek()
{
if (isEmpty())
{
System.out.println ("Underflow Exception");
}
else
{
f=queue[front];
System.out.println (f);
}
}
public void insert(int i)
{
if (front==0 && rear== size-1 || front == (rear+1)%size)
{
System.out.println("Overflow Exception");
}
else
{
if (rear == -1)
{
front = 0;
rear = 0;
queue[rear] = i;
}
else
{
rear = (rear + 1) % size;
queue[rear] = i;
}
len++ ;
}
}
public int remove()
{
if (front==-1)
{
System.out.println ("Underflow Exception");
}
else
{
len-- ;
ele = queue[front];
if ( front == rear)
{
front = -1;
rear = -1;
}
else
{
front=(front+1)%size;
}
}
return ele;
}
public void display()
{
int front_pos = front, rear_pos = rear;
System.out.print("\nQueue = ");
if (front==-1)
{
System.out.print("Empty\n");
}
else
{
if(front_pos <= rear_pos)
{
while(front_pos <= rear_pos)
{
System.out.print(queue[front_pos]+" ");
front_pos++;
}
}
else
{
while(front_pos <= size-1)
{
System.out.print(queue[front_pos]+" ");
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
System.out.print(queue[front_pos]+" ");
front_pos++;
}
}
}
System.out.println();
}
public static void main(String argsp[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Array Queue Test\n");
System.out.println("Enter Size of Integer Queue ");
int n = scan.nextInt();
circularqueue q = new circularqueue(n);
char ch;
do{
System.out.println("\nQueue Operations");
System.out.println("1. Insert");
System.out.println("2. Remove");
System.out.println("3. Peek");
System.out.println("4. Check Empty");
System.out.println("5. Check Full");
System.out.println("6. Size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
q.insert( scan.nextInt() );
break;
case 2 :
System.out.println("Removed Element = "+q.remove());
break;
case 3 :
System.out.print("Peek Element = ");
q.peek();
break;
case 4 :
System.out.println("Empty status = "+q.isEmpty());
break;
case 5 :
System.out.println("Full status = "+q.isFull());
break;
case 6 :
System.out.println("Size = "+ q.getSize());
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
q.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
}
while (ch == 'Y'|| ch == 'y');
scan.close();
}
}