-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStacks.java
More file actions
60 lines (51 loc) · 1.15 KB
/
Copy pathStacks.java
File metadata and controls
60 lines (51 loc) · 1.15 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
class Stack {
int maxSize;
int[] arr;
int top;
Stack(int size) {
maxSize = size;
arr = new int[maxSize];
top = -1;
}
void insert(int value) {
if (top == maxSize - 1) return;
arr[++top] = value;
}
void delete() {
if (top == -1) return;
top--;
}
void display() {
if (top == -1) {
System.out.println("Stack is empty");
return;
}
for (int i = 0; i <= top; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
void getsize() {
System.out.println( top + 1);
}
boolean isEmpty() {
return top == -1;
}
}
public class Stacks {
public static void main(String[] args) {
Stack s = new Stack(5);
s.display();
s.insert(5);
s.insert(7);
s.insert(6);
s.display();
s.delete();
s.display();
s.insert(8);
s.insert(9);
s.display();
s.getsize(); // size
System.out.println(s.isEmpty()); // false
}
}