public class TwoStack {
public static int TYPE_TOP_1 = 0;
public static int TYPE_TOP_2 = 1;
private StackNode top1, top2;
public TwoStack() {
top1 = null; //top1 초기화(공백 상태의 스택
top2 = null; //top2 초기화(공백 상태의 스택
}
public void push(int type, int value) { //스택의 top에 자료 삽입
StackNode newNode = new StackNode(); //새 노드 생성
newNode.value = value;
if (type == TYPE_TOP_1) {
newNode.nextLink = top1; //새 노드가 top이 가리키던 노드를 가리키도록 함
top1 = newNode;//top이 새 노드를 가리키도록 변경
} else {
newNode.nextLink = top2; //새 노드가 top이 가리키던 노드를 가리키도록 함
top2 = newNode;//top이 새 노드를 가리키도록 변경
}
}
public int peek(int type) {
if (type == TYPE_TOP_1) {
return top1.value;
} else {
return top2.value;
}
}
public int pop(int type) {
int value = peek(type);
if (type == TYPE_TOP_1) {
top1 = top1.nextLink;
} else {
top2 = top2.nextLink;
}
return value;
}
public boolean isEmpty(int type) {
if (type == TYPE_TOP_1) {
return (top1 == null);
} else {
return (top2 == null);
}
}
class StackNode {
public StackNode nextLink; //리스트 상의 다음 연결
public int value; //리스트 상의 자료
}
}
'프로그래밍 > 알고리즘' 카테고리의 다른 글
Queue (0) | 2017.02.24 |
---|---|
binarySearch (0) | 2017.02.24 |
Stack(getMin) (0) | 2017.02.24 |
문자열 rotate 체크 (0) | 2017.02.24 |
문자열 치환 (0) | 2017.02.24 |