Program 9
Program 9
PROBLEM DEFINITION :-
Write a program to perform the following operations on a double-ended queue using switch case.
ALGORITHM :-
STEP 1 - START
STEP 3 - Declare integer variables: `rear`, `front`, `size`, and an integer array `a[]`.
- Set `size` to `n` and initialize the array `a[]` of size `n`.
STEP 5 - Ask the user for the size of the queue and initialize the `Dequeue` object.
STEP 7 - Start a loop that runs until the user selects the option to quit (i.e., `6`).
- If rear and front are -1, initialize both and insert an element at the rear.
- If `front` is not at the first position, decrement `front` and insert the element.
- If the queue has one element, delete it and reset `front` and `rear` to -1.
- If the queue has one element, delete it and reset `front` and `rear` to -1.
STEP 15 – END.
PROGRAM CODE :-
package Project_12;
import java.util.*;
public class Dequeue {
Scanner sc = new Scanner(System.in);
int rear;
int front;
int size;
int[] a;
Dequeue(int n) {
a = new int[n];
rear = -1;
front = -1;
size = n;
}
void InRear() {
if (rear == size - 1 && front == 0) {
System.out.println("Queue is full.");
return;
}
if (rear == size - 1) {
System.out.println("Cannot enter to the rear.");
return;
}
if (rear == -1 && front == -1) {
rear++;
front++;
System.out.println("Enter the number to insert :");
a[rear] = sc.nextInt();
} else {
rear++;
System.out.println("Enter the number to insert :");
a[rear] = sc.nextInt();
}
}
void InFront() {
if (rear == size - 1 && front == 0) {
System.out.println("Queue is full.");
return;
}
if (front == 0) {
System.out.println("Cannot enter to the front.");
return;
} else {
front--;
System.out.println("Enter the number to insert :");
a[front] = sc.nextInt();
}
}
void DelFront() {
if (rear == -1 && front == -1) {
System.out.println("Queue id full.");
}
if (rear == front) {
System.out.println(a[front] + " is deleted successfully");
System.out.println("Now Queue is empty.");
front = -1;
rear = -1;
} else {
System.out.println(a[front] + " is deleted successfully");
front++;
}
}
void DelRear() {
if (rear == -1 && front == -1) {
System.out.println("Queue id full.");
}
if (rear == front) {
System.out.println(a[rear] + " is deleted successfully");
System.out.println("Now Queue is empty.");
front = -1;
rear = -1;
} else {
System.out.println(a[rear] + " is deleted successfully");
rear--;
}
}
void Display() {
if (rear == -1 && front == -1) {
System.out.println("Queue id full.");
} else {
for (int i = front; i <= rear; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
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();
Dequeue dq = new Dequeue(n);
System.out.println("Enter as following :");
System.out.println("'1' : To insert at rear.");
System.out.println("'2' : To insert at front.");
System.out.println("'3' : To delete from front.");
System.out.println("'4' : To delete from rear.");
System.out.println("'5' : To display all contents.");
System.out.println("'6' : To quit from the system.");
int k = 0;
do {
System.out.println("Enter you choice :");
k = sc.nextInt();
switch (k) {
case 1:
dq.InRear();
break;
case 2:
dq.InFront();
break;
case 3:
dq.DelFront();
break;
case 4:
dq.DelRear();
break;
case 5:
dq.Display();
break;
case 6:
System.out.println("Operation Ended.");
break;
}
} while (k != 6);
sc.close();
}
}
}
INPUT OUTPUT :-
VARIABLE DESCRIPTION TABLE :-