stack : LIFO(last in first out) 형태의 자료 구조
1
2
3
4
5
6
7
8
9
10
public class Node {
public Node next;
public int value;
public Node(int value) {
this.value = value;
}
}
stack의 기본 구성
1
2
3
4
5
6
public class Stack {
private Node top;
private int height;
}
구현 메소드 목록
1
2
3
public Stack(int value)
public void push(int value)
public Node pop()
생성자 : stack 생성시 기본 노드를 설정한다
1
2
3
4
public Stack(int value) {
this.top = new Node(value);
this.height++;
}
void push(int value) : 요소를 추가한다.
1
2
3
4
5
6
7
8
public void push(int value) {
Node newNode = new Node(value);
if (this.height != 0) {
newNode.next = this.top;
}
this.top = newNode;
this.height++;
}
Node pop() : 맨 위 요소를 가져온다.
1
2
3
4
5
6
7
8
9
10
11
public Node pop() {
if (this.height == 0) {
return null;
} else {
Node temp = this.top;
this.top = temp.next;
temp.next = null;
this.height--;
return temp;
}
}