일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 링크드리스트
- android java
- 자바
- Xcode
- FLUTTER
- Swift
- xocde
- 보호와 보안
- 예외처리
- storyboard
- Android Studio
- 플러터
- BAEKJOON
- 준코딩
- swift baekjoon
- Firebase
- 연결리스트
- text to speech
- Android
- 커스텀팝업
- IOS
- deeplink
- 버블정렬
- C언어
- 백준
- label
- customPopup
- 안드로이드
- TextField
- 안드로이드스튜디오
- 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 |