-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingArray.java
More file actions
44 lines (44 loc) · 1.07 KB
/
StackUsingArray.java
File metadata and controls
44 lines (44 loc) · 1.07 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
import java.util.*;
public class StackUsingArray {
public static class Stack{
static int arr[]=new int[5];
static int i=-1;
public static boolean isFull(){
return i==arr.length-1;
}
public static boolean isEmpty(){
return i==-1;
}
public static void push(int data){
if (Stack.isFull()){
System.out.println("Stack iverflow");
return;
}
i++;
arr[i]=data;
}
public static int pop(){
if (i==-1){
return -1;
}
int top=arr[i];
i--;
return top;
}
public static int peek(){
if (i==-1){
return -1;
}
return arr[i];
}
}
public static void main(String[] args) {
Stack.push(30);
Stack.push(20);
Stack.push(10);
while(!Stack.isEmpty()){
System.out.println("Top: " + Stack.peek());
Stack.pop();
}
}
}