-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
51 lines (37 loc) · 895 Bytes
/
Stack.java
File metadata and controls
51 lines (37 loc) · 895 Bytes
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
public class Stack {
int[] arrs;
int top;
int size;
public Stack(){
arrs = new int[5];
top = -1;
size = arrs.length;
}
//push element in stack
public void push(int data){
if(top < size )
arrs[++top] = data;
else
System.out.println("Stack overflow " + data);
}
//to pop the element in the stack
public void pop(){
if(top == -1){
System.out.println("Stack underflow");
}
else{
arrs[top--] = 0;
}
}
//to print peek element
public int peek(){
return arrs[top];
}
//print element in stack
void display(){
for (int arr:arrs){
System.out.print(arr+" ");
}
System.out.println();
}
}