forked from srinidh-007/Coding_Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackmain.java
72 lines (58 loc) · 1.26 KB
/
stackmain.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
/**
* stackmain
*/
class stack{
int top=-1;
int limit = 5;
float array[] = new float[limit];
boolean push(float x)
{
if (top >= (limit - 1)) {
System.out.println("Stack Overflow");
return false;
}
else {
array[++top] = x;
return true;
}
}
float pop()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
float x = array[top--];
return x;
}
}
void display() {
for(int i=top;i>=0;i--) {
System.out.println("| " + array[i] + " ");
}
System.out.println("\n");
}
public static void copy(stack s1, stack s2){
int i;
for(i=0;i<=s1.top;i++) {
s2.array[i] = s1.array[i];
}
s2.top = s1.top;
}
}
class stackmain {
public static void main(String[] args) {
stack stack1 = new stack();
stack stack2 = new stack();
stack1.push(2.1f);
stack1.push(2.2f);
stack1.push(2.3f);
stack1.pop();
stack1.push(2.4f);
stack1.pop();
stack.copy(stack1,stack2);
stack1.display();
stack2.display();
}
}