-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
78 lines (63 loc) · 1.47 KB
/
Copy pathStack.java
File metadata and controls
78 lines (63 loc) · 1.47 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
//Stack Implementation using Array
// Youtube : https://www.youtube.com/watch?v=GYptUgnIM_I&list=PLgUwDviBIf0oSO572kQ7KCSvCUh1AdILj
public class Stack{
static int[] arr;
static int top;
public Stack(int n){
arr = new int[n];
top = -1;
}
public static void push(int val){
if((top == arr.length-1)){
System.out.println("Stack is full");
}else{
arr[++top] = val;
}
}
public static void pop(){
if(! isEmpty()){
top = top-1;
}else{
System.out.println("Stack already empty");
}
}
public static int top(){
if(!isEmpty()){
return arr[top];
}else{
System.out.println("stack is empty");
return -1;
}
}
public static int size(){
return top+1;
}
public static boolean isEmpty(){
return top == -1;
}
public static void printStack(){
if(!isEmpty()){
for(int i=0;i<=top;i++){
System.out.println(arr[i]);
}
}else{
System.out.println("stack is empty");
}
}
public static void main(String[] args){
Stack st = new Stack(4);
st.push(2);
st.push(3);
st.push(4);
st.push(6);
st.push(2);
st.pop();
st.pop();
st.printStack();
}
}
/*
Stack is full
2
3
*/