| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
- 백준
- C언어
- IOS
- text to speech
- 커스텀팝업
- 준코딩
- deeplink
- customPopup
- BAEKJOON
- xocde
- Android Studio
- 자바
- 플러터
- FLUTTER
- 안드로이드
- Android
- TextField
- 예외처리
- label
- Swift
- storyboard
- 연결리스트
- 버블정렬
- 링크드리스트
- 안드로이드스튜디오
- Xcode
- Firebase
- android java
- 보호와 보안
- swift baekjoon
- Today
- Total
준코딩
자바 스택구현 본문
package algorithm;
public class IntStack {
private int max;
private int ptr;
private int[] stk;
public class EmptyIntStackException extends RuntimeException {
public EmptyIntStackException() {
}
}
public class OverflowIntStackException extends RuntimeException {
public OverflowIntStackException() {
}
}
public IntStack(int capacity) {
ptr = 0;
max = capacity;
try {
stk = new int[max];
} catch (OutOfMemoryError e) {
max = 0;
}
}
public int push(int x) throws OverflowIntStackException {
if (ptr >= max)
throw new OverflowIntStackException();
return stk[ptr++] = x;
}
public int pop() throws EmptyIntStackException {
if (ptr <= 0)
throw new EmptyIntStackException();
return stk[--ptr];
}
public int peek() throws EmptyIntStackException {
if (ptr <= 0)
throw new EmptyIntStackException();
return stk[ptr - 1];
}
public int indexOf(int x) {
for (int i = ptr - 1; i >= 0; i--)
if (stk[i] == x)
return i;
return -1;
}
public void clear() {
ptr = 0;
}
public int capacity() {
return max;
}
public int size() {
return ptr;
}
public boolean isEmpty() {
return ptr <= 0;
}
public boolean ifFull() {
return ptr >= max;
}
public void dump() {
if (ptr <= 0)
System.out.println("스택이 비어있습니다.");
else {
for (int i = 0; i < ptr; i++)
System.out.print(stk[i] + " ");
System.out.println();
}
}
}
'프로그래밍 > 자바' 카테고리의 다른 글
| 배열 복제 Clone (0) | 2019.01.09 |
|---|---|
| 중앙값 구하기 (0) | 2019.01.09 |
| 최대값 구하기 (0) | 2019.01.09 |
| 버블정렬코드 (스택, 큐 , 정렬) (0) | 2018.12.11 |
| BubbleSort 버블정렬 (0) | 2018.12.10 |