๐๋ฆฌ์คํธ(List) ๋?
์์๋ฅผ ๊ฐ์ง๋๋ก ๋ฐ์ดํฐ๋ฅผ ๋์ดํ ๊ฒ
๋ฆฌ์คํธ์์๋ ๊ฐ๊ฐ์ ์์๋ฅผ ๋
ธ๋๋ผ๊ณ ํ๋ค.
๋
ธ๋๋ ๋ฐ์ดํฐ์ ๋ค์ ๋
ธ๋์ ์์น๋ฅผ ์ ์ฅํ๋ค.
๐๋จ์ผ ์ฐ๊ฒฐ๋ฆฌ์คํธ(List) ์ ๊ตฌํ
๐พ ํด๋์ค ๊ตฌ์กฐ
1. ๋
ธ๋ ํด๋์ค
๋ฐ์ดํฐ์ ๋
ธ๋๋ฅผ ์ ์ฅํ ์ ์๋ ํด๋์ค
package list;
public class Node {
Integer data;
Node next;
public Node(Integer data, Node next) {
this.data = data;
this.next = next;
}
}
2. ๋ฆฌ์คํธ ํด๋์ค
head : ๋งจ ์ฒ์ ๋
ธ๋๋ฅผ ์ ์ฅํ๋ค.
๋ฆฌ์คํธ ํด๋์ค์ ๋ฉ์๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
๐พ์์ฑ์
head์ null์ ์ ์ฅํ๋ค.
public LinkedList() {
this.head = null;
}
๐พ insertFirst(x); ๋งจ ์ฒ์์ ๋ฐ์ดํฐ x๋ฅผ ์ถ๊ฐ
๋ฐ์ดํฐ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ ๋ฐ๋๋ค.
๋ ธ๋ ๊ฐ์ฒด ์์ฑ(๋ ธ๋์ next์๋ ๊ธฐ์กด head๋ฅผ ์ ์ฅ)
head์ ๋ฐฉ๊ธ ์์ฑํ ๋ ธ๋๋ฅผ ์ ์ฅ
void insertFirst(int x){
Node node = new Node(x, head);
head = node;
}
๐พ insertLast(x); ๋งจ ๋ง์ง๋ง์ ๋ฐ์ดํฐ x๋ฅผ ์ถ๊ฐ
๋ฐ์ดํฐ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ ๋ฐ๋๋ค.
head๊ฐ null์ด๋ฉด insertFirst๋ฅผ ์คํํ๋ค.
๋ ธ๋ ๊ฐ์ฒด ์์ฑ(๋ ธ๋์ next์๋ null ์ ์ฅ)
๋ง์ง๋ง ๋ ธ๋์๋ current์ next์๋ ๋ฐฉ๊ธ ์์ฑํ ๋ ธ๋๋ฅผ ์ ์ฅ
void insertLast(int x){
if(head == null) this.insertFirst(x);
else {
Node current = head;
while (current.next != null) {
current = current.next;
}
Node node = new Node(x, null);
current.next = node;
}
}
๐พ inserIndex(index, x); ์ํ๋ index์ ๋ฐ์ดํฐ x๋ฅผ ์ถ๊ฐ
๋ฐ์ดํฐ๋ฅผ ์ฝ์ ํ ์ธ๋ฑ์ค์ ๋ฐ์ดํฐ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ ๋ฐ๋๋ค.
๋ ธ๋ ๊ฐ์ฒด ์์ฑ(๋ ธ๋์ next์๋ ์ํ๋ index์ ๋ ธ๋๋ฅผ ์ ์ฅ)
์ํ๋ index์ ์ ๋ ธ๋์ธ current์ next์๋ ๋ฐฉ๊ธ ์์ฑํ ๋ ธ๋๋ฅผ ์ ์ฅ
void insertIndex(int index, int x){
Node current = head;
//head๊ฐ ์ธ๋ฑ์ค 0
for (int i = 1; i < index; i++) {
current = current.next;
}
Node node = new Node(x, current.next);
current.next = node;
}
๐พ display(); ํ์ฌ ๋ ธ๋๋ค์ ๋ชจ๋ ์ถ๋ ฅ
void display(){
Node current = head;
while(true){
System.out.printf("[%d] ",current.data);
if(current.next == null) break;
current = current.next;
}
System.out.println();
}
๐พ removeFirst(); ๋งจ ์ฒ์ ๋ ธ๋๋ฅผ ์ญ์
๐พremoveLast(); ๋งจ ๋ง์ง๋ง ๋ ธ๋๋ฅผ ์ญ์
๐พ removeIndex(index); ์ํ๋ ์ธ๋ฑ์ค์ ๋ ธ๋๋ฅผ ์ญ์
๐์ ์ฒด ์ฝ๋
LinkedList.java
package list;
public class LinkedList {
Node head;
public LinkedList() {
this.head = null;
}
void insertFirst(int x){
Node node = new Node(x, head);
head = node;
}
void insertLast(int x){
if(head == null) this.insertFirst(x);
else {
Node current = head;
while (current.next != null) {
current = current.next;
}
Node node = new Node(x, null);
current.next = node;
}
}
void insertIndex(int index, int x){
Node current = head;
//head๊ฐ ์ธ๋ฑ์ค 0
for (int i = 1; i < index; i++) {
current = current.next;
}
Node node = new Node(x, current.next);
current.next = node;
}
void display(){
Node current = head;
while(true){
System.out.printf("[%d] ",current.data);
if(current.next == null) break;
current = current.next;
}
System.out.println();
}
}
LinkedListMain.java
package list;
public class LinkedListMain {
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.insertFirst(10);
ll.insertFirst(20);
ll.insertFirst(30);
ll.display();
ll.insertLast(40);
ll.insertLast(50);
ll.display();
ll.insertIndex(3, 60);
ll.display();
}
}
'โก๐ฉโ๐ป โก > ยบ Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋ฃ๊ตฌ์กฐ] ์ด์ง ํ์ ํธ๋ฆฌ ๊ตฌํ | Java (0) | 2023.12.09 |
---|---|
[์๊ณ ๋ฆฌ์ฆ] ์ต๋จ ๊ฑฐ๋ฆฌ | A-Star ์๊ณ ๋ฆฌ์ฆ (A* algorithm) ๊ตฌ (0) | 2023.12.08 |
[Java] ์ฌ๊ท | ํ๋ ธ์ด ํ (0) | 2023.12.04 |
[์๋ฃ๊ตฌ์กฐ] ํ (Queue) ๊ตฌํ (Java) (0) | 2023.12.04 |
[Java] ์ฌ๊ท | ์ ํด๋ฆฌ๋ ํธ์ ๋ฒ(์ต๋ ๊ณต์ฝ์ ๊ตฌํ๊ธฐ) (0) | 2023.12.04 |