0% found this document useful (0 votes)
0 views

Program 9

Uploaded by

nandiswastik2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Program 9

Uploaded by

nandiswastik2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PROGRAM No.

PROBLEM DEFINITION :-
Write a program to perform the following operations on a double-ended queue using switch case.

Case 1: Insert at rear

Case 2: insert at front

Case 3: delete from front

Case 4: delete from rear

Case 5: display the contents

Case 6: Quit from the system.

ALGORITHM :-
STEP 1 - START

STEP 2 - Declare a scanner object to take input.

STEP 3 - Declare integer variables: `rear`, `front`, `size`, and an integer array `a[]`.

STEP 4 - Initialize the deque using a constructor `Dequeue(int n)`:

- Set `rear` and `front` to -1.

- 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 6 - Display menu options to the user for deque operations.

STEP 7 - Start a loop that runs until the user selects the option to quit (i.e., `6`).

STEP 8 - Ask for the user's choice.

STEP 9 - If choice is 1 (insert at rear), call `InRear()`:

- Check if the queue is full.

- If the rear is at the last position or full, print "Queue is full."

- If rear and front are -1, initialize both and insert an element at the rear.

- Otherwise, increment `rear` and insert the element at the rear.

STEP 10 - If choice is 2 (insert at front), call `InFront()`:

- Check if the queue is full or insertion at the front is not possible.

- If `front` is not at the first position, decrement `front` and insert the element.

STEP 11 - If choice is 3 (delete from front), call `DelFront()`:

- If the queue is empty, print "Queue is full."

- If the queue has one element, delete it and reset `front` and `rear` to -1.

- Otherwise, increment `front` to delete the element.

STEP 12 - If choice is 4 (delete from rear), call `DelRear()`:


- If the queue is empty, print "Queue is full."

- If the queue has one element, delete it and reset `front` and `rear` to -1.

- Otherwise, decrement `rear` to delete the element.

STEP 13 - If choice is 5 (display the queue), call `Display()`:

- If the queue is empty, print "Queue is full."

- Otherwise, display elements between `front` and `rear`.

STEP 14 - If choice is 6, exit the loop.

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 :-

Name Type Scope Purpose


sc Scanner Class-level To take input from the user.
rear int Class-level Tracks the rear index of the queue.
front int Class-level Tracks the front index of the queue.
size int Class-level Stores the size of the deque (queue).
a[] int[] Class-level Stores the elements of the deque.
n int Method-level (main) Stores the size of the queue inputted by the
user.
k int Method-level (main) Stores the user’s choice for operations.

METHOD DESCRIPTION TABLE :-

Name Return Argument Argument Purpose


Type Name Type
InRear() void None N/A Inserts an element at the rear of the deque.
InFront() void None N/A Inserts an element at the front of the deque.
DelFront void None N/A Deletes an element from the front of the
() deque.
DelRear void None N/A Deletes an element from the rear of the
() deque.
Display( void None N/A Displays all elements in the deque.
)
main() void args String[] Entry point for the program.
Dequeu Constructo n int Initializes the deque with a size n.
e() r

You might also like