-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.java
39 lines (35 loc) · 877 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
package doughts;
public class Stack {
private int tos;
private int [] arr;
private int size;
public Stack(int size){
tos=-1;
this.size=size;
arr=new int[size];
}
public void push(int x){
if(tos==size-1){
System.out.println("Stack Full Ho gya hai");
}
tos++;
arr[tos]=x;
System.out.println("Element pushed:"+x);
}
public int pop(){
if(tos==-1){
System.out.println("Stack is underflow");
}
int x=arr[tos];
tos--;
System.out.println("Element is poped"+x);
return x;
}
public int peek(){
if(tos==-1){
System.out.println("Stack is underflow");
}
System.out.println("Top element is:"+ arr[tos]);
return arr[tos];
}
}