0% found this document useful (0 votes)
11 views3 pages

De Que Operations

de queue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

De Que Operations

de queue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

Scanner;

class Deque {
int front;
int rear;
int maxsize = 5; // Predefined size of deque
int[] arr = new int[maxsize];

// Constructor to initialize deque


Deque() {
front = 0;
rear = 0;
}

// Insert at the rear side


void RDQInsert(int value) {
if (rear == maxsize - 1) {
System.out.println("Deque is full on the right side");
return;
}
if (rear == 0 && front == 0) {
// First insertion
rear = maxsize - 1;
front = maxsize - 1;
} else {
rear++;
}
arr[rear] = value;
if (front == 0) {
front = maxsize - 1;
}
}

// Insert at the front side


void LDQInsert(int value) {
if (front == 1) {
System.out.println("Deque is full on the left side");
return;
}
if (front == 0 && rear == 0) {
// First insertion
front = 1;
rear = 1;
} else {
if (front == 0) {
front = maxsize - 1;
} else {
front--;
}
}
arr[front] = value;
if (rear == 0) {
rear = maxsize - 1;
}
}

// Remove from the rear side


void RDQRemove() {
if (rear == 0) {
System.out.println("Deque is empty on the right side");
return;
}
System.out.println("Removed from rear: " + arr[rear]);
if (front == rear) {
front = 0;
rear = 0;
} else {
rear--;
}
}

// Remove from the front side


void LDQRemove() {
if (front == 0) {
System.out.println("Deque is empty on the left side");
return;
}
System.out.println("Removed from front: " + arr[front]);
if (front == rear) {
front = 0;
rear = 0;
} else {
front++;
}
}

// Display elements of the deque


void display() {
System.out.print("Deque elements: ");
if (front <= rear) {
for (int i = front; i <= rear; i++) {
System.out.print(arr[i] + " ");
}
} else {
for (int i = front; i < maxsize; i++) {
System.out.print(arr[i] + " ");
}
for (int i = 0; i <= rear; i++) {
System.out.print(arr[i] + " ");
}
}
System.out.println();
}
}

public class DequeOperations {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Deque dq = new Deque();

int choice = 0;
while (choice != 6) {
System.out.println("\nChoose an option:");
System.out.println("1. Insert at Rear");
System.out.println("2. Insert at Front");
System.out.println("3. Remove from Rear");
System.out.println("4. Remove from Front");
System.out.println("5. Display");
System.out.println("6. Exit");
choice = sc.nextInt();

switch (choice) {
case 1:
System.out.println("Enter value to insert at rear:");
int rearValue = sc.nextInt();
dq.RDQInsert(rearValue);
break;
case 2:
System.out.println("Enter value to insert at front:");
int frontValue = sc.nextInt();
dq.LDQInsert(frontValue);
break;
case 3:
dq.RDQRemove();
break;
case 4:
dq.LDQRemove();
break;
case 5:
dq.display();
break;
case 6:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice, please try again.");
}
}
sc.close();
}
}

You might also like