forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDequeueExample.java
99 lines (93 loc) · 3.18 KB
/
DequeueExample.java
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
import java.util.LinkedList;
import java.util.Scanner;
public class DequeueExample {
public static void main(String[] args) {
LinkedList<String> Queue = new LinkedList<String>();
/*
* What is DeQueue?
* DeQueue stands for Double Ended Queue that means elements
* can be inserted or deleted from both the end.
*
* Operations
* 1. Insert_first : Add an element at the start of Queue.
* 2. Insert_last : Add an element at the end of Queue.
* 3. Delete_first : Remove an element from the first of Queue.
* 4. Delete_last : Remove an element from the end of Queue.
* 3. Display : Print the all elements in a Queue.
*
* Input Restricted DeQueue:
* 1. Insert_last : Add an element at the end of Queue.
* 2. Delete_first : Remove an element from the first of Queue.
* 3. Delete_last : Remove an element from the end of Queue.
* 4. Display : Print the all elements in a Queue.
*
* Output Restricted DeQueue:
* 1. Insert_first : Add an element at the start of Queue.
* 2. Insert_last : Add an element at the end of Queue.
* 3. Delete_first : Remove an element from the first of Queue.
* 4. Display : Print the all elements in a Queue.
*/
Scanner sc = new Scanner(System.in);
System.out.println("Total numbers of elements: ");
int maxsize = sc.nextInt();
System.out.println("Total numbers of elements: "+maxsize);
boolean n=true;
while(n) {
System.out.println("1.Insert_last 2.Delete_first 3.Display 4.Quit 5.Insert first 6.Delete_last");
//Scanner sc = new Scanner(System.in);
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch(choice) {
case 1: if(Queue.size()==maxsize)
System.out.println("Queue is full");
else
{
System.out.println("Enter a String as Input: ");
String val = sc.next();
Queue.addLast(val);
System.out.println("Your Queue: "+Queue);
}
break;
case 2: if(Queue.size()==0)
System.out.println("Queue Empty");
else
System.out.println("Deleting: "+Queue.removeFirst());
break;
case 3: System.out.println(Queue);
break;
case 4: sc.close();
n=false;
break;
case 5: if(Queue.size()==maxsize)
System.out.println("Queue is full");
else
{
System.out.println("Enter a String as Input: ");
String val1 = sc.next();
Queue.addFirst(val1);
System.out.println("Your Queue: "+Queue);
}
break;
case 6: if(Queue.size()==0)
System.out.println("Queue is Empty");
else
System.out.println("Deleting: "+Queue.removeLast());
break;
}
}
//Insert Operation
Queue.addLast("Sun");
Queue.addLast("Mon");
Queue.addLast("Tues");
Queue.addLast("Wednes");
Queue.addLast("Thurs");
//Print the Queue
System.out.println("The elements within Queue: "+Queue);
//Delete Operation
System.out.println("Delete() : "+Queue.removeFirst());
System.out.println("Delete() : "+Queue.removeFirst());
System.out.println("The elements within Queue : "+Queue);
//Display Operation
System.out.println("Display : "+Queue);
}
}