Skip to content

Latest commit

ย 

History

History
57 lines (36 loc) ยท 2.39 KB

File metadata and controls

57 lines (36 loc) ยท 2.39 KB

Week4 (21.03.29.์›”)

ํ•™์Šตํ•œ ๋‚ด์šฉ

Linked List

    public class LinkedList<E> extends AbstractSequentialList<E>
        implements List<E>, Deque<E>, Cloneable, Serializable
  • ์ž๋ฐ”์˜ LinkedList๋Š” Stack์œผ๋กœ๋„ Queue๋กœ๋„ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.
  • AbstractSequentialList๋ฅผ ์ƒ์†ํ•œ๋‹ค.
  • List, Deque๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
  • Doubly Linked List๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค.
  • ์ค‘๋ณต ๊ฐ’๊ณผ null ๋ชจ๋‘ ์‚ฝ์ž… ๊ฐ€๋Šฅํ•˜๋‹ค.

Stack

public class Stack<E> extends Vector<E>
  • ๊ณต์‹ ๋ฌธ์„œ์—์„œ Deque<Integer> stack = new ArrayDeque<Integer>(); ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•œ๋‹ค.
    • Why?

      https://stackoverflow.com/questions/12524826/why-should-i-use-deque-over-stack

      • Object-oriented design - inheritance, abstraction, classess and interfaces

        Stack์€ ํด๋ž˜์Šค๊ณ  Deque๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋‹ค. ํด๋ž˜์Šค๋Š” ๋‹จ์ผ ์ƒ์†๋งŒ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋‹ค์ค‘ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค. Deque๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ Stack ํด๋ž˜์Šค์™€ ๊ทธ์˜ ๋ถ€๋ชจํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์˜์กด๊ด€๊ณ„๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ข€ ๋” ์œ ์—ฐํ•˜๊ฒŒ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, Deque ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๋‹ค๋ฅธ ํด๋ž˜์Šค(LinkedList)๋กœ ์‰ฝ๊ฒŒ ๊ต์ฒด๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

      • Inconsistency

        Stack์€ ์•„์ฃผ ์˜ค๋ž˜์ „์— ๋งŒ๋“ค์–ด์ง„ ํด๋ž˜์Šค๋กœ์„œ Vector ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๊ณ  ์žˆ๋‹ค. Vector ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•จ์œผ๋กœ ์ธํ•ด Stack์— ๋ถˆํ•„์š”ํ•œ ๊ธฐ๋Šฅ(์ธ๋ฑ์Šค๋กœ ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ)์ด ์ถ”๊ฐ€๋œ๋‹ค.

      • Performance

        Stack ํด๋ž˜์Šค๊ฐ€ ์ƒ์†ํ•œ Vector ํด๋ž˜์Šค๋Š” ArrayList์˜ ์Šค๋ ˆ๋“œ ์„ธ์ดํ”„ํ•œ ๋ฒ„์ „์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋™๊ธฐํ™” ๊ณผ์ •์€ ์ถ”๊ฐ€์ ์ธ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์š”๊ตฌํ•˜๊ฑฐ๋‚˜ ์„ฑ๋Šฅ์˜ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์•ผ๊ธฐํ•œ๋‹ค.

Queue

public interface Queue<E> extends Collection<E>
  • Collection ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ฉ”์†Œ๋“œ์— ์ถ”๊ฐ€์ ์œผ๋กœ Queue๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์‚ฝ์ž…/์‚ญ์ œ/์กฐํšŒ ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋‹ค. ๋ฉ”์†Œ๋“œ๋Š” ๋‘ ๊ฐ€์ง€ ํ˜•ํƒœ๋กœ ์ œ๊ณต๋œ๋‹ค. ๋ฉ”์†Œ๋“œ์—์„œ ์ˆ˜ํ–‰ํ•˜๋Š” ์ž‘์—…์ด ์‹คํŒจํ–ˆ์„ ๋•Œ ํ•˜๋‚˜๋Š” ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋Š” ๋ฐฉ์‹์ด๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ํŠน๋ณ„ํ•œ ๊ฐ’(null์ด๋‚˜ false)์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

    cf. https://docs.oracle.com/javase/7/docs/api/java/util/Queue.html

    Throws exception Returns special value
    Insert add(e) offer(e)
    Remove remove() poll()
    Examine element() peek()