ํ์ต ํ ๋ด์ฉ๋ค
- ์ ํ๋ฌธ
- ๋ฐ๋ณต๋ฌธ
๊ณผ์ ์ ์ถ
- ๊ณผ์ 0. JUnit5 ํ์ต
- ๊ณผ์ 1. live-study ๋์ ๋ณด๋๋ฅผ ๋ง๋๋ ์ฝ๋ ์์ฑ
- ๊ณผ์ 2. LinkedList ๊ตฌํ
- ๊ณผ์ 3. Stack ๊ตฌํ
- ๊ณผ์ 4. ์์ ๋ง๋ ListNode๋ฅผ ์ด์ฉํด์ Stack ๊ตฌํ
- ๊ณผ์ 5. Queue๋ฅผ ๊ตฌํ (Optional)
- ๋ฐฐ์ด์ ์ฌ์ฉํด์ ํ๋ฒ
- ListNode๋ฅผ ์ฌ์ฉํด์ ํ๋ฒ.
์ ํ๋ฌธ
- if๋ฌธ
- switch๋ฌธ
if๋ฌธ
if (์กฐ๊ฑด1) {
// ์กฐ๊ฑด1์ด ์ฐธ์ผ ๋, ์คํ ( {} ์ค๊ดํธ ๋ธ๋ญ ์ ๋ถ๋ถ๋ง ์คํ!)
}
// if๋ฌธ์ด ์ข
๋ฃ ๋๊ณ ๋ ํ์ ์คํ (์กฐ๊ฑด1 ์ ๋ง์กฑ ์ํด๋ ์คํ๋๋ค.)
if - else ๋ฌธ
if(์กฐ๊ฑด1) {
// ์กฐ๊ฑด1์ด ์ฐธ์ผ ๋ ์คํ
} else {
// ์กฐ๊ฑด1์ด ๊ฑฐ์ง์ผ ๋ ์คํ
}
๋ค์ค if๋ฌธ
if(์กฐ๊ฑด1) {
// ์กฐ๊ฑด1์ด ์ฐธ์ผ ๋ ์คํ
} else if (์กฐ๊ฑด2) {
// ์กฐ๊ฑด2์ด ์ฐธ์ผ ๋ ์คํ
} else if (์กฐ๊ฑด3) {
// ์กฐ๊ฑด3์ด ์ฐธ์ผ ๋ ์คํ
} ..
..
else {
// ๋ชจ๋ ์กฐ๊ฑด์ ๋ง์กฑ ํ์ง ๋ชปํ ๊ฒฝ์ฐ ์คํ
}
switch ๋ฌธ
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
switch(input) {
case 0:
// input์ด 0์ผ ๊ฒฝ์ฐ์ ์คํ!
break; //case์๋ ํญ์ break๋ฅผ ๋ฃ์ด์ค์ ์ข
๋ฃ ์์ผ์ค์ผํจ!
case 1:
// input์ด 1์ผ ๊ฒฝ์ฐ์ ์คํ!
break;
case 2: case 3:
// input์ด 2, 3์ผ ๊ฒฝ์ฐ์ ์คํ!
break;
default :
// case ์ค์ ์ํ ๊ฐ์ผ ๊ฒฝ์ฐ์ ์คํ!
break;
}
๋ฐ๋ณต๋ฌธ
- while๋ฌธ
while(์กฐ๊ฑด) { // ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์ฐ์๋ง ์คํ, ๊ฑฐ์ง์ด๋ฉด ์ข
๋ฃ!
//์ฐธ์ผ ๋ ์คํ
..
}
์์ ์ฝ๋
int N = 1;
while(N < 10) { // N=10์ผ๋ ์ข
๋ฃ!
System.out.print(N + " ");
N++;
}
// ์คํ๊ฒฐ๊ณผ : 1 2 3 4 5 6 7 8 9
- do-while๋ฌธ
do { // ์กฐ๊ฑด ๊ฒ์ฌ ์ด์ ์ ๋จผ์ ์คํ!
System.out.println(N + " ");
N++;
} while( N < 10 ); // ์คํ์ด ๋๋๊ณ ์กฐ๊ฑด์ ๊ฒ์ฌ! N = 11์ผ ๋ ์ข
๋ฃ!
// ์คํ๊ฒฐ๊ณผ : 1 2 3 4 5 6 7 8 9 10
- for๋ฌธ
for๋ฌธ์ ์คํ ์์
int N = 10;
for(int i=N; i>=1; i--){ // N = 10 ๋ถํฐ~ 1๊น์ง ๋ฐ๋ณต!
System.out.print(i + " ");
}
// ์คํ๊ฒฐ๊ณผ : 10 9 8 7 6 5 4 3 2 1
๊ณผ์ 0. JUnit 5 ํ์ตํ์ธ์.
- ์ธํ ๋ฆฌJ, ์ดํด๋ฆฝ์ค, VS Code์์ JUnit 5๋ก ํ ์คํธ ์ฝ๋ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ์ต์ํด ์ง ๊ฒ.
- ์ด๋ฏธ JUnit ์๊ณ ๊ณ์ ๋ถ๋ค์ ๋ค๋ฅธ ๊ฒ ์๋ฌด๊ฑฐ๋!
JUnit5 ๋ถํฐ๋ ๋ชจ๋์ ์งํฉ์ผ๋ก ๊ตฌ์ฑ์ด ๋์ด์๋ค. (java8 ์ด์์ ๋ฒ์ ์ ํ์๋ก ํ์ง๋ง, ์ด์ JDK ๋ฒ์ ์ด์ฌ๋ ์ปดํ์ผ๋ ์ฝ๋๋ ํ ์คํธ ๊ฐ๋ฅ)
JUnit 5 = Junit Platform + Junit Jupiter + Junit Vintage ๋ก ๊ตฌ์ฑ ๋์ด ์๋ค.
- JUnit Platform
- Testing Frameworks๋ฅผ ๊ตฌ๋ํ๊ธฐ ์ํ ์ญํ ์ ํ๋ค.
- Testing Frameworks๋ฅผ ๊ฐ๋ฐํ๊ธฐ ์ํ TestEngine API๋ฅผ ์ ๊ณตํ๋ค.
- Console Launcher๋ฅผ ์ ๊ณตํ๋ค.
- JUnit Jupiter
- JUnit5 ์์ ํ ์คํธ ๋ฐ ํ์ฅ์ ์์ฑํ๊ธฐ ์ํ ์๋ก์ด ํ๋ก๊ทธ๋๋ฐ ๋ชจ๋ธ๊ณผ ํ์ฅ ๋ชจ๋ธ์ด๋ค. TestEngine์ ์ ๊ณตํ๋ค.
- JUnit Vintage
- JUnit3 ์ JUnit4 ๊ธฐ๋ฐ ํ ์คํธ๋ฅผ ์คํํ๊ธฐ ์ํ TestEngine์ ์ ๊ณตํ๋ค.
ํ ์คํธ๋ฅผ ์์ฑํ๋ ๋ฒ
ํ ์คํธ๋ฅผ ์งํํ๊ธฐ ์ํด Sample ํด๋์ค๋ฅผ ํ๋ ์์ฑํ๋ค. ๊ฐ๋จํ๊ฒ name๊ณผ price๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
Ctrl + shift + T ๋ฅผ ๋๋ฅด๋ฉด ํ ์คํธ๋ฅผ ์์ฑ ํ ์ ์๋ค.
ํ ์คํธ ์ด๋ฆ์ ...test ๋ผ๊ณ ์ด๋ฆ์ ์ค์ ํ๋๊ฒ ์ข๋ค.
์ด ํ ์คํธ์์๋ ๊ฐ๋จํ๊ฒ sample์๋ค๊ฐ "sample1"์ด๋ฆ์ ๋ฃ๊ณ , sample.getName()์ด sample1์ ๋ฆฌํดํ๋์ง ํ ์คํธ๋ฅผ ํ๋ ์ฝ๋์ด๋ค.
- ๊ธฐ๋ณธ ๋ฌธ๋ฒ์ Given , When , Then ์ธ๊ฐ์ง๋ก ๋๋๋ค.
- assertEqauls( expected , Actual ); ๊ธฐ๋๊ฐ๊ณผ ๋น๊ต ํ ์ค์ ๊ฐ์ ๋ฃ์ด์ค๋ค.
ํ ์คํธ๋ฅผ ์คํํด์ฃผ๋ฉด ?
์คํ์ด ์์ฃผ ์ ๋๋๊ฒ์ ์ ์ ์๋ค.
๋ง์ฝ sample2๋ผ๊ณ ์์ธกํ๊ณ ํ ์คํธ๋ฅผ ๋๋ฆฌ๊ฒ ๋๋ฉด?
๊ธฐ๋ ๊ฐ์ sample2 ์ธ๋ฐ , ์ค์ ๊ฐ์ sample1์ด ๋ฐํ๋์ด์ ํ ์คํธ๊ฐ ๊นจ์ง๊ฒ ๋๋ค.
@DisplayName : ํ ์คํธ ๋ฉ์๋ ์ด๋ฆ ์ค์ ํ๋ ๋ฒ
- @DisplayName( String s ) ๋ฅผ ์ค์ ํด์ฃผ๋ฉด, ์๋์ ๊ฐ์ด ํ ์คํธ ์คํํ๋ฉด ๋ด๊ฐ ์ค์ ํ ์ด๋ฆ์ด ๋์ค๊ฒ ๋๋ค. ํ ์คํธ ๊ฐ์๊ฐ ์์ฒญ ๋ง์ ๊ฒฝ์ฐ์ ์ด ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ฉด ํ ์คํธ๋ฅผ ๊ตฌ๋ณํ ๋ ํธ๋ฆฌ ํ ๊ฒ์ด๋ค.
์์ธํ ๋ด์ฉ์ Junit - JUnit5 ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ ์ ์ฐธ๊ณ ํ์ธ์.
๊ณผ์ 1. live-study ๋์ ๋ณด๋๋ฅผ ๋ง๋๋ ์ฝ๋๋ฅผ ์์ฑํ์ธ์.
- ๊นํ ์ด์ 1๋ฒ๋ถํฐ 18๋ฒ๊น์ง ๋๊ธ์ ์ํํ๋ฉฐ ๋๊ธ์ ๋จ๊ธด ์ฌ์ฉ์๋ฅผ ์ฒดํฌ ํ ๊ฒ.
- ์ฐธ์ฌ์จ์ ๊ณ์ฐํ์ธ์. ์ด 18ํ์ ์ค์ ๋ช %๋ฅผ ์ฐธ์ฌํ๋์ง ์์ซ์ ๋์๋ฆฌ๊ฐ์ง ๋ณด์ฌ์ค ๊ฒ.
- Github ์๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ฉด ํธ๋ฆฌํฉ๋๋ค.
- ๊นํ API๋ฅผ ์ต๋ช ์ผ๋ก ํธ์ถํ๋๋ฐ ์ ํ์ด ์๊ธฐ ๋๋ฌธ์ ๋ณธ์ธ์ ๊นํ ํ๋ก์ ํธ์ ์ด์๋ฅผ ๋ง๋ค๊ณ ํ ์คํธ๋ฅผ ํ์๋ฉด ๋ ์์ฃผ ํ ์คํธํ ์ ์์ต๋๋ค.
์ฝ๋
package study;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHIssueComment;
import org.kohsuke.github.GHRepository;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
public class Main {
static List<Participant> participants;
private static GHRepository init() throws IOException {
participants = new ArrayList<>();
App app = new App();
GHRepository repository = app.getRepository();
return repository;
}
// list์ ํด๋น ์ ์ ์ด๋ฆ์ด ์๋์ง ์ฒดํฌ (return boolean)
public static Participant isContains(String username) {
for (Participant participant : participants) {
if (participant.getUserName().equals(username)) {
return participant;
}
}
return null;
}
public static void check_HomeWork(GHRepository repository) throws IOException {
for (int i = 1; i <= 18; i++) {
GHIssue issue = repository.getIssue(i);
List<GHIssueComment> comments = issue.getComments();
for (GHIssueComment comment : comments) {
String userName = comment.getUserName();
Participant findUser = isContains(userName);
if (findUser != null) { // ๋ง์ฝ list์ ํด๋น ์ ์ ๊ฐ ์กด์ฌํ๋ค?
findUser.todayHomeWorkSuccess(i);
}else{
Participant p = new Participant(userName);
p.todayHomeWorkSuccess(i);
participants.add(p);
}
}
}
}
public static void print_board() throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write("| ์ฐธ์ฌ์ | 1์ฃผ์ฐจ | 2์ฃผ์ฐจ | 3์ฃผ์ฐจ | 4์ฃผ์ฐจ | 5์ฃผ์ฐจ | 6์ฃผ์ฐจ | 7์ฃผ์ฐจ | 8์ฃผ์ฐจ | 9์ฃผ์ฐจ | 10์ฃผ์ฐจ | 11์ฃผ์ฐจ | 12์ฃผ์ฐจ | 13์ฃผ์ฐจ | 14์ฃผ์ฐจ | 15์ฃผ์ฐจ | 16์ฃผ์ฐจ | 17์ฃผ์ฐจ | 18์ฃผ์ฐจ | ์ฐธ์์จ |\n");
bw.write("| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |\n");
for (Participant participant : participants) {
bw.write("| " + participant.getUserName() + " |");
int success = participant.getSuccess();
for (int i = 1; i <= 18; i++) {
if (participant.is_HW_Done(i)) {
bw.write(" โ
|");
}else{
bw.write(" |");
}
}
double value = (success / (18.0)) * 100;
bw.write(" " + (Math.round(value * 100) / 100.0) + "% |\n");
}
bw.flush(); bw.close();
}
public static void main(String[] args) throws IOException {
GHRepository repository = init();
check_HomeWork(repository); // ๊ณผ์ ํ๋์ง ์ฒดํฌ!
print_board(); // ์ถ๋ ฅ
}
}
๋๋จธ์ง ์ฝ๋๋ค์ Github API ์ฌ์ฉ๋ฒ์ ์์ธํ๊ฒ ์ ์ด๋์์ต๋๋ค~
์ถ๋ ฅ ํ๋ฉด
| ์ฐธ์ฌ์ | 1์ฃผ์ฐจ | 2์ฃผ์ฐจ | 3์ฃผ์ฐจ | 4์ฃผ์ฐจ | 5์ฃผ์ฐจ | 6์ฃผ์ฐจ | 7์ฃผ์ฐจ | 8์ฃผ์ฐจ | 9์ฃผ์ฐจ | 10์ฃผ์ฐจ | 11์ฃผ์ฐจ | 12์ฃผ์ฐจ | 13์ฃผ์ฐจ | 14์ฃผ์ฐจ | 15์ฃผ์ฐจ | 16์ฃผ์ฐจ | 17์ฃผ์ฐจ | 18์ฃผ์ฐจ | ์ฐธ์์จ |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| nimkoes | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| JeongJin984 | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| league3236 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| sowhat9293 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| WonYong-Jang | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| Chohongjae | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| yeo311 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| gblee87 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| kksb0831 | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| sangw0804 | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| Jul-liet | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| idiot2222 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| JadenKim940105 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| addadda15 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| JsKim4 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| binghe819 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| cbw1030 | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| pond1029 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| lee-maru | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| kingsubin | โ
| | โ
| โ
| | | | | | | | | | | | | | | 16.67% |
| ksundong | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| dudqls5271 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| Yadon079 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| tbnsok40 | โ
| โ
| | โ
| | | | | | | | | | | | | | | 16.67% |
| hwonny | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| hyeonic | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| twowinsh87 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| Lob-dev | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| asqwklop12 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| DevelopJKong | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| Lee-jaeyong | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| fpdjsns | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| hanull | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| KyungJae-Jang | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| sojintjdals | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| fkfkfk9 | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| ggomjae | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| hyenny | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| zilzu4165 | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| yeGenieee | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| abcdsds | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| jaehyunup | โ
| | | โ
| | | | | | | | | | | | | | | 11.11% |
| wdEffort | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| dmstjd1024 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| dev-jaekkim | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| m3252 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| hongminpark | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| HyangKeunChoi | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| id6827 | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| ahyz0569 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| giyeon95 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| memoregoing | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| batboy118 | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| jaeyeon93 | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| whiteship | โ
| โ
| | | | | | | | | | | | | | | | | 66.67% |
| roeniss | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| plzprayme | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| mongzza | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| sigriswil | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| JoongSeokD | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| sejongdeveloper | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| KilJaeeun | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| JuHyun419 | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| YuSeungmo | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| damho1104 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| junhok82 | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| jjone36 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| dacapolife87 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| rlatmd0829 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| sskim91 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| ssayebee | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| Jangilkyu | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| kyu9 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| ParkIlHoon | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| Youngerjesus | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| good-influence | โ
| | โ
| | | | | | | | | | | | | | | | 11.11% |
| accidentlywoo | โ
| | โ
| | | | | | | | | | | | | | | | 11.11% |
| yky03 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| catsbi | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| jwsims1995 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| jessi68 | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| angelatto | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| gtpe | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| hyngsk | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| sjhello | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| ChoiGiSung | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 27.78% |
| Yo0oN | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| Rebwon | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| HyeonWuJeon | โ
| โ
| | โ
| | | | | | | | | | | | | | | 16.67% |
| kjw217 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| LeeWoooo | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| star1606 | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| hypernova1 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| 0417taehyun | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| gcha-kim | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| GGob2 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| kongduboo | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| numuduwer | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| etff | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| honux77 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| jymaeng95 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| conyconydev | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| thisisyoungbin | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| jongnan | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| loop-study | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| Sungjun-HQ | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| ejxzhn22 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| iseunghan | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| jikimee64 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| ohjoohyung | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| 1031nice | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| cold-pumpkin | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| koreas9408 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| jongyeans | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 27.78% |
| yskkkkkk | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| haemin-jeong | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| ufonetcom | โ
| โ
| โ
| | | | | | | | | | | | | | | | 22.22% |
| 2yeseul | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| EdwardJae | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| sowjd | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| jigmini | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| devksh930 | โ
| โ
| | โ
| | | | | | | | | | | | | | | 16.67% |
| SooJungDev | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| oktopman | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| YeseulDo | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| kopokero | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| SeungWoo-Oh | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| goodzzong | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| Dubidubab | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| uHan2 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| keunyop | โ
| | โ
| | | | | | | | | | | | | | | | 11.11% |
| gurumee92 | โ
| | โ
| โ
| | | | | | | | | | | | | | | 16.67% |
| zhaoSeo | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| limyeonsoo | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| jaewon0913 | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| nekisse-lee | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| ldw1220 | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| rshak8912 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| surfing2003 | โ
| | | โ
| | | | | | | | | | | | | | | 11.11% |
| yks095 | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| pka0220z | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| MoonHKLee | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| jiwoo-choi | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| sweetchinmusic | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| younwony | โ
| | โ
| | | | | | | | | | | | | | | | 11.11% |
| sujl95 | โ
| โ
| | โ
| | | | | | | | | | | | | | | 16.67% |
| devvip | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| Junhan0037 | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| DDOEUN | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| monkeyDugi | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| chaechae0322 | โ
| โ
| | โ
| | | | | | | | | | | | | | | 16.67% |
| sungpillhong | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| inhalin | โ
| | โ
| โ
| | | | | | | | | | | | | | | 16.67% |
| 372dev | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| doyoung0205 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| seovalue | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| manOfBackend | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| sky7th | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| geneaky | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| Jason-time | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| Kim-JunHyeong | โ
| | โ
| | | | | | | | | | | | | | | | 11.11% |
| choiyoungkwon12 | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| hong918kr | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| pej4303 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| GunnwooKim | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| s0w0s | โ
| | โ
| | | | | | | | | | | | | | | | 11.11% |
| 9m1i9n1 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| ysmiler | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| Ryureka | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| gintire | โ
| | โ
| | | | | | | | | | | | | | | | 11.11% |
| garlickim | โ
| โ
| โ
| โ
| | | | | | | | | | | | | | | 22.22% |
| CODEMCD | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| kimseungki94 | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| devShLee7017 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| riyenas0925 | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| kys4548 | โ
| | โ
| โ
| | | | | | | | | | | | | | | 16.67% |
| lee-jemu | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| kyunyan | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| ghwann | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| JOYB28 | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| kdm8939 | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| kimmy100b | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| coldhoon | โ
| โ
| | | | | | | | | | | | | | | | | 11.11% |
| elon09 | โ
| | โ
| | | | | | | | | | | | | | | | 11.11% |
| jaxx2001 | โ
| | โ
| | | | | | | | | | | | | | | | 11.11% |
| sangwoobae | โ
| | โ
| โ
| | | | | | | | | | | | | | | 16.67% |
| ohhhmycode | โ
| | โ
| | | | | | | | | | | | | | | | 11.11% |
| sunnynote | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| BaeJi77 | โ
| | | โ
| | | | | | | | | | | | | | | 11.11% |
| yallyyally | โ
| | | | | | | | | | | | | | | | | | 5.56% |
| msmn1729 | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| mokaim | โ
| | โ
| | | | | | | | | | | | | | | | 11.11% |
| JoosJuliet | โ
| โ
| โ
| | | | | | | | | | | | | | | | 16.67% |
| ehdrhelr | | โ
| โ
| โ
| | | | | | | | | | | | | | | 16.67% |
| ByungJun25 | | โ
| โ
| โ
| | | | | | | | | | | | | | | 16.67% |
| Youngjin-KimY | | โ
| โ
| | | | | | | | | | | | | | | | 11.11% |
| Gomding | | โ
| โ
| โ
| | | | | | | | | | | | | | | 16.67% |
| castleCircle | | โ
| | | | | | | | | | | | | | | | | 5.56% |
| dongyeon94 | | โ
| โ
| โ
| | | | | | | | | | | | | | | 16.67% |
| ShimSeoungChul | | โ
| โ
| | | | | | | | | | | | | | | | 11.11% |
| DevRyu | | โ
| โ
| | | | | | | | | | | | | | | | 11.11% |
| lleezz | | โ
| | | | | | | | | | | | | | | | | 5.56% |
| sinchang1 | | โ
| โ
| | | | | | | | | | | | | | | | 11.11% |
| tocgic | | โ
| โ
| | | | | | | | | | | | | | | | 11.11% |
| TaeYing | | | โ
| | | | | | | | | | | | | | | | 5.56% |
| metorg | | | โ
| | | | | | | | | | | | | | | | 5.56% |
| redbean88 | | | โ
| โ
| | | | | | | | | | | | | | | 11.11% |
| jeeneee | | | โ
| โ
| | | | | | | | | | | | | | | 11.11% |
| tjdqls1200 | | | โ
| | | | | | | | | | | | | | | | 5.56% |
| cs7998 | | | โ
| | | | | | | | | | | | | | | | 5.56% |
| Ahnyezi | | | | โ
| | | | | | | | | | | | | | | 5.56% |
| SnowisTargaryen | | | | โ
| | | | | | | | | | | | | | | 5.56% |
| KJJ924 | | | | โ
| | | | | | | | | | | | | | | 5.56% |
| yhxkit | | | | โ
| | | | | | | | | | | | | | | 5.56% |
| JJongSue | | | | โ
| | | | | | | | | | | | | | | 5.56% |
| youngsunshin94 | | | | โ
| | | | | | | | | | | | | | | 5.56% |
| sunho-lee | | | | โ
| | | | | | | | | | | | | | | 5.56% |
| LeeJeongSeok | | | | โ
| | | | | | | | | | | | | | | 5.56% |
| rudus1012 | | | | โ
| | | | | | | | | | | | | | | 5.56% |
| dkyou7 | | | | โ
| | | | | | | | | | | | | | | 5.56% |
| ku-kim | | | | โ
| | | | | | | | | | | | | | | 5.56% |
| glowing713 | | | | โ
| | | | | | | | | | | | | | | 5.56% |
๊ณผ์ 2. LinkedList๋ฅผ ๊ตฌํํ์ธ์.
- LinkedList์ ๋ํด ๊ณต๋ถํ์ธ์.
- ์ ์๋ฅผ ์ ์ฅํ๋ ListNode ํด๋์ค๋ฅผ ๊ตฌํํ์ธ์.
- ListNode add(ListNode head, ListNode nodeToAdd, int position)๋ฅผ ๊ตฌํํ์ธ์.
- ListNode remove(ListNode head, int positionToRemove)๋ฅผ ๊ตฌํํ์ธ์.
- boolean contains(ListNode head, ListNode nodeTocheck)๋ฅผ ๊ตฌํํ์ธ์.
iseunghan.tistory.com/232?category=912560
LinkedList ์ฝ๋
package demo;
public class LinkedList {
private int size = 0; // ์ ์ฒด์ ์ธ ์ฌ์ด์ฆ
private ListNode head;
private ListNode tail; // ๋น ๋ฅธ ์ ๊ทผ์ ์ํด tail์ ๋ง๋ค์๋ค.
public static class ListNode {
private int data; // ํ์ฌ ๋
ธ๋์ ๋ฐ์ดํฐ
private ListNode next; // next ๋
ธ๋๋ฅผ ์ ์ฅ ํ ๋ณ์
private int position; // ํ์ฌ ๋
ธ๋์ ์์น(index)
public ListNode(int data) {
this.data = data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public ListNode getNext() {
return next;
}
public void setNext(ListNode next) {
this.next = next;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
//=======================================================================
// add Area
//=======================================================================
public ListNode add(ListNode head, ListNode nodeToAdd, int position) {
// TODO 1. addFirst head์ ์ถ๊ฐ
// 2. addLast tail์ ์ถ๊ฐ
// 3. addPosition ์ค๊ฐ์ ์ถ๊ฐ
if (position == 0) {
addFirst(nodeToAdd);
} else if (position == size) {
addLast(nodeToAdd);
} else {
addPosition(nodeToAdd, position);
}
return nodeToAdd;
}
public void addFirst(ListNode nodeToAdd) {
// TODO 1. head null ? -> list.size = 0
// 2. head not null ? -> change head
if (this.head == null) {
this.tail = nodeToAdd; // list์ 1๊ฐ๋ฐ์ ์๋ค๋ณด๋๊น head์ tail ์ญํ ๊น์ง ํ๋ค.
}else {
if (head == tail) {
nodeToAdd.next = this.head;
this.head.position += 1;
}
nodeToAdd.next = this.head;
increasePosition(this.head); // head๋ถํฐ ๋ง์ง๋ง ๊น์ง position์ ํ๋์ฉ ์ฆ๊ฐ์์ผ์ค๋ค.
}
this.head = nodeToAdd;
nodeToAdd.position = 0;
size++;
}
public void addLast(ListNode nodeToAdd) {
this.tail.next = nodeToAdd;
this.tail = nodeToAdd;
this.tail.position = size;
size++;
}
public void addPosition(ListNode nodeToAdd, int position) {
ListNode preNode = findByPosition(position - 1);
increasePosition(preNode.next); // ์ถ๊ฐํ ์์น์ ์์๋ ๋
ธ๋๋ถํฐ ๋ง์ง๋ง ๋
ธ๋๊น์ง pos+1
nodeToAdd.next = preNode.next;
preNode.next = nodeToAdd;
nodeToAdd.position = position;
size++;
}
//=======================================================================
// remove Area
//=======================================================================
public ListNode remove(ListNode head, int positionToRemove) {
ListNode temp = head;
if (positionToRemove == 0) {
this.head = temp.next; // head๋ฅผ ๋ฐ๊ฟ์ค๋ค.
temp.next = null; // ๊ธฐ์กด head๋ ์ด์ ์ญ์
temp = this.head; // ์์๋ก ์ฌ์ฉํ temp ๋ค์ ์ด๊ธฐํ
for (int i = 1; i < size; i++) {
temp.position -= 1;
temp = temp.next;
}
size--;
return temp;
} else if (positionToRemove == (size - 1)) {
ListNode preNode = findByPosition(tail.position - 1);
this.tail = preNode;
tail.next = null;
size--;
return tail;
}
ListNode preNode = findByPosition(positionToRemove - 1);
ListNode cur = preNode.next;
preNode.next = cur.next;
cur.next = null;
cur = preNode.next; // for๋ฌธ์์ ์ฌ์ฉํ ์์ ๋ณ์ cur ์ฌํ์ฉ
for (int i = preNode.next.position; i < size; i++) {
cur.position -= 1;
cur = cur.next;
}
size--;
return cur;
}
/**
* find by position
*
* @param position
* @return ListNode
*/
public ListNode findByPosition(int position) {
ListNode temp = head; // head๋ถํฐ ํ์
while (temp.position != position) {
temp = temp.next;
if (temp == null) {
System.out.println("NullPointerException");
return null;
}
}
return temp;
}
/**
* start ๋ถํฐ ๋ง์ง๋ง ๋
ธ๋๊น์ง position์ 1์ฉ ์ฆ๊ฐ์์ผ์ค๋ค.
* @param start
*/
public void increasePosition(ListNode start) {
ListNode temp = start;
for (int i = start.position; i < size; i++) {
temp.position += 1;
temp = temp.next;
}
}
/**
* is nodeTocheck contains to list?
* @param head
* @param nodeTocheck
* @return boolean
*/
public boolean contains(ListNode head, ListNode nodeTocheck) {
ListNode temp = head;
for (int i = 0; i < size; i++) {
if (nodeTocheck.equals(temp)) {
return true;
}
temp = temp.next;
}
return false;
}
/**
* print all list
*/
public void print() {
ListNode temp = head;
System.out.print("[ ");
for (int i = 0; i < size; i++) {
System.out.print(temp.data + "(" + temp.position + ")" + " ");
temp = temp.next;
}
System.out.print("]");
}
public int getSize() {
return size;
}
public ListNode getHead() {
return head;
}
public ListNode getTail() {
return tail;
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
for (int i = 0; i <= 10; i++) {
list.add(list.head, new ListNode(i), i);
}
System.out.println("head : " + list.head.data);
list.print(); System.out.println();
// [ 0(0) 1(1) 2(2) 3(3) 4(4) 5(5) 6(6) 7(7) 8(8) 9(9) 10(10) ]
list.add(list.head, new ListNode(400), 5);
list.print(); System.out.println();
// [ 0(0) 1(1) 2(2) 3(3) 4(4) 400(5) 5(6) 6(7) 7(8) 8(9) 9(10) 10(11) ]
list.remove(list.head, 5);
list.remove(list.head, 5);
list.remove(list.head, 5);
list.remove(list.head, 5);
list.remove(list.head, 5);
list.remove(list.head, 0);
list.print(); System.out.println();
// [ 1(0) 2(1) 3(2) 4(3) 9(4) 10(5) ]
ListNode add = new ListNode(200);
list.add(list.head, add, 0);
System.out.println(list.contains(list.head, new ListNode(1000))); // false
System.out.println(list.contains(list.head, add)); // true
}
}
Test ์ฝ๋
package demo;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static demo.LinkedList.ListNode;
import static org.junit.jupiter.api.Assertions.*;
class LinkedListTest {
@Test
@DisplayName("addFirst ํ
์คํธ")
void addFirstTest() {
// Given
LinkedList list = new LinkedList();
ListNode node = new ListNode(10);
ListNode node2 = new ListNode(20);
// When
ListNode add = list.add(list.getHead(), node, 0); // position:1
ListNode add1 = list.add(list.getHead(), node2, 0); // position:0
// Then
assertEquals(10, add.getData());
assertEquals(1, add.getPosition());
assertEquals(20, add1.getData());
assertEquals(0, add1.getPosition());
assertEquals(10, list.getTail().getData()); // tail์ด ์ ๋ค์ด๊ฐ๋์ง?
}
@Test
@DisplayName("addLast ํ
์คํธ")
void addLastTest() {
// Given
LinkedList list = new LinkedList();
ListNode node = new ListNode(10);
ListNode node1 = new ListNode(20);
list.add(list.getHead(), node, 0); // [10] head == tail
// When
list.add(list.getHead(), node1, 1);
// Then
assertEquals(node, list.getHead());
assertEquals(0, list.getHead().getPosition());
assertEquals(10, list.getHead().getData());
assertEquals(node1, list.getTail());
assertEquals(1, list.getTail().getPosition());
assertEquals(20, list.getTail().getData());
}
@Test
@DisplayName("addPosition ํ
์คํธ")
void addPositionTest() {
// Given
LinkedList list = new LinkedList();
ListNode node = new ListNode(10);
ListNode node1 = new ListNode(20);
ListNode node2 = new ListNode(30);
list.add(list.getHead(), node, 0);
list.add(list.getHead(), node1, 1);
// When
list.add(list.getHead(), node2, 1);
// Then
assertEquals(node1, list.getTail());
assertEquals(2, list.getTail().getPosition());
assertEquals(20, list.getTail().getData());
assertEquals(30, list.findByPosition(1).getData());
assertEquals(1, list.findByPosition(1).getPosition());
}
@Test
@DisplayName("contains ํ
์คํธ")
void containsTest() {
// Given
LinkedList list = new LinkedList();
ListNode node = new ListNode(10);
ListNode node1 = new ListNode(20);
list.add(list.getHead(), node, 0); // node ๋ง ์ถ๊ฐํจ
// When & Then
assertTrue(list.contains(list.getHead(),node));
assertFalse(list.contains(list.getHead(),node1));
}
}
๊ณผ์ 3. Stack์ ๊ตฌํํ์ธ์.
- int ๋ฐฐ์ด์ ์ฌ์ฉํด์ ์ ์๋ฅผ ์ ์ฅํ๋ Stack์ ๊ตฌํํ์ธ์.
- void push(int data)๋ฅผ ๊ตฌํํ์ธ์.
- int pop()์ ๊ตฌํํ์ธ์.
Stack ์ฝ๋
public class Stack {
int size = 0;
Element tail;
public class Element {
Element down;
int data;
public Element(int data) {
this.data = data;
}
}
public void push(int data) {
Element temp = new Element(data);
if (size == 0) {
this.tail = temp; // ๋น์ด์๋ ์ํ๋ผ์ ๋ฐ๋ก tail๋ก ์ค์
}else{ // ๋ฐ์ดํฐ๊ฐ ์์ ๋
temp.down = tail;
this.tail = temp;
}
size++;
}
public int pop() {
Element pop = tail;
if (size > 0) {
this.tail = tail.down;
} else {
throw new NullPointerException("Size๋ฅผ ํ์ธ ํด์ฃผ์ธ์");
}
pop.down = null;
size--;
return pop.data;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
if (size == 0) {
return "| |" + "\n------";
}
Element temp = tail;
while(temp != null){
sb.append("| " + temp.data +" |" + "\n");
temp = temp.down;
}
sb.append("------");
String result = sb.toString();
return result;
}
public static void main(String[] args) {
Stack stack = new Stack();
System.out.println("=== push ===");
stack.push(10);
stack.push(10);
stack.push(10);
System.out.println("=== pop ====");
System.out.println(stack.pop() + " size : " + stack.size);
System.out.println(stack);
System.out.println(stack.pop() + " size : " + stack.size);
System.out.println(stack);
System.out.println(stack.pop() + " size : " + stack.size);
System.out.println(stack);
}
}
๊ณผ์ 4. ์์ ๋ง๋ ListNode๋ฅผ ์ฌ์ฉํด์ Stack์ ๊ตฌํํ์ธ์.
- ListNode head๋ฅผ ๊ฐ์ง๊ณ ์๋ ListNodeStack ํด๋์ค๋ฅผ ๊ตฌํํ์ธ์.
- void push(int data)๋ฅผ ๊ตฌํํ์ธ์.
- int pop()์ ๊ตฌํํ์ธ์.
public class ListNodeStack {
ListNode head;
int size = 0;
public class ListNode {
ListNode down;
int data;
public ListNode(int data) {
this.data = data;
}
}
public void push(int data) {
ListNode add = new ListNode(data);
if (this.head == null) {
this.head = add;
}else{
add.down = this.head;
this.head = add;
}
size++;
}
public int pop() {
ListNode temp = this.head;
if (size > 1) {
this.head = temp.down;
size--;
return temp.data;
} // else if (size == 1)
this.head = null;
size--;
return temp.data;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
ListNode temp = this.head;
for (int i = 0; i < size; i++) {
sb.append("| " + temp.data + " |\n");
temp = temp.down;
}
if(size != 0) {
sb.append("------");
}else{
sb.append("[ ]");
}
String result = sb.toString();
return result;
}
public static void main(String[] args) {
ListNodeStack lns = new ListNodeStack();
lns.push(30);
lns.push(20);
lns.push(10);
System.out.println("head : " + lns.head.data); // head : 10
lns.pop(); // 10 -> pop
System.out.println(lns); // 20, 30
System.out.println("---------------------------------------");
System.out.println("head : " + lns.head.data); // head : 20
lns.pop(); // 20 -> pop
System.out.println(lns); // 30
System.out.println("---------------------------------------");
System.out.println("head : " + lns.head.data); // head : 30
lns.pop(); // 30 -> pop
System.out.println(lns); // [ ]
System.out.println("---------------------------------------");
System.out.println("head : " + lns.head); // head : null
}
}
(optional) ๊ณผ์ 5. Queue๋ฅผ ๊ตฌํํ์ธ์.
- ๋ฐฐ์ด์ ์ฌ์ฉํด์ ํ๋ฒ
- ListNode๋ฅผ ์ฌ์ฉํด์ ํ๋ฒ.
๋ฐฐ์ด์ ์ฌ์ฉํ Queue
public class Queue {
int[] queue;
int first = 0;
int index = 0;
int size = 0;
public Queue(int size) {
queue = new int[size];
this.size = size;
System.out.println("--- System : Queue size (" + size + ") ๊ฐ ์์ฑ ๋์์ต๋๋ค. ---");
}
public void add(int data) {
queue[index++] = data;
System.out.println("ADD : " + data);
}
public int pop() {
if (queue[first] == 0) {
throw new NullPointerException("queue๊ฐ ๋น์ด์์ต๋๋ค.");
}
System.out.println("Pop : " + queue[first]);
return queue[first++];
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Queue์ ๋ชจ๋ ์์๋ฅผ ์ถ๋ ฅํฉ๋๋ค.\n");
sb.append("--------------------------\n");
for (int i = first; i < size; i++) {
if (queue[i] != 0) {
sb.append(queue[i] + " ");
}else{
break;
}
}
sb.append("\n--------------------------");
String result = sb.toString();
return result;
}
public static void main(String[] args) {
Queue q1 = new Queue(100);
q1.add(1);
q1.add(2);
q1.add(3);
q1.pop();
q1.pop();
q1.pop();
q1.pop();
System.out.println(q1);
}
}
ListNode๋ฅผ ์ฌ์ฉํ Queue
public class Queue_ListNode {
int size;
ListNode top;
public class ListNode {
ListNode pre;
int data;
public ListNode(int data) {
this.data = data;
}
}
public void push(int data) {
ListNode node = new ListNode(data);
if (top == null) {
top = node;
}else{
ListNode temp = top;
for (int i = 0; i < size-1; i++) {
temp = temp.pre;
}
temp.pre = node;
}
size++;
}
public int pop() {
if (size > 0) {
ListNode temp = top;
top = temp.pre;
temp.pre = null;
return temp.data;
}
throw new NullPointerException("size : 0");
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("์คํ์ ๋ชจ๋ ์์๋ฅผ ์ถ๋ ฅํฉ๋๋ค.\n");
ListNode temp = top;
if (temp == null) {
sb.append("[ ]");
}
while (temp != null) {
sb.append(temp.data + " ");
temp = temp.pre;
}
String result = sb.toString();
return result;
}
public static void main(String[] args) {
Stack_ListNode sl = new Stack_ListNode();
sl.push(10);
sl.push(20);
sl.push(30);
sl.pop();
sl.pop();
sl.pop();
System.out.println(sl); // ์คํ์ ๋ชจ๋ ์์๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
// [ ]
}
}
References