-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathmultipleStacks.java
More file actions
83 lines (74 loc) · 1.79 KB
/
Copy pathmultipleStacks.java
File metadata and controls
83 lines (74 loc) · 1.79 KB
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
import java.util.*;
public class multipleStacks {
int topA, topB, stack[];
multipleStacks(int max)// parameterised constructor
{
topA = -1;
topB = max;
stack = new int[max];
}
void pushA(int n)// pushing into A
{
if (topA == topB - 1)// checking for overflowing
{
System.out.println("Stack A Overflows");
} else {
++topA;
stack[topA] = n;
}
}
void pushB(int n)// pushing into stack B
{
if (topB - 1 == topA)// checking for overflowing
{
System.out.println("Stack B Overflows");
} else {
--topB;
stack[topB] = n;
}
}
int popA()// poping from A
{
if (topA == -1) {
System.out.println("Stack A underflows");
return (-999);
} else {
int value;
value = stack[topA];
--topA;
return (value);
}
}
int popB()// poping from B
{
if (topB == stack.length) {
System.out.println("Stack B underflows");
return (-999);
} else {
int value;
value = stack[topB];
++topB;
return (value);
}
}
void displayA() {
if (topA == -1) {
System.out.println("Stack A is empty");
}
int i;
System.out.println("Stack A");
for (i = topA; i >= 0; i--) {
System.out.println(stack[i]);
}
}
void displayB() {
if (topB == stack.length) {
System.out.println("Stack B is empty");
}
int i;
System.out.println("Stack B");
for (i = topB; i < stack.length; i++) {
System.out.println(stack[i]);
}
}
}