- Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathStackExample.java
More file actions
Latest commit
50 lines (41 loc) · 1.21 KB
/
Copy pathStackExample.java
File metadata and controls
50 lines (41 loc) · 1.21 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
packagech09;
publicclassStackExample {
staticclassMyNode {
intitem;
// 노드의 다음 노드
MyNodenext;
publicMyNode(intitem, MyNodenext) {
this.item = item;
this.next = next;
}
}
staticclassMyStack {
MyNodelast;
// 스택 최초 생성시 마지막 노드는 없음
publicMyStack() {
this.last = null;
}
publicvoidpush(intitem) {
// 입력값으로 신규 노드를 생성하며, 기존의 마지막 노드는 다음 노드가 된다.
this.last = newMyNode(item, this.last);
}
publicintpop() {
// 마지막 노드의 값을 끄집어낸다.
intitem = this.last.item;
// 마지막 노드를 한 칸 앞으로 이동한다.
this.last = this.last.next;
returnitem;
}
}
publicstaticvoidmain(String[] args) {
MyStackstack = newMyStack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
for (inti = 0; i < 5; i++) {
System.out.println(stack.pop());
}
}
}