Wed Oct 20 2021

Double Ended Queue

File Name: deque.java

import java.io.*;
import java.util.*;

class datadeque {
	public static void main(String args[ ]) {
		Deque deque = new LinkedList();
		Scanner scan = new Scanner(System.in);
		int opt = 0;
		while(opt != 6) {
			System.out.println("1. Front Enqueue 2. Rear Endeque 3. Front Dequeue 4. Rear Dequeue 5. Display 6. Exit");
			System.out.println("Enter your choice:");
			opt = Integer.parseInt(scan.nextLine());
			switch(opt) {
				case 1:
					System.out.println("Please Enter Value in front of the Queue:");

					/* Enqueue data at the front */
					if(deque.offerFirst(scan.nextLine()))
						System.out.println("Data successfully enqueue at front!");
					break;
				case 2:
					System.out.println("Please Enter Value in rear of the Queue:");

					/* Enqueue data at the rear */
					if(deque.offerLast(scan.nextLine()))
						System.out.println("Data successfully enqueue at rear!");
					break;
				case 3:

					/* Dequeue data from the front */
					System.out.println(deque.pollFirst()+" - dequeue successfully!");
					break;
				case 4:

					/* Dequeue data from the rear */
					System.out.println(deque.pollLast()+" - dequeue successfully!");
					break;
				case 5:
					System.out.println("Deque:");
					System.out.println(deque);
					break;
				case 6:
					System.out.println("Bye Bye!");
					System.exit(0);
					break;
				default:
					System.out.println("Invalid choice!");
			}
		}
	}
}




/* Output */
1. Front Enqueue 2. Rear Endeque 3. Front Dequeue 4. Rear Dequeue 5. Display 6. Exit
Enter your choice:
1

Please Enter Value in front of the Queue:
5

Data successfully enqueue at front!

1. Front Enqueue 2. Rear Endeque 3. Front Dequeue 4. Rear Dequeue 5. Display 6. Exit
Enter your choice:
2

Please Enter Value in rear of the Queue:
6

Data successfully enqueue at rear!

1. Front Enqueue 2. Rear Endeque 3. Front Dequeue 4. Rear Dequeue 5. Display 6. Exit
Enter your choice:
1

Please Enter Value in front of the Queue:
7

Data successfully enqueue at front!

1. Front Enqueue 2. Rear Endeque 3. Front Dequeue 4. Rear Dequeue 5. Display 6. Exit
Enter your choice:
2

Please Enter Value in rear of the Queue:
8

Data successfully enqueue at rear!

1. Front Enqueue 2. Rear Endeque 3. Front Dequeue 4. Rear Dequeue 5. Display 6. Exit
Enter your choice:
5

Queue:
[7, 5, 6, 8]

1. Front Enqueue 2. Rear Endeque 3. Front Dequeue 4. Rear Dequeue 5. Display 6. Exit
Enter your choice:
3

7 - dequeued successfully!

1. Front Enqueue 2. Rear Endeque 3. Front Dequeue 4. Rear Dequeue 5. Display 6. Exit
Enter your choice:
4

8 - dequeued successfully!

1. Front Enqueue 2. Rear Endeque 3. Front Dequeue 4. Rear Dequeue 5. Display 6. Exit
Enter your choice:
6

Bye Bye!
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.