-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathDoubleEndedQueue.java
157 lines (154 loc) · 4.29 KB
/
DoubleEndedQueue.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.util.*;
//class Double ended queue
//class insert_at_first
//class insert_at_last
//class delete_element
//class display
class doublequeue{
int front=-1,rear=-1;
int queue[]=new int[10];
void insert_at_first(int x){
if(front==-1){
front=rear=0;
queue[front]=x;
}
else if(front==0){
System.out.println("Queue is full");
}
else{
front--;
queue[front]=x;
}
}
void insert_at_last(int x){
if(front==-1){
front=rear=0;
queue[rear]=x;
}
else if(rear==9){
System.out.println("Queue is full");
}
else{
rear++;
queue[rear]=x;
}
}
void delete_element(){
if(front==-1){
System.out.println("Queue is empty");
}
else if(front==rear){
front=rear=-1;
}
else{
front++;
}
}
//sort
void sort(){
int temp;
for(int i=front;i<=rear;i++){
for(int j=i+1;j<=rear;j++){
if(queue[i]>queue[j]){
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}
}
}
}
//search_element
void search_element(int x){
int flag=0;
for(int i=front;i<=rear;i++){
if(queue[i]==x){
flag=1;
break;
}
}
if(flag==1){
System.out.println("Element found");
}
else{
System.out.println("Element not found");
}
}
//replace_second_element
void replace_second_element(int x){
if(front==-1){
System.out.println("Queue is empty");
}
else if(front==rear){
System.out.println("Only one element is present");
}
else{
queue[front+1]=x;
}
}
void display(){
if(front==-1){
System.out.println("Queue is empty");
}
else{
for(int i=front;i<=rear;i++){
System.out.println(queue[i]);
}
}
}
}
//main class
class DoubleEndedQueue{
public static void main(String args[]){
doublequeue dq=new doublequeue();
Scanner sc=new Scanner(System.in);
int ch,x;
do{
System.out.println("1.Insert at first");
System.out.println("2.Insert at last");
System.out.println("3.Delete element");
System.out.println("4.Display");
System.out.println("5.Sort");
System.out.println("6.Search element");
System.out.println("7.Replace second element");
System.out.println("8.Exit");
System.out.println("Enter your choice");
ch=sc.nextInt();
switch(ch){
case 1:
System.out.println("Enter element to be inserted");
x=sc.nextInt();
dq.insert_at_first(x);
break;
case 2:
System.out.println("Enter element to be inserted");
x=sc.nextInt();
dq.insert_at_last(x);
break;
case 3:
dq.delete_element();
break;
case 4:
dq.display();
break;
case 5:
dq.sort();
break;
case 6:
System.out.println("Enter element to be searched");
x=sc.nextInt();
dq.search_element(x);
break;
case 7:
System.out.println("Enter element to be replaced");
x=sc.nextInt();
dq.replace_second_element(x);
break;
case 8:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}while(ch!=8);
}
}