forked from im-anahata/-HacktoberFest2k22-Public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack and Queue using Interface in java
212 lines (194 loc) · 3.67 KB
/
Stack and Queue using Interface in 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import java.util.Scanner;
interface Stack
{
int push(int data);
int pop();
void sdisplay();
}
interface Queue
{
int enqueue(int data);
int dequeue();
void qdisplay();
}
class StackQueue implements Stack, Queue
{
int sizes, top = -1;
private int[] sarr, qarr;
int sizeq, front = -1, rear = -1;
StackQueue(int sizes, int sizeq)
{
this.sizes = sizes;
this.sizeq = sizeq;
this.sarr = new int[sizes];
this.qarr = new int[sizeq];
}
public int push(int data) {
if (this.top == this.sizes - 1)
{
return -1;
}
else
{
top++;
this.sarr[this.top] = data;
return 1;
}
}
public int pop() {
if (this.top == -1)
{
return -1;
}
else
{
int temp = this.sarr[this.top];
this.top--;
return temp;
}
}
public void sdisplay() {
for (int i = this.top; i >= 0; i--)
{
System.out.print(this.sarr[i] + " ");
}
}
public int enqueue(int data) {
if (this.rear == this.sizeq - 1)
{
return -1;
}
else
{
if (this.front == -1 & this.rear == -1) {
this.front = 0;
}
this.rear++;
this.qarr[this.rear] = data;
return 1;
}
}
public int dequeue() {
if (this.front == -1)
{
return -1;
}
else
{
int temp = this.qarr[this.front];
this.front++;
if (this.front > this.rear)
{
this.front = -1;
this.rear = -1;
}
return temp;
}
}
public void qdisplay()
{
for (int i = this.front; i <= this.rear; i++)
{
System.out.print(this.qarr[i] + " ");
}
}
}
class StackTest
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of Stack: ");
int sizes = sc.nextInt();
System.out.println("Enter size of Queue: ");
int sizeq = sc.nextInt();
StackQueue sq = new StackQueue(sizes, sizeq);
while (true)
{
System.out.println("\n1.Stack\n2.Queue\n3.Exit");
int ch = sc.nextInt();
switch (ch)
{
case 1:
int choice;
do
{
System.out.print("1.Push\n2.Pop\n3.Display\n4.Exit Stack\nEnter your choice: ");
choice = sc.nextInt();
switch (choice)
{
case 1:
System.out.print("Enter the data: ");
int data = sc.nextInt();
int temp1 = sq.push(data);
if (temp1 == -1)
{
System.out.println("Stack Overflow");
}
else
{
System.out.println(data + " pushed");
}
break;
case 2:
int temp2 = sq.pop();
if (temp2 == -1)
{
System.out.println("Stack Underflow");
}
else
{
System.out.println(temp2 + " poped");
}
break;
case 3:
sq.sdisplay();
break;
default:
System.out.println("Enter valid choice");
break;
}
} while ((choice != 4));
break;
case 2:
do
{
System.out.print("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit Queue\nEnter your choice: ");
choice = sc.nextInt();
switch (choice)
{
case 1:
System.out.println("Enter the data: ");
int data = sc.nextInt();
int temp = sq.enqueue(data);
if (temp == -1)
{
System.out.println("Queue is full");
}
else
{
System.out.println(data + " enqueued");
}
break;
case 2:
int temp1 = sq.dequeue();
if (temp1 == -1)
{
System.out.println("Queue is Empty");
}
else
{
System.out.println(temp1 + " Dequeued");
}
break;
case 3:
sq.qdisplay();
break;
}
} while (choice != 4);
break;
case 3:
System.exit(0);
}
}
}
}