[JAVA-Data Structures] Stack

NamdarineΒ·2022λ…„ 1μ›” 23일
0

Data Structure

λͺ©λ‘ 보기
3/4
post-thumbnail

λ³Έ ν¬μŠ€νŒ…μ€ ν•™κ΅μ—μ„œ 배운 μˆ˜μ—…λ‚΄μš©κ³Ό 더 μ°Ύμ•„λ³Έ 자료λ₯Ό 기반으둜 μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
λ‹€μ‹œ λ³΅μŠ΅ν•˜λ©΄μ„œ κ³΅λΆ€ν•˜κΈ°μœ„ν•΄μ„œ μž‘μ„±ν•˜μ˜€μœΌλ―€λ‘œ λΆ€μ‘±ν•˜κ±°λ‚˜ 잘λͺ»λœ 것이 μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.
μˆ˜μ • 사항이 μžˆλ‹€λ©΄ λŒ“κΈ€ λ‹¬μ•„μ£Όμ„Έμš”!

μŠ€νƒ

μŠ€νƒμ€ μš”μ†Œμ˜ μ§‘ν•©μœΌλ‘œ μ‚¬μš©λ˜λŠ” 좔상 데이터 μœ ν˜•μ΄λ‹€. μŠ€νƒμ€ μ„ ν˜•κ΅¬μ‘°μ΄λ‹€. JAVAλŠ” 'Java.util' νŒ¨ν‚€μ§€λ‚΄μ— μŠ€νƒ 클래슀λ₯Ό μ œκ³΅ν•˜κ³  μžˆλ‹€. μš°λ¦¬λŠ” 이걸 μ΄μš©ν•¨μœΌλ‘œμ¨ μŠ€νƒμ„ λ”°λ‘œ κ΅¬ν˜„ν•˜μ§€μ•Šκ³  'import'ν•¨μœΌλ‘œμ¨ μ‰½κ²Œ μš°λ¦¬κ°€ μ›ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ λ§Œλ“€ 수 μžˆλ‹€.
ν•˜μ§€λ§Œ 더 잘 μ΄ν•΄ν•˜κΈ°μœ„ν•΄μ„œ μŠ€νƒμ„ ν•˜λ‚˜ν•˜λ‚˜ λœ―μ–΄μ„œ κ³΅λΆ€ν•΄λ³΄λ €ν•œλ‹€. 더 λ‚˜μ•„κ°€ λ‚˜λ§Œμ˜ μŠ€νƒμ„ λ§Œλ“€μ–΄λ³΄μž.

μŠ€νƒμ˜ μ •μ˜

A pile of something, ususally neatly arranged

μŠ€νƒμ˜ 영영 사전적 μ˜λ―Έμ΄λ‹€. κ·Έλ¦Όκ³Ό 같이 'μŒ“λ‹€', 'μŒ“μ•„ μ˜¬λ¦¬λ‹€'λΌλŠ” μ˜λ―Έμ΄λ‹€. μŠ€νƒμ€ ν•œ μͺ½μ—μ„œλ§Œ μŒ“κ³ , λΊ„ 수 μžˆλ‹€. 단 λ°©ν–₯이닀. λ¨Όμ € λ“€μ–΄κ°€κ³ , λ‚˜μ€‘μ— λ‚˜μ˜¨λ‹€ ("First in, Last out").
μ‚¬μ§„μ²˜λŸΌ 제일 밑에 μžˆλŠ” 그릇이 λ¨Όμ € λ“€μ–΄κ°„ 그릇이고, 맨 μœ„μ— μžˆλŠ” 그릇은 λ§ˆμ§€λ§‰μ— λ“€μ–΄κ°„ 그릇이닀. λ°”λ‹₯에 μžˆλŠ” 그릇을 λΉΌλ €λ©΄ μœ„μ— μžˆλŠ” 그릇듀을 λ‹€ λΉΌμ•Ό λΊ„ 수 μžˆλ‹€.
이것이 μŠ€νƒμ΄ 가지고 μžˆλŠ” ꡬ쑰적 νŠΉμ§•μ΄λ‹€.

μŠ€νƒμ˜ μž‘λ™

  • μƒμ„±μž
    • new: 빈 μŠ€νƒμ„ λ§Œλ“ λ‹€.
  • λ³€ν™˜κΈ° (트랜슀포머)
    • push
    • pop
  • κ΄€μ°°μž
    - top (or peek)

μŠ€νƒμ˜ μ‚¬μš©

μŠ€νƒμ€ μ’…μ’… 'μ‹œμŠ€ν…œ' ν”„λ‘œκ·Έλž˜λ°μ— μ‚¬μš©λœλ‹€.

  • μž‘μ—… 호좜 μˆœμ„œλ₯Ό μΆ”μ ν•˜κΈ° μœ„ν•΄μ„œ.
  • μ»΄νŒŒμΌλŸ¬λŠ” μŠ€νƒμ„ μ‚¬μš©ν•΄μ„œ μ€‘μ²©λœ 언어문을 λΆ„μ„ν•œλ‹€.
  • 운영 μ²΄μ œλŠ” ν˜„μ œ 싀행쀑인 ν”„λ‘œμ„ΈμŠ€μ— λŒ€ν•œ 정보λ₯Ό μŠ€νƒμ— μ €μž₯ν•˜λ―€λ‘œ μ€‘λ‹¨λœ ν”„λ‘œμ„ΈμŠ€μ˜ 더 높은 μš°μ„ μˆœμœ„λ‘œ μž‘μ—…ν•  수 μžˆλ‹€.

Methods

  • push: 개체λ₯Ό μŠ€νƒμ˜ 맨 μœ„μ— μΆ”κ°€ν•œλ‹€.
  • pop: μŠ€νƒμ˜ 맨 μœ„ 개체λ₯Ό μ œκ±°ν•œλ‹€. λ§Œμ•½ μŠ€νƒμ΄ λΉ„μ–΄μžˆλ‹€λ©΄ μ˜ˆμ™Έκ°€ λ°œμƒν•œλ‹€.
  • top(or peek): μŠ€νƒμœΌλ‘œλΆ€ν„° μŠ€νƒμ˜ 맨 μœ„ 개체λ₯Ό μ œκ±°ν•˜μ§€μ•Šκ³  λ³Έλ‹€. 이 method μ—­μ‹œ μŠ€νƒμ΄ λΉ„μ–΄μžˆλ‹€λ©΄ μ˜ˆμ™Έκ°€ λ°œμƒν•œλ‹€.
  • isEmpty: μŠ€νƒμ— 아무것도 μ—†λ‹€λ©΄, 'true'λ₯Ό λ°˜ν™˜ν•œλ‹€. 그게 μ•„λ‹ˆλ©΄ 'false'λ₯Ό λ°˜ν™˜ν•œλ‹€.
  • search: 이 μŠ€νƒμ— κ°œμ²΄κ°€ μžˆλŠ” μœ„μΉ˜λ₯Ό λ°˜ν™˜ν•œλ‹€. κ°œμ²΄κ°€ μ‘΄μž¬ν•˜μ§€μ•ŠμœΌλ©΄ '-1'을 λ°˜ν™˜ν•œλ‹€.

전체 μ½”λ“œ

μŠ€νƒμ˜ λ³΅μž‘λ„ (Big-O)

μŠ€νƒμ˜ λ³΅μž‘λ„λŠ” O(1)이닀. μš°λ¦¬λŠ” search 연산을 μ œμ™Έν•œ μŠ€νƒμ˜ κΈ°λ³Έ 연산인 push, pop, peek, isEmpty 연산은 'loop'κΈ°λŠ₯을 μ‹€ν–‰ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ΄λ‹€. Search 연산은 O(n)이닀.

μŠ€νƒ μ‘μš© ν”„λ‘œκ·Έλž¨

  • 기호 κ· ν˜• μ‘°μ •
  • μ€‘μœ„ ν‘œκΈ°λ²•(infix), μ „μœ„ ν‘œκΈ°λ²•(prefix), ν›„μœ„ ν‘œκΈ°λ²•(postfix)의 μ„œλ‘œκ°„μ˜ λ³€ν™˜
  • MS μ˜€ν”ΌμŠ€, 에디터, 포토샡 λ“±μ—μ„œ 'Redo-undo' κΈ°λŠ₯
  • μ›Ή λΈŒλΌμš°μ €μ˜ λ’€λ‘œκ°€κΈ° λ˜λŠ” μ•žμœΌλ‘œκ°€κΈ° κΈ°λŠ₯
  • ν•˜λ…Έμ΄ νƒ€μ›Œ, 트리 νŠΈλž˜λ²„μ„€, μŠ€ν†‘ 슀팬 문제, νžˆμŠ€ν† κ·Έλž¨ λ¬Έμ œμ™€ 같은 λ§Žμ€ μ•Œκ³ λ¦¬μ¦˜
  • 역좔적은 μ•Œκ³ λ¦¬μ¦˜ 섀계 기법 쀑 ν•˜λ‚˜μ΄λ‹€. λ‚˜μ΄νŠΈ νˆ¬μ–΄ 문제, N-Queens 문제, 체슀, λ―Έλ‘œκ°€ κ·Έ μ˜ˆμ΄λ‹€.
  • κ·Έλž˜ν”„ μ•Œκ³ λ¦¬μ¦˜ (μœ„μƒ μ •λ ¬ 및 κ°•ν•˜κ²Œ μ—°κ²°λœ μ„±λΆ„)
  • λ©”λͺ¨λ¦¬ 관리. μš”μ¦˜ μΆœμ‹œλ˜λŠ” μ»΄ν“¨ν„°λŠ” μ‹€ν–‰ λͺ©μ μœΌλ‘œ μŠ€νƒμ„ μ£Ό κ΄€λ¦¬λ‘œ μ‚¬μš©ν•œλ‹€. 컴퓨터 μ‹œμŠ€ν…œμ—μ„œ μ‚¬μš©λ˜λŠ” 각각의 ν”„λ‘œκ·Έλž¨μ€ 각자의 λ©”λͺ¨λ¦¬λ₯Ό ν• λ‹Ήλœλ‹€.

κ΅¬ν˜„ 방법

Arrayλ‚˜ Linked List둜 κ΅¬ν˜„ν•  수 μžˆλ‹€.

https://github.com/namdarine/DataStructure.git
이 κΉƒν—ˆλΈŒμ— StackInterface, StackOverflowException, StackUnderflowException, Array Stack, Linked List Stack μ½”λ“œλ₯Ό μ˜¬λ €λ†“μ•˜μŠ΅λ‹ˆλ‹€.


This post was written based on what I've learn in college class and the materials I looked for. Also, it was written to study while reviewing, so there may be insufficient or wrong information. If there's anything to fix, please leave a comment!

Stack

Stack is an Abstract Data Type that serves as a collection of elements. Also, stack is a linear structure.
Java do provide the stack class under 'Java.util' package. We can use it easily using 'import'.

Definition of Stack

A pile of something, usually neatly arranged

A structure in which elements are added and removed from only one end; "Last in, First out" (LIFO) structure.

Operations on Stack

  • Constructor
    • new: creates an empty stack
  • Transformers
    • push
    • pop
  • Observer
    - top (or peek)

Usage of Stack

Stack is often used for 'system' programming.

  • To keep track of sequences of operation calls
  • Compilers use stacks to analyze nested language statements
  • Operating systems save information about the current executing process on a stack, so that it can work on a higher-priority, interrupting process.

Methods

  • push: Add an item onto the top of this stack.
  • pop: Removes the object at the top of this stack. It throws exception, if this stack is empty.
  • top (or peek): Looks at the object at the top of this stack without removing it from the stack. This method also throws exception, if this stack is empty.
  • isEmpty: Returns true if this stack has nothing. Otherwise, false.
  • search: Returns the 1-based position where an object is on this stack. Returns '-1' if the object does not on the stack.

Full Code

Complexity of Stack (Big-O)

The Complexity of the stack is O(1). Because we do not run 'loop' function in the push, pop, peek, and empty operations which is basic operation of stack without search operation. The complexity of search operation is O(n).

Applications of Stack

  • Balancing of symbols
  • Infix to Postfix or Prefix conversion
  • Redo-undo features in many programs such as MS office, editors, photoshop.
  • Forward and backward feature in web browsers.
  • In many algorithms such as Tower of Hanoi, tree traversals, stock span problem, histogram problem.
  • Backtracking is one of the algorithm designing techniques. Such as Knight-Tour problem, N-Queen problem, chess, maze.
  • In graph algorithms (Topological Sorting and Strongly Connected Components).
  • In memory management. Any modern computer uses a stack as the primary management for a running purpose. Each program that is running in a computer system has its own memory allocations.

Implementation

Array and Linked List

Reference

https://github.com/namdarine/DataStructure.git
StackInterface, StackOverflowException, StackUnderflowException, Array Stack, and Linked List Stack codes are uploaded in 'Stack' package inthis github repository.

0개의 λŒ“κΈ€