-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
116 lines (102 loc) · 3.45 KB
/
Queue.java
File metadata and controls
116 lines (102 loc) · 3.45 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
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.util.Scanner;
public class Queue_Data_Structure {
int SIZE_OF_QUEUE = 8;
int array_of_Queue[] = new int[SIZE_OF_QUEUE];
int front_index, rear_index;
Queue_Data_Structure() {
front_index = -1;
rear_index = -1;
}
boolean isFull() {
if (front_index == 0 && rear_index == SIZE_OF_QUEUE - 1) {
return true;
}
return false;
}
boolean isEmpty() {
if (front_index == -1)
return true;
else
return false;
}
void enQueue_Data_Structure(int element) {
if (isFull()) {
System.out.println("Queue_Data_Structure is full");
}
else {
if (front_index == -1) {
front_index = 0;
}
rear_index++;
array_of_Queue[rear_index] = element;
System.out.println("Inserted " + element);
}
}
int deQueue_Data_Structure() {
int element;
if (isEmpty()) {
System.out.println("Queue_Data_Structure is empty");
return (-1);
}
else {
element = array_of_Queue[front_index];
if (front_index >= rear_index) {
front_index = -1;
rear_index = -1;
}
else {
front_index++;
}
System.out.println( element + " Deleted");
return (element);
}
}
void display() {
int i;
if (isEmpty()) {
System.out.println("Empty Queue_Data_Structure");
}
else {
System.out.println("\nfront_index index-> " + front_index);
System.out.println("array_of_Queue -> ");
for (i = front_index; i <= rear_index; i++)
System.out.print(array_of_Queue[i] + " ");
System.out.println("\nrear_index index-> " + rear_index);
}
}
public static void main(String[] args) {
Queue_Data_Structure q = new Queue_Data_Structure();
char ch;
System.out.println("\nSIZE of Queue Data Structure is 8\n");
Scanner scan = new Scanner(System.in);
/** Perform operations **/
do
{
System.out.println("\nSelect one of the operations::");
System.out.println("1. To insert data in the Queue_Data_Structure Data Structure.");
System.out.println("2. To display the data present in the Queue_Data_Structure Data Structure.");
System.out.println("3. To perform deQueue_Data_Structure operation on the Queue_Data_Structure Data Structure.");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
q.enQueue_Data_Structure(scan.nextInt());
break;
case 2 :
System.out.println("Queue_Data_Structure::");
q.display();
break;
case 3 :
// System.out.println("Data removed.");
q.deQueue_Data_Structure();
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}