-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack2
More file actions
74 lines (71 loc) · 1.45 KB
/
Copy pathStack2
File metadata and controls
74 lines (71 loc) · 1.45 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
class stack_2in_array
{
int top1, top2;
static int n=8;
int a[];
stack_2in_array()
{
top1=-1;
top2=n;
a = new int[n];
}
public void push1(int x)
{
if (top1 < (top2-1))
{
a[++top1] = x;
System.out.println(x+" pushed in stack 1");
}
else
{
System.out.println("Overflow!");
}
}
public void push2(int x)
{
if (top1 < (top2-1))
{
a[--top2] = x;
System.out.println(x+" pushed in stack 2");
}
else
{
System.out.println("Overflow!");
}
}
public void pop1()
{
if (top1 >= 0)
{
int x = a[top1--];
System.out.println(x+" removed from stack 1");
}
else {
System.out.println("Stack 1 Underflow!");
}
}
public void pop2()
{
if (top2 < n)
{
int x = a[top2++];
System.out.println(x+" removed from stack 2");
}
else
{
System.out.println("Underflow! stack 2");
}
}
public static void main(String args[])
{
stack_2in_array s=new stack_2in_array();
s.push1(1);
s.push2(11);
s.push2(5);
s.push1(3);
s.push2(4);
s.pop1();
s.pop1();
s.pop1();
}
}