๋ฐ์ํ
์ฐ๊ฒฐ ๋ฆฌ์คํธ (Linked List)
์ฐ๊ฒฐ๋ ๋ค์ ์ด์์ ๋ํ ์ฃผ์๋ฅผ ์ ์ฅํด์ผ ํ๋ฏ๋ก <data + link> ์ ๋จ์๋ก ์ ์ฅํ๋ค.(์ด์ ๊ฐ์ ๋จ์๋ฅผ Node ๋ผ๊ณ ํจ)
์ฆ, ์์(์ฃผ์) - ์ฐ๊ฒฐ(Link) - ๋(Null Pointer or Circular) ์ ๊ฐ์ ์์๊ฐ ์กด์ฌ
1. ๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ (Singly Linked List)
2. ์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ (Circular Linked List)
3. ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ (Doubly Linked List)
Linked List ์์
package linkedList;
public interface LinkedList {
ListNode add(ListNode head, ListNode nodeToAdd, int position);
ListNode remove(ListNode head, int positionToRemove);
void printList(ListNode head);
boolean contains(ListNode head, ListNode nodeToCheck);
}
package linkedList;
public class ListNode implements LinkedList{
int data;
private ListNode next;
public ListNode(int data) {
this.data = data;
next = null;
}
public ListNode() {
}
@Override
public ListNode add(ListNode head, ListNode nodeToAdd, int position) {
// ์ด๊ธฐ ์ํ
if (position == 0 && head == null) {
head = nodeToAdd;
return head;
}
// ๋งจ ์์ add
else if (position == 0){
nodeToAdd.next = head;
head = nodeToAdd;
return head;
}
ListNode node = head;
// find pos
for (int i = 0; i < position - 1; i++) {
node = node.next;
}
// ํด๋น pos์ add
nodeToAdd.next = node.next;
node.next = nodeToAdd;
return head;
}
@Override
public ListNode remove(ListNode head, int positionToRemove) {
if (positionToRemove == 0) {
head = head.next;
return head;
}
else {
ListNode node = head;
for(int i=0; i< positionToRemove; i++) {
node = node.next;
}
node.next = node.next.next;
return head;
}
}
@Override
public void printList(ListNode head) {
ListNode node = head;
while (node != null) {
System.out.print(node.data);
node = node.next;
}
System.out.println();
}
@Override
public boolean contains(ListNode head, ListNode nodeToCheck) {
ListNode node = head;
while(node != null) {
if (node.data == nodeToCheck.data) {
return true;
}
node = node.next;
}
return false;
}
public static void main(String[] args) {
LinkedList nums = new ListNode();
ListNode head = null;
for(int i=0; i<5; i++) {
head = nums.add(head, new ListNode(i), i);
}
nums.printList(head);
head = nums.remove(head, 0);
head = nums.remove(head, 0);
nums.printList(head);
System.out.println(nums.contains(head, new ListNode(3)));
System.out.println(nums.contains(head, new ListNode(1)));
}
}
package linkedList;
public class ListNodeStack {
private ListNode head;
private int top;
public ListNodeStack() {
this.head = new ListNode();
this.top = -1;
}
public void push(int data) {
ListNode listNode = new ListNode(data);
head.add(this.head, listNode, ++top);
}
public int pop() {
if(top == -1) {
System.out.println("Empty");
return top;
}
return head.remove(head,--top).data;
}
public void print() {
head.printList(head);
}
public static void main(String[] args) {
ListNodeStack listNodeStack = new ListNodeStack();
for(int i=0; i<5; i++) {
listNodeStack.push(i);
}
listNodeStack.print();
listNodeStack.pop();
listNodeStack.print();
}
}
300x250
'IT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Eclipse] ์ดํด๋ฆฝ์ค Devstyle ์ญ์ ํ๋ ๋ฐฉ๋ฒ (0) | 2023.03.02 |
---|---|
[์๋ฃ๊ตฌ์กฐ] ํธ๋ฆฌ์ ์ข ๋ฅ, 3๊ฐ์ง ์ํ๋ฐฉ๋ฒ (0) | 2022.05.02 |
[์๋ฃ๊ตฌ์กฐ] ํธ๋ฆฌ ์ฉ์ด๋? (0) | 2022.05.01 |
๊ด๊ณ ์ฑ๊ณผ ์ธก์ CPM ๊ณผ eCPM ์ด๋? (0) | 2022.04.13 |
[error] Server Tomcat v9.0 Server at localhost failed to start. ํด๊ฒฐํ๊ธฐ (0) | 2022.04.07 |