-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.java
49 lines (47 loc) · 891 Bytes
/
Stack.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
package stack;
public class Stack {
static final int max=100;
int top=-1;
int[] a=new int[max];
boolean push(int x)
{
if (top >= (max - 1)) {
System.out.println("Stack Overflow");
return false;
}
else {
a[++top] = x;
System.out.println(x + " pushed into stack");
return true;
}
}
int pop() {
if(top<0) {
System.out.println("stack underflow");
return 0;
}
else {
int x=a[top];
top--;
return x;
}
}
int peek() {
if(top<0) {
System.out.println("stack underflow");
return 0;
}
else {
int x=a[top];
return x;
}
}
public static void main(String args[]) {
Stack st=new Stack();
for(int i=0;i<10;i++) {
st.push(i);
}
System.out.println(st.pop()+" popped out");
System.out.println(st.peek()+" peeked element");
}
}