-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.java
More file actions
75 lines (63 loc) · 1.52 KB
/
ArrayStack.java
File metadata and controls
75 lines (63 loc) · 1.52 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
package Stack;
public class ArrayStack implements Stack{
private int top;
private int stackSize;
private char itemArray[];
public ArrayStack(int stackSize) {
this.top = -1;
this.stackSize = stackSize;
this.itemArray = new char[this.stackSize];
}
@Override
public boolean isEmpty() {
return (top==-1);
}
public boolean isFull() {
return (top==stackSize-1);
}
@Override
public void push(char item) {
if(isFull()){
System.out.println("스택이 꽉차있음");
}else{
itemArray[++top] = item;
}
}
@Override
public char pop() {
if(isEmpty()){
System.out.println("스택이 비어있음");
return 0;
}else{
return itemArray[top--];
}
}
@Override
public void delete() {
if(isEmpty()){
System.out.println("삭제할 요소가 존재하지 않음");
}else{
top--;
}
}
@Override
public char peek() {
if(isEmpty()){
System.out.println("스택이 비어있음");
}else{
return itemArray[top];
}
return 0;
}
public void printStack() {
if(isEmpty()){
System.out.println("스택이 비어있음");
}else{
System.out.println("<<Stack>>");
for(int i=top; i>-1; i--){
System.out.println(itemArray[i]);
}
System.out.println();
}
}
}