Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
50 lines (41 loc) · 1.21 KB

File metadata and controls

50 lines (41 loc) · 1.21 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package ch09;
public class StackExample {
static class MyNode {
int item;
// 노드의 다음 노드
MyNode next;
public MyNode(int item, MyNode next) {
this.item = item;
this.next = next;
}
}
static class MyStack {
MyNode last;
// 스택 최초 생성시 마지막 노드는 없음
public MyStack() {
this.last = null;
}
public void push(int item) {
// 입력값으로 신규 노드를 생성하며, 기존의 마지막 노드는 다음 노드가 된다.
this.last = new MyNode(item, this.last);
}
public int pop() {
// 마지막 노드의 값을 끄집어낸다.
int item = this.last.item;
// 마지막 노드를 한 칸 앞으로 이동한다.
this.last = this.last.next;
return item;
}
}
public static void main(String[] args) {
MyStack stack = new MyStack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
for (int i = 0; i < 5; i++) {
System.out.println(stack.pop());
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.