KangMoo
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 자료구조 만들어보기 ## 배열 리스트 (Array List) 1. MyList 클래스를 생성한다. 2. 배열을 사용하여 리스트를 구현한다. 3. add() 메소드를 사용하여 요소를 추가한다. 4. get() 메소드를 사용하여 요소를 조회한다. 5. size() 메소드를 사용하여 리스트의 크기를 조회한다. 6. remove() 메소드를 사용하여 요소를 삭제한다. ### 배열 리스트 (Array List) 코드 ```java public class MyArrayList { private int[] array; private int size; public MyArrayList() { array = new int[10]; size = 0; } public void add(int value) { if (size >= array.length) { int[] newArray = new int[array.length * 2]; System.arraycopy(array, 0, newArray, 0, array.length); array = newArray; } array[size++] = value; } public int get(int index) { return array[index]; } public int size() { return size; } public void remove(int index) { System.arraycopy(array, index + 1, array, index, size - index - 1); size--; } } ``` ```java public class Main { public static void main(String[] args) { MyArrayList list = new MyArrayList(); list.add(10); list.add(20); list.add(30); System.out.println(list.size()); System.out.println("---------"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println("---------"); list.remove(1); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } } ``` ## 연결 리스트 (Linked List) 1. 데이터와 다음 노드를 저장할 Node 클래스를 생성한다. 2. add() 메소드를 사용하여 요소를 추가한다. 3. get() 메소드를 사용하여 요소를 조회한다. 4. size() 메소드를 사용하여 리스트의 크기를 조회한다. 5. remove() 메소드를 사용하여 요소를 삭제한다. ### 연결 리스트 (Linked List) 코드 ```java public class MyLinkedList { public static class Node { int data; Node next; public Node(int data) { this.data = data; } } private Node head; private int size; public void add(int value) { Node newNode = new Node(value); if (head == null) { head = newNode; } else { Node last = head; while (last.next != null) { last = last.next; } last.next = newNode; } size++; } public int get(int index) { Node node = head; for (int i = 0; i < index; i++) { node = node.next; } return node.data; } public int size() { return size; } public void remove(int index) { if (index == 0) { head = head.next; } else { Node node = head; for (int i = 0; i < index - 1; i++) { node = node.next; } node.next = node.next.next; } size--; } } ``` ```java public class Main { public static void main(String[] args) { MyLinkedList list = new MyLinkedList(); list.add(10); list.add(20); list.add(30); System.out.println(list.size()); System.out.println("---------"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println("---------"); list.remove(1); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } } ``` ## 스택 (Stack) 1. MyStack 클래스를 생성한다. 2. 배열을 사용하여 스택을 구현한다. 3. push() 메소드를 사용하여 요소를 추가한다. 4. pop() 메소드를 사용하여 요소를 제거한다. 5. peek() 메소드를 사용하여 요소를 조회한다. 6. size() 메소드를 사용하여 스택의 크기를 조회한다. ### 스택 (Stack) 코드 ```java public class MyStack { private int[] array; private int size; public MyStack() { array = new int[10]; size = 0; } public void push(int value) { if (size >= array.length) { int[] newArray = new int[array.length * 2]; System.arraycopy(array, 0, newArray, 0, array.length); array = newArray; } array[size++] = value; } public int pop() { return array[--size]; } public int peek() { return array[size - 1]; } public int size() { return size; } } ``` ```java public class Main { public static void main(String[] args) { MyStack stack = new MyStack(); stack.push(10); stack.push(20); stack.push(30); System.out.println(stack.size()); System.out.println(stack.pop()); System.out.println(stack.pop()); System.out.println(stack.peek()); } } ``` ## 큐 (Queue) 1. MyQueue 클래스를 생성한다. 2. 배열을 사용하여 큐를 구현한다. 3. add() 메소드를 사용하여 요소를 추가한다. 4. poll() 메소드를 사용하여 요소를 제거한다. 5. peek() 메소드를 사용하여 요소를 조회한다. 6. size() 메소드를 사용하여 큐의 크기를 조회한다. ### 큐 (Queue) 코드 ```java public class MyQueue { private int[] array; private int size; private int front; private int rear; public MyQueue() { array = new int[10]; size = 0; front = 0; rear = 0; } public void add(int value) { if (size >= array.length) { int[] newArray = new int[array.length * 2]; System.arraycopy(array, 0, newArray, 0, array.length); array = newArray; } array[rear] = value; rear = (rear + 1) % array.length; size++; } public int poll() { int value = array[front]; front = (front + 1) % array.length; size--; return value; } public int peek() { return array[front]; } public int size() { return size; } } ``` ```java public class Main { public static void main(String[] args) { MyQueue queue = new MyQueue(); queue.add(10); queue.add(20); queue.add(30); System.out.println(queue.size()); System.out.println(queue.poll()); System.out.println(queue.poll()); System.out.println(queue.peek()); } } ``` ## 데크 (Deque, Double-Ended Queue) 1. MyDeque 클래스를 생성한다. 2. 배열을 사용하여 데크를 구현한다. 3. addFirst() 메소드를 사용하여 요소를 앞쪽에 추가한다. 4. addLast() 메소드를 사용하여 요소를 뒤쪽에 추가한다. 5. pollFirst() 메소드를 사용하여 요소를 앞쪽에서 제거한다. 6. pollLast() 메소드를 사용하여 요소를 뒤쪽에서 제거한다. 7. peekFirst() 메소드를 사용하여 요소를 앞쪽에서 조회한다. 8. peekLast() 메소드를 사용하여 요소를 뒤쪽에서 조회한다. 9. size() 메소드를 사용하여 데크의 크기를 조회한다. ### 데크 (Deque, Double-Ended Queue) 코드 ```java public class MyDeque { private int[] array; private int size; private int front; private int rear; public MyDeque() { array = new int[10]; size = 0; front = 0; rear = 0; } public void addFirst(int value) { if (size >= array.length) { int[] newArray = new int[array.length * 2]; System.arraycopy(array, 0, newArray, 0, array.length); array = newArray; } front = (front - 1 + array.length) % array.length; array[front] = value; size++; } public void addLast(int value) { if (size >= array.length) { int[] newArray = new int[array.length * 2]; System.arraycopy(array, 0, newArray, 0, array.length); array = newArray; } array[rear] = value; rear = (rear + 1) % array.length; size++; } public int pollFirst() { int value = array[front]; front = (front + 1) % array.length; size--; return value; } public int pollLast() { rear = (rear - 1 + array.length) % array.length; int value = array[rear]; size--; return value; } public int peekFirst() { return array[front]; } public int peekLast() { return array[(rear - 1 + array.length) % array.length]; } public int size() { return size; } } ``` ```java public class Main { public static void main(String[] args) { MyDeque deque = new MyDeque(); deque.addFirst(10); deque.addLast(20); deque.addLast(30); System.out.println(deque.size()); System.out.println(deque.pollFirst()); System.out.println(deque.pollLast()); System.out.println(deque.peekFirst()); } } ``` ## 이진 트리 (Binary Tree) 1. 데이터와 왼쪽 자식 노드, 오른쪽 자식 노드를 저장할 Node 클래스를 생성한다. 2. insert() 메소드를 사용하여 요소를 추가한다. 3. contains() 메소드를 사용하여 요소를 조회한다. 4. delete() 메소드를 사용하여 요소를 삭제한다. ### 이진 트리 (Binary Tree) 코드 ```java public class MyBinaryTree { public static class Node { int data; Node left; Node right; public Node(int data) { this.data = data; } } private Node root; public void insert(int value) { root = insert(root, value); } private Node insert(Node node, int value) { if (node == null) { return new Node(value); } if (value < node.data) { node.left = insert(node.left, value); } else if (value > node.data) { node.right = insert(node.right, value); } return node; } public boolean contains(int value) { return contains(root, value); } private boolean contains(Node node, int value) { if (node == null) { return false; } if (value < node.data) { return contains(node.left, value); } else if (value > node.data) { return contains(node.right, value); } else { return true; } } public void delete(int value) { root = delete(root, value); } private Node delete(Node node, int value) { if (node == null) { return null; } if (value < node.data) { node.left = delete(node.left, value); } else if (value > node.data) { node.right = delete(node.right, value); } else { if (node.left == null) { return node.right; } else if (node.right == null) { return node.left; } node.data = findMin(node.right).data; node.right = delete(node.right, node.data); } return node; } private Node findMin(Node node) { while (node.left != null) { node = node.left; } return node; } } ``` ```java public class Main { public static void main(String[] args) { MyBinaryTree tree = new MyBinaryTree(); tree.insert(10); tree.insert(20); tree.insert(30); System.out.println(tree.contains(20)); tree.delete(20); System.out.println(tree.contains(20)); } } ``` ## 트리 셋 (Tree Set) 1. 앞서 만든 이진 트리를 사용하여 트리 셋을 구현한다. 2. add() 메소드를 사용하여 요소를 추가한다. 3. contains() 메소드를 사용하여 요소를 조회한다. 4. remove() 메소드를 사용하여 요소를 삭제한다. ### 트리 셋 (Tree Set) 코드 ```java public class MyTreeSet { public static class Node { int data; Node left; Node right; public Node(int data) { this.data = data; } } private Node root; public void add(int value) { root = add(root, value); } private Node add(Node node, int value) { if (node == null) { return new Node(value); } if (value < node.data) { node.left = add(node.left, value); } else if (value > node.data) { node.right = add(node.right, value); } return node; } public boolean contains(int value) { return contains(root, value); } private boolean contains(Node node, int value) { if (node == null) { return false; } if (value < node.data) { return contains(node.left, value); } else if (value > node.data) { return contains(node.right, value); } else { return true; } } public void remove(int value) { root = remove(root, value); } private Node remove(Node node, int value) { if (node == null) { return null; } if (value < node.data) { node.left = remove(node.left, value); } else if (value > node.data) { node.right = remove(node.right, value); } else { if (node.left == null) { return node.right; } else if (node.right == null) { return node.left; } node.data = findMin(node.right).data; node.right = remove(node.right, node.data); } return node; } private Node findMin(Node node) { while (node.left != null) { node = node.left; } return node; } } ``` ```java public class Main { public static void main(String[] args) { MyTreeSet set = new MyTreeSet(); set.add(10); set.add(20); set.add(30); System.out.println(set.contains(20)); set.remove(20); System.out.println(set.contains(20)); } } ``` ## 트리 맵 (Tree Map) 1. 앞서 만든 이진 트리를 사용하여 트리 셋을 구현한다. 2. put() 메소드를 사용하여 요소를 추가한다. 3. get() 메소드를 사용하여 요소를 조회한다. 4. remove() 메소드를 사용하여 요소를 삭제한다. ### 트리 맵 (Tree Map) 코드 ```java public class MyTreeMap { public static class Node { int key; int value; Node left; Node right; public Node(int key, int value) { this.key = key; this.value = value; } } private Node root; public void put(int key, int value) { root = put(root, key, value); } private Node put(Node node, int key, int value) { if (node == null) { return new Node(key, value); } if (key < node.key) { node.left = put(node.left, key, value); } else if (key > node.key) { node.right = put(node.right, key, value); } else { node.value = value; } return node; } public Integer get(int key) { return get(root, key); } private Integer get(Node node, int key) { if (node == null) { return null; } if (key < node.key) { return get(node.left, key); } else if (key > node.key) { return get(node.right, key); } else { return node.value; } } public void remove(int key) { root = remove(root, key); } private Node remove(Node node, int key) { if (node == null) { return null; } if (key < node.key) { node.left = remove(node.left, key); } else if (key > node.key) { node.right = remove(node.right, key); } else { if (node.left == null) { return node.right; } else if (node.right == null) { return node.left; } Node min = findMin(node.right); node.key = min.key; node.value = min.value; node.right = remove(node.right, node.key); } return node; } private Node findMin(Node node) { while (node.left != null) { node = node.left; } return node; } } ``` ```java public class Main { public static void main(String[] args) { MyTreeMap map = new MyTreeMap(); map.put(10, 100); map.put(20, 200); map.put(30, 300); System.out.println(map.get(20)); map.remove(20); System.out.println(map.get(20)); } } ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully