-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.java
52 lines (39 loc) · 930 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
40
41
42
43
44
45
46
47
48
49
50
51
52
class StackClass {
int top = 0;
String[] data = { "", "", "" };
int maximumSize = 3;
public void get() {
System.out.print("\n " + this.top + " [ ");
for (int i = 0; i < this.top; i++) {
System.out.print(this.data[i] + " ");
}
System.out.print("]\n");
}
public void push(String newItem) {
if (this.top < this.maximumSize) {
this.data[this.top] = newItem;
this.top++;
this.get();
} else {
System.out.println("\n Stack (" + this.top + ") is full, can't add " + newItem);
}
}
public void pop() {
if (this.top != 0) {
this.top--;
this.get();
}
}
}
public class stack {
public static void main(String[] args) {
StackClass array = new StackClass();
array.push("Kevin");
array.push("Sally");
array.push("David");
array.push("Sonya");
array.pop();
array.push("Jessy");
array.push("Harry");
}
}