Post

🐣 2. Data Structures

πŸ“š 1. λ°°μ—΄κ³Ό λ¬Έμžμ—΄

1. Hash Tables

  • ν•΄μ‹œν…Œμ΄λΈ” : 효율적인 탐색을 μœ„ν•œ 자료ꡬ쑰
  • ν‚€(key)λ₯Ό κ°’(value)에 λŒ€μ‘ν•œλ‹€.

  • κ°„λ‹¨ν•œ ν•΄μ‹œν…Œμ΄λΈ” κ΅¬ν˜„ 방법

    • μ—°κ²°λ¦¬μŠ€νŠΈ(linked list), ν•΄μ‹œ μ½”λ“œ ν•¨μˆ˜(hash code function)
  • ν•΄μ‹œν…Œμ΄λΈ” 데이터 μ‚½μž… κ³Όμ •
    • ν‚€μ˜ ν•΄μ‹œ μ½”λ“œλ₯Ό κ³„μ‚°ν•œλ‹€.
      • ν‚€μ˜ μžλ£Œν˜•μ€ 보톡 int ν˜Ήμ€ long 이 λœλ‹€.
      • ➑️ μ΄λŠ” μ„œλ‘œ λ‹€λ₯Έ 두 개의 ν‚€κ°€ 같은 ν•΄μ‹œ μ½”λ“œλ₯Ό 가리킬 수 μžˆμŒμ„ λ‚˜νƒ€λƒ„!
    • hash(key) % array_length 와 같은 λ°©μ‹μœΌλ‘œ ν•΄μ‹œ μ½”λ“œλ₯Ό μ΄μš©ν•΄, λ°°μ—΄μ˜ 인덱슀λ₯Ό κ΅¬ν•œλ‹€.
    • λ°°μ—΄μ˜ 각 μΈλ±μŠ€μ—λŠ” ν‚€-κ°’ 으둜 이루어진 μ—°κ²°λ¦¬μŠ€νŠΈκ°€ μ‘΄μž¬ν•œλ‹€.
      • μΆ©λŒμ— λŒ€λΉ„ν•˜μ—¬ λ°˜λ“œμ‹œ μ—°κ²°λ¦¬μŠ€νŠΈλ₯Ό μ΄μš©ν•œλ‹€!



πŸ€” ν•΄μ‹œ 좩돌?

  • μ„œλ‘œ λ‹€λ₯Έ 두 개의 ν‚€κ°€ 같은 ν•΄μ‹œ μ½”λ“œλ₯Ό κ°€λ¦¬ν‚€κ±°λ‚˜
  • μ„œλ‘œ λ‹€λ₯Έ 두 개의 ν•΄μ‹œ μ½”λ“œκ°€ 같은 인덱슀λ₯Ό κ°€λ¦¬ν‚€λŠ” 경우


πŸ• μˆ˜ν–‰ μ‹œκ°„

  • μ΅œμ•…μ˜ 경우 : $O(N)$
  • But 일반적으둜 ν•΄μ‹œμ— λŒ€ν•΄ 이야기할 λ•ŒλŠ” μΆ©λŒμ„ μ΅œμ†Œν™”ν•˜λ„λ‘ 잘 κ΅¬ν˜„λœ 경우λ₯Ό κ°€μ •ν•œλ‹€. ➑️ 이 κ²½μš°λŠ” O(1)




2. ArrayList & Resizable Arraya

  • ArrayList : 동적 κ°€λ³€ 크기 κΈ°λŠ₯ λ‚΄μž¬ μžλ£Œν˜•

    • ArrayListλŠ” ν•„μš”μ— 따라 크기λ₯Ό λ³€ν™”μ‹œν‚¬ 수 μžˆμœΌλ©΄μ„œ $O(1)$ 의 μ ‘κ·Ό μ‹œκ°„μ„ μœ μ§€ν•œλ‹€.
    • 배열이 가득 μ°¨λŠ” μˆœκ°„, λ°°μ—΄μ˜ 크기λ₯Ό 2배둜 λŠ˜λ¦°λ‹€.
  • 크기λ₯Ό 2λ°° λŠ˜λ¦¬λŠ” 연산은 $O(N)$ μ΄μ§€λ§Œ 자주 λ°œμƒν•˜μ§€ μ•ŠμœΌλ―€λ‘œ, $O(1)$이 λœλ‹€.

    • _ β­οΈμƒν™˜ μž…λ ₯ μ‹œκ°„ κ°œλ… λ„μž…!_






3. StringBuilder

  • λ¬Έμžμ—΄μ„ ν•˜λ‚˜λ‘œ μ΄μ–΄λΆ™μ΄λŠ” 방법


  • ex1
1
2
3
4
5
6
7
String joinWords(String[] words){
	String sentence = "";
	for (String w : words) {
		sentence = sentence + w;
	}
	return sentence;
}
  • λ¬Έμžμ—΄μ„ 이어뢙일 λ•Œλ§ˆλ‹€ μƒˆλ‘œμš΄ λ¬Έμžμ—΄μ— λ³΅μ‚¬ν•˜λŠ” 방법
    • μ²˜μŒμ—λŠ” x개, 두 λ²ˆμ§ΈλŠ” 2x개, μ„Έ λ²ˆμ§ΈλŠ” 3x개, nλ²ˆμ§ΈλŠ” nx개의 문자λ₯Ό λ³΅μ‚¬ν•œλ‹€.
  • μ‹œκ°„λ³΅μž‘λ„ : $O(xn^2)$

    • $1 + 2 + … + n = n(n + 1)/2$


  • ex2
1
2
3
4
5
6
7
String joinWords(String[] words) {
	StringBuilder sentence = new StringBuilder();
	for (String w : words) {
		sentence.append(w);
	}
	return sentence.toString;
}
  • StringBuilder λ₯Ό μ΄μš©ν•˜μ—¬ κ°€λ³€ 크기 배열을 μ‚¬μš©ν•˜λŠ” 방법
    • ν•„μš”ν•œ κ²½μš°μ—λ§Œ λ¬Έμžμ—΄μ„ λ³΅μ‚¬ν•˜κ²Œλ” ν•œλ‹€.




πŸ“š 2. μ—°κ²°λ¦¬μŠ€νŠΈ

  • μ—°κ²°λ¦¬μŠ€νŠΈ : μ°¨λ‘€λ‘œ μ—°κ²°λœ λ…Έλ“œλ₯Ό ν‘œν˜„ν•΄μ£ΌλŠ” 자료ꡬ쑰
  • 단방ν–₯ μ—°κ²°λ¦¬μŠ€νŠΈ : κ°œλ³„ λ…Έλ“œ ➑️ λ‹€μŒ λ…Έλ“œ
  • μ–‘λ°©ν–₯ μ—°κ²°λ¦¬μŠ€νŠΈ : κ°œλ³„ λ…Έλ“œ ➑️ λ‹€μŒ λ…Έλ“œ & 이전 λ…Έλ“œ
  • ⭐️ μ‹œμž‘ μ§€μ μ—μ„œμ˜ μ•„μ΄ν…œ μΆ”κ°€ 및 μ‚­μ œ 연산이 μƒμˆ˜ μ‹œκ°„ μ†Œμš”


1. Creating a Linked List

  • 단방ν–₯ μ—°κ²°λ¦¬μŠ€νŠΈ κ΅¬ν˜„ μ½”λ“œ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Node {
  Node next = null;
  int data;
  public Node(int d) {
   data = d;
  }
  void appendToTail(int d){
   Node end = new Node(d);
   Node n = this;
   while(n.next != null){
    n = n.next;
   }
   n.next = end;
  }
}
  • 단방ν–₯ μ—°κ²°λ¦¬μŠ€νŠΈμ—μ„œμ˜ 고렀사항 : head κ°€ λ°”λ€Œλ©΄ μ–΄λ–»κ²Œ λ˜λŠ”κ°€?
    • πŸ‘‰ head κ°€ λ°”λ€Œμ—ˆμŒμ—λ„ μ–΄λ–€ κ°μ²΄λŠ” headλ₯Ό 계속 가리킀고 μžˆμ„ μˆ˜λ„ μžˆλ‹€.
    • Node 클래슀λ₯Ό ν¬ν•¨ν•˜λŠ” LinkedList 클래슀λ₯Ό λ§Œλ“ λ‹€.(head Node λ³€μˆ˜μ— headλ₯Ό κ°€λ¦¬ν‚€λŠ” κ°’ μ €μž₯)


2. Deleting a Node from a Singly Linked List

  • μ‚­μ œ λ…Έλ“œ : n
    1. prev.next λ₯Ό n.next 둜 μ—°κ²°ν•œλ‹€.
    2. (μ–‘λ°©ν–₯ μ—°κ²°λ¦¬μŠ€νŠΈμΌ 경우) n.next.prev λ₯Ό n.prev 둜 μ—°κ²°ν•œλ‹€.
  • ⚠️ λ©”λͺ¨λ¦¬ 관리가 ν•„μš”ν•œ μ–Έμ–΄λ₯Ό μ‚¬μš©ν•΄ κ΅¬ν˜„ν•˜λŠ” κ²½μš°μ—λŠ” μ‚­μ œν•œ λ…Έλ“œμ— ν• λ‹Ήλ˜μ—ˆλ˜ λ©”λͺ¨λ¦¬κ°€ μ œλŒ€λ‘œ λ°˜ν™˜λ˜μ—ˆλŠ”μ§€ 확인 ν•„μš”!
  • λ…Έλ“œ μ‚­μ œ μ½”λ“œ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Node deleteNode(Node head, int d) {
   Node n = head;
   if (n.data == d) {
    return head.next;
   }

   while (n.next != null) {
    if (n.next.data == d) {
       n.next = n.next.next;
       return head;
    }
     n = n.next;
   }
  return head;
}


3. The β€œRunner” Technique

  • Runner : μ—°κ²°λ¦¬μŠ€νŠΈλ₯Ό μˆœνšŒν•  λ•Œ 두 개의 포인터λ₯Ό λ™μ‹œμ— μ‚¬μš©ν•œλ‹€.
  • ν•œ 포인터가 λ‹€λ₯Έ 포인터보닀 μ•žμ„œλ„λ‘ ν•˜κ³  포인터λ₯Ό 움직일 λ•Œ μ§€μ •λœ 개수 ν˜Ήμ€ μ—¬λŸ¬ λ…Έλ“œλ₯Ό ν•œλ²ˆμ— 움직일 수 μžˆλ„λ‘ μ„€μ •ν•œλ‹€.


4. Recursive Problems

  • μ—°κ²°λ¦¬μŠ€νŠΈ 문제 β‰’ μž¬κ·€ 호좜
  • ⚠️ μž¬κ·€(recursive) μ•Œκ³ λ¦¬μ¦˜μ€ 적어도 $O(n)$의 곡간 λ³΅μž‘λ„λ₯Ό κ°–λŠ”λ‹€!




πŸŽ“Β 1. CS50 2020 - Lecture 2 - Arrays

1
int scores[3];
  • a chunk of memory
  • μ—°μ†λœ κ°’μœΌλ‘œ μž…λ ₯λœλ‹€.


1
2
3
4
int scores[3];
scores[0] = 72;
scores[1] = 73;
scores[2] = 33;

πŸ’‘ μΌμ’…μ˜ ν”„λ‘œκ·Έλž˜λ° κ΄€λ‘€λ‘œ 0λΆ€ν„° 인덱슀λ₯Ό μ‹œμž‘ν•œλ‹€.

  • 배열을 μ„ μ–Έν•  λ•Œ β€œ[3]”과 같은 ν‘œν˜„μ„ μ“°λŠ” 데, μ΄λŠ” 총 κ°’μ˜ 수λ₯Ό λ‚˜νƒ€λ‚Έλ‹€.
  • 배열에 색인을 생성할 λ•Œ, ν•΄λ‹Ή λ©”λͺ¨λ¦¬ 청크의 νŠΉμ • μœ„μΉ˜λ‘œ μ΄λ™ν•˜λ©΄ μœ μ‚¬ν•œ 숫자λ₯Ό μ‚¬μš©ν•œλ‹€.
    • ➑️ μƒλŒ€μ μΈ μœ„μΉ˜λ₯Ό μ°Έμ‘°(첫 번째, 두 번째, μ„Έ 번째)




πŸŽ“Β 2. Arrays | Coursera

1. Arrays

  • Array : λ©”λͺ¨λ¦¬μ˜ 연속적인 μ˜μ—­

    • ν•˜λ‚˜μ˜ λ©”λͺ¨λ¦¬ 청크
  • Key Point : random access

    • λ°°μ—΄μ˜ μ–΄λŠ νŠΉμ •ν•œ μš”μ†Œμ— μ ‘κ·Όν•  수 μžˆλ‹€.
    • β€œConstant time access to read, Constant time access to wright”
  • 각 μ›μ†Œμ˜ μ˜μ—­μ€ λͺ¨λ‘ 같은 μ‚¬μ΄μ¦ˆλ‘œ 할당이 λœλ‹€.
  • μ›μ†Œμ˜ μ£Όμ†Œλ₯Ό λ„μΆœν•˜λŠ” μˆ˜μ‹
\[arrayAddr + elemSize * (i - firstIndex)\]




2. Multi-Dimensional Arrays

Image

  • row - column 쌍으둜 ν‘œν˜„μ΄ κ°€λŠ₯ν•˜λ‹€.

  • μ ‘κ·Ό 방법

    • μ ‘κ·Όν•˜λ €λŠ” μš”μ†Œ μ΄μ „μ˜ 행듀을 κ±΄λ„ˆλ›΄λ‹€.
    • ν•œ ν–‰μ˜ λͺ¨λ“  μš”μ†Œλ“€μ˜ 개수λ₯Ό κ³±ν•œλ‹€.
\[elemSize _ ((3-1) _ 6 + (4-1))\]


2-1. row-major ordering & column-major ordering

Image

_(쒌 : row-major ordering우 : column-major ordering)_
  • λ©”λͺ¨λ¦¬μ—λŠ” λ‹€μŒκ³Ό 같이 λ°°μΉ˜λœλ‹€.

  • row-major ordering : μ›μ†Œκ°€ 행에 따라 μ—°μ†μ μœΌλ‘œ λ°°μΉ˜λ˜λŠ” 방식
  • column-major ordering : μ›μ†Œκ°€ 열에 따라 μ—°μ†μ μœΌλ‘œ λ°°μΉ˜λ˜λŠ” 방식
    • 언어와 μ»΄νŒŒμΌλŸ¬λ§ˆλ‹€ λ‹€λ₯΄λ‹€.




3. Times for Common Operations

Image

  • beginning : n개 μš”μ†Œ μž¬μ •λ ¬
  • end : 전체 μ›μ†Œμ˜ 개수만 μ—…λ°μ΄νŠΈν•˜λ©΄ 됨
  • middle : n/2개 μš”μ†Œ μž¬μ •λ ¬

  • ⭐ 핡심 : μ›μ†Œμ— μ ‘κ·Ό/읽기/쓰기에 μΌμ •ν•œ μ‹œκ°„μ΄ μ†Œμš”λœλ‹€.




✨ Summary

  • Array : contiguous area of memory consisting of equal-size elements indexed by contiguous integers.
  • Constant-time access to any element.
  • Constant time to add/remove at the end.
  • Linear time to add/remove at an arbitrary location.




πŸŽ“Β 3. Dynamic Arrays | Coursera

1. Problem

  • 문제 사항 : 배열은 정적이닀.

    • ν•΄κ²° : λŸ°νƒ€μž„μ‹œμ— 크기가 κ²°μ •λ˜λŠ” 동적 κ°€λ³€ 배열을 μ‚¬μš©ν•œλ‹€.


  • Solution : dynamic arrays

    • λ™μ μœΌλ‘œ ν• λ‹Ήλ˜λŠ” λ°°μ—΄μ˜ 포인터λ₯Ό μ €μž₯ν•˜κ³  μƒˆλ‘œ 할당이 ν•„μš”ν•  λ•Œμ—λŠ” ν•΄λ‹Ή 포인터λ₯Ό λ°”κΎΌλ‹€.
    • 이전 λ°°μ—΄μ˜ μ›μ†Œλ“€μ„ λ³΅μ‚¬ν•˜κ³  이전 λ°°μ—΄μ˜ μœ„μΉ˜λ₯Ό κ°€λ¦¬ν‚€λ˜ 포인터 값을 μƒˆλ‘œμš΄ 배열을 κ°€λ¦¬ν‚€λŠ” 포인터 κ°’μœΌλ‘œ λ°”κΎΌλ‹€.


  • Operations

    • get(i) : iλ²ˆμ§Έμ— μœ„μΉ˜ν•œ μ›μ†Œ
    • set(i, val) : i번째 μœ„μΉ˜μ— val μ‚½μž…
    • pushback(val) : val을 λ§ˆμ§€λ§‰ μœ„μΉ˜μ— μ‚½μž…
    • remove(i) : iλ²ˆμ§Έμ— μœ„μΉ˜ν•œ μ›μ†Œ 제거
    • size(i) : 전체 μ›μ†Œ 개수


  • size :vs: capacity

Image

  • 배열이 λ‹€ 찼을 경우
    1. μ›λž˜ 크기의 두 배만큼 μƒˆλ‘œμš΄ 배열을 λ§Œλ“ λ‹€.
    2. κΈ°μ‘΄ μ›μ†Œλ“€μ„ λ³΅μ‚¬ν•˜μ—¬ λ„£λŠ”λ‹€.
    3. μ›λž˜μ˜ 포인터 μœ„μΉ˜λ₯Ό μƒˆ λ°°μ—΄μ˜ 포인터 μœ„μΉ˜λ‘œ λ°”κΎΌλ‹€.




2. Common Implementations

  • C++ : vector
  • Java : ArrayList
  • Python : list
    • pythonμ—μ„œλŠ” 정적 배열을 ν‘œν˜„ν•  수 μ—†λ‹€.




3. Runtimes

  • get(i) : $O(1)$
  • set(i, val) : $O(1)$
  • pushback(val) : $O(n)$
  • remove(i) : $O(n)$
  • size() : $O(1)$




✨ Summary

  • Unlike static arrays, dynamic arrays can be resized.
  • Appending a new element to a dynamic array is often constant time, but can take $O(n)$.
  • Some space is wasted.




πŸŽ“ 4. Implementation Arrays

Implement a vector (mutable array with automatic resizing)

  • Practice coding using arrays and pointers, and pointer math to jump to an index instead of using indexing.
  • New raw data array with allocated memory
    • can allocate int array under the hood, just not use its features
    • start with 16, or if starting number is greater, use power of 2 - 16, 32, 64, 128
  • size() - number of items
  • capacity() - number of items it can hold
  • is_empty()
  • at(index) - returns item at given index, blows up if index out of bounds
  • push(item)
  • insert(index, item) - inserts item at index, shifts that index’s value and trailing elements to the right
  • prepend(item) - can use insert above at index 0
  • pop() - remove from end, return value
  • delete(index) - delete item at index, shifting all trailing elements left
  • remove(item) - looks for value and removes index holding it (even if in multiple places)
  • find(item) - looks for value and returns first index with that value, -1 if not found
  • resize(new_capacity) // private function
    • when you reach capacity, resize to double the size
    • when popping an item, if size is 1/4 of capacity, resize to half
  • Time
    • O(1) to add/remove at end (amortized for allocations for more space), index, or update
    • O(n) to insert/remove elsewhere
  • Space
    • contiguous in memory, so proximity helps performance
    • space needed = (array capacity, which is >= n) * size of item, but even if 2n, still O(n)




πŸ€” 섀계 고렀사항

  • μžλ£Œν˜•μ€ int둜 ν•œμ •ν•œλ‹€.
  • 맨 처음 μƒμ„±μžλ₯Ό 톡해 μ΄ˆκΈ°ν™” 벑터가 μƒμ„±λœλ‹€.

New raw data array with allocated memory

  • can allocate int array under the hood, just not use its features
  • start with 16, or if starting number is greater, use power of 2 - 16, 32, 64, 128


  • java의 ArrayList λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³  정적 λ°°μ—΄ ν˜•νƒœλ‘œ 동적 κ°€λ³€ 배열을 κ΅¬ν˜„ν•œλ‹€.
  • κΈ°λ³Έ μ›μ†Œμ˜ κ°œμˆ˜λŠ” 16λΆ€ν„° μ‹œμž‘ν•œλ‹€.
  • κ·Έ λ°–μ˜ λ””ν…ŒμΌν•œ κ΅¬ν˜„ 사항듀은 λ¬Έμ„œν™” μ£Όμ„μœΌλ‘œ ν•¨μˆ˜ μ •μ˜λ¬Έ μœ„μ— κΈ°λ‘ν•œλ‹€.


  • ⚠️ μ£Όμ˜μ‚¬ν•­
    • μ‚½μž… μ‹œ, λ°°μ—΄ ν™•μž₯ κ³ λ €
    • μ‚­μ œ μ‹œ, λ°°μ—΄ μΆ•μ†Œ κ³ λ €
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
public class Arrays {
    private int[] vector;
    private int elemCnt = 0;

    public Arrays(){
        vector = new int[16];
    }

    public int size(){
        return elemCnt;
    }

    public int capacity(){
        return vector.length - size();
    }

    /**
     * @return λΉ„μ–΄μžˆμœΌλ©΄ True(1), μš”μ†Œκ°€ 있으면 False(0)
     */
    public boolean is_empty(){
        return elemCnt == 0;
    }

    /**
     * μž…λ ₯ μΈλ±μŠ€κ°€ λ²”μœ„ 밖이면 'IndexOutOfBoundsException' μ—λŸ¬ λ°œμƒ
     */
    public int at(int index){
        if (index >= 0 || index < size()){
            return vector[index];
        } else {
            throw new IndexOutOfBoundsException();
        }
    }

    public void push(int item){
        if (capacity() == 0){
            resize(size() * 2);
        }
        vector[elemCnt++] = item;
    }

    /**
     * μž…λ ₯ μΈλ±μŠ€κ°€ λ²”μœ„ 밖이면 'IndexOutOfBoundsException' μ—λŸ¬ λ°œμƒ
     */
    public void insert(int index, int item){
        if (index < size()){
            int[] newVector = new int[vector.length];

            if (capacity() == 0) {
                resize(size() * 2);
            }

            for (int i = 0; i < index; i++) {
                newVector[i] = vector[i];
            }
            newVector[index] = item;
            for (int i = index + 1; i < size() + 1; i++) {
                newVector[i] = vector[i - 1];
            }

            elemCnt++;
            vector = newVector;
        } else {
            throw new IndexOutOfBoundsException();
        }
    }

    public void prepend(int item){
        if (capacity() == 0){
            resize(size() * 2);
        }

        int[] newVector = new int[vector.length];

        for (int i = 0; i < size(); i++){
            newVector[i + 1] = vector[i];
        }
        newVector[0] = item;

        elemCnt++;
        vector = newVector;
    }

    /**
     * 벑터가 λΉ„μ—ˆμ„ 경우 'NullPointerException' μ—λŸ¬ λ°œμƒ
     */
    public int pop(){
        int rst;

        if (is_empty()){
            throw new NullPointerException();
        }

        rst = vector[elemCnt - 1];
        vector[elemCnt--] = 0;

        if (size() / 4 == vector.length){
            resize(size() / 4);
        }

        return rst;
    }

    /**
     * μž…λ ₯ μΈλ±μŠ€κ°€ λ²”μœ„ 밖이면 'IndexOutOfBoundsException' μ—λŸ¬ λ°œμƒ
     */
    public void delete(int index){
        if (index < size()){
            for (int i = index; i < size(); i++){
                vector[i] = vector[i + 1];
            }

            elemCnt--;

            if (size() / 4 == vector.length){
                resize(size() / 4);
            }
        } else {
            throw new IndexOutOfBoundsException();
        }
    }

    /**
     * item이 μ‘΄μž¬ν•˜λŠ” λͺ¨λ“  인덱슀 μœ„μΉ˜μ˜ μ›μ†Œ μ‚­μ œ
     */
    public void remove(int item){
        for (int i = 0; i < size(); i++) {
            if (vector[i] == item){
                delete(i);
            }
        }
    }

    /**
     * item이 벑터 내에 μ—†μœΌλ©΄ -1 리턴
     */
    public int find(int item){
        for (int i = 0; i < size(); i++) {
            return i;
        }
        return -1;
    }

    private void resize(int new_capacity){
        int[] newVector = new int[new_capacity];

        for (int i = 0; i < size(); i++) {
            newVector[i] = vector[i];
        }
        vector = newVector;
    }

    // test
    public static void main(String[] args) {
        Arrays tmpVector = new Arrays();
        System.out.println("test 1:" + tmpVector.size());
        System.out.println("test 2:" + tmpVector.capacity());
        System.out.println("test 3:" + tmpVector.is_empty());
        tmpVector.push(3);
        tmpVector.push(4);
        System.out.println("test 4:" + tmpVector.at(0)); // 3
        System.out.println("test 5:" + tmpVector.at(1)); // 4
        tmpVector.insert(0, 2);
        tmpVector.prepend(1);
        System.out.println("test 6:" + java.util.Arrays.toString(tmpVector.vector)); // [1, 2, 3, 4, ...]
        System.out.println("test 7:" + tmpVector.pop());
        tmpVector.delete(0);
        tmpVector.remove(2);
        System.out.println("test 8:" + tmpVector.find(3)); // 0
    }
}




πŸŽ“Β 4. Implementation LinkedList

  • Implement (I did with tail pointer & without):
    • size() - returns number of data elements in list
    • empty() - bool returns true if empty
    • value_at(index) - returns the value of the nth item (starting at 0 for first)
    • push_front(value) - adds an item to the front of the list
    • pop_front() - remove front item and return its value
    • push_back(value) - adds an item at the end
    • pop_back() - removes end item and returns its value
    • front() - get value of front item
    • back() - get value of end item
    • insert(index, value) - insert value at index, so current item at that index is pointed to by new item at index
    • erase(index) - removes node at given index
    • value_n_from_end(n) - returns the value of the node at nth position from the end of the list
    • reverse() - reverses the list
    • remove_value(value) - removes the first item in the list with this value
    • Doubly-linked List




πŸ€” 섀계 고렀사항

  • μžλ£Œν˜•μ€ int둜 ν•œμ •ν•œλ‹€.
  • 맨 처음 μƒμ„±μžλ₯Ό 톡해 μ΄ˆκΈ°ν™” λ§ν¬λ“œ λ¦¬μŠ€νŠΈκ°€ μƒμ„±λœλ‹€.
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.util.*;

public class ImplLinkedList {
 private LinkedList<Integer> LinkedList;

 public ImplLinkedList() {
  LinkedList = new LinkedList<>();
 }

 public int size() {
  return LinkedList.size();
 }

 public boolean empty() {
  return LinkedList.size() == 0;
 }

 /**
  * μž…λ ₯ μΈλ±μŠ€κ°€ λ²”μœ„ 밖이면 'IndexOutOfBoundsException' μ—λŸ¬ λ°œμƒ
  */
 public int value_at(int index) {
  if (index >= 0 || index < size()){
   return LinkedList.get(index);
  } else {
   throw new IndexOutOfBoundsException();
  }
 }

 public void push_front(int value) {
  LinkedList<Integer> newLinkedList = new LinkedList<>();
  newLinkedList.push(value);

  for (int e : LinkedList)
   newLinkedList.push(e);

  LinkedList = newLinkedList;
 }

 /**
  * λ§ν¬λ“œ λ¦¬μŠ€νŠΈκ°€ λΉ„μ—ˆμ„ 경우 'NullPointerException' μ—λŸ¬ λ°œμƒ
  */
 public int pop_front() {
  if (empty()){
   throw new NullPointerException();
  }
  return LinkedList.pollFirst();
 }

 public void push_back(int value){
  LinkedList.push(value);
 }

 /**
  * λ§ν¬λ“œ λ¦¬μŠ€νŠΈκ°€ λΉ„μ—ˆμ„ 경우 'NullPointerException' μ—λŸ¬ λ°œμƒ
  */
 public int pop_back() {
  if (empty()){
   throw new NullPointerException();
  }
  return LinkedList.poll();
 }

 /**
  * λ§ν¬λ“œ λ¦¬μŠ€νŠΈκ°€ λΉ„μ—ˆμ„ 경우 'NullPointerException' μ—λŸ¬ λ°œμƒ
  */
 public int front() {
  if (empty()){
   throw new NullPointerException();
  }
  return LinkedList.peekFirst();
 }

 /**
  * λ§ν¬λ“œ λ¦¬μŠ€νŠΈκ°€ λΉ„μ—ˆμ„ 경우 'NullPointerException' μ—λŸ¬ λ°œμƒ
  */
 public int back() {
  if (empty()){
   throw new NullPointerException();
  }
  return LinkedList.peek();
 }

 /**
  * μž…λ ₯ μΈλ±μŠ€κ°€ λ²”μœ„ 밖이면 'IndexOutOfBoundsException' μ—λŸ¬ λ°œμƒ
  */
 public void insert(int index, int value) {
  if (index >= 0 || index < size()){
   LinkedList<> newLinkedList = new LinkedList<Integer>();

   for (int i = 0; i < index; i++) {
    newLinkedList.push(LinkedList.get(i));
   }

   newLinkedList.add(index, value);

   for (int i = index; i <= size(); i++) {
    newLinkedList.push(LinkedList.get(i));
   }
   LinkedList = newLinkedList;

  } else {
   throw new IndexOutOfBoundsException();
  }
 }

 /**
  * μž…λ ₯ μΈλ±μŠ€κ°€ λ²”μœ„ 밖이면 'IndexOutOfBoundsException' μ—λŸ¬ λ°œμƒ
  */
 public void erase(int index) {
  if (index >= 0 || index < size()){
   LinkedList.remove(index);
  } else {
   throw new IndexOutOfBoundsException();
  }
 }

 /**
  * 0번째 μš”μ†ŒλΆ€ν„° κ°€λŠ₯ -> 0번째 μš”μ†Œ : λ§ˆμ§€λ§‰ μš”μ†Œ
  * μž…λ ₯ μΈλ±μŠ€κ°€ λ²”μœ„ 밖이면 'IndexOutOfBoundsException' μ—λŸ¬ λ°œμƒ
  */
 public int value_v_from_end(int n) {
  if (n >= 0 || n < size()){
   return LinkedList.get(size() - 1 - n);
  } else {
   throw new IndexOutOfBoundsException();
  }
 }

 public LinkedList<Integer> reverse() {
  LinkedList<Integer> newLinkedList = new LinkedList<>();

  for (int e : LinkedList)
   newLinkedList.add(0, e);

  return newLinkedList;
 }

 /**
  * 값이 μ—†μœΌλ©΄ 아무것도 μˆ˜ν–‰ν•˜μ§€ μ•ŠμŒ
  * 닀쀑 κ°’ 쑴재 μ‹œ, 맨 μ•ž μš”μ†Œ 제거
  */
 public void remove_value(int value) {
  for (int e : LinkedList){
   if (e == value)
    LinkedList.remove(e);
  }
 }
}
This post is licensed under CC BY 4.0 by the author.