-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
78 lines (57 loc) · 1.34 KB
/
Stack.java
File metadata and controls
78 lines (57 loc) · 1.34 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
package Collection;
public class Stack {
int[] stack =new int[10];
int temp =-1;
public void push(int data){
if((stack.length -1)==(temp)){
}else
temp++;
stack[temp]= data;
}
public void pop(){
if (temp == -1){
}
int val = stack[temp];
stack[temp]= 0;
temp--;
return;
}
public int peek() {
if (temp == -1) {
System.out.println("Stack is empty......");
return 0;
}
else {
int val = stack[temp];
return val;
}
}
public void printStack() {
System.out.println("Stack elements:");
for (int i = temp; i >= 0; i--) {
System.out.println(stack[i]);
}
}
}
class Stackmain{
public static void main(String[] args) {
Stack obj = new Stack();
try {
for (int i = 0; i < 10; i++) {
}
} catch (StackOverflowError ex) {
System.out.println("Stack Overflow >>>>");
}
obj.push(10);
obj.push(20);
obj.push(30);
obj.push(40);
obj.push(50);
obj.push(60);
obj.push(70);
obj.pop();
obj.pop();
obj.printStack();
System.out.println("Top element is :"+ obj.peek());
}
}