-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMyStack.java
53 lines (48 loc) Β· 1.06 KB
/
MyStack.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
53
package day9;
public class MyStack {
private final int[] array = new int[20];
private int position = 0;
/*
push(int value)
place value at top of stack
@return void
*/
public void push(int element) {
if (size() == array.length) {
System.out.println("stack overflow error");
return;
}
array[position++] = element;
}
/*
peek()
top element --> value
@return top element value
*/
public int peek() {
if (size() == 0) {
System.out.println("empty stack error");
return -1;
}
return array[position - 1];
}
/*
pop()
@return top element value
remove top element
*/
public int pop() {
if (size() == 0) {
System.out.println("stack underflow error");
return -1;
}
return array[--position];
}
/*
size()
@return --> number of elements
*/
public int size() {
return position;
}
}