λ³Έ ν¬μ€ν
μ νκ΅μμ λ°°μ΄ μμ
λ΄μ©κ³Ό λ μ°Ύμλ³Έ μλ£λ₯Ό κΈ°λ°μΌλ‘ μμ±νμμ΅λλ€.
λ€μ 볡μ΅νλ©΄μ 곡λΆνκΈ°μν΄μ μμ±νμμΌλ―λ‘ λΆμ‘±νκ±°λ μλͺ»λ κ²μ΄ μμ μ μμ΅λλ€.
μμ μ¬νμ΄ μλ€λ©΄ λκΈ λ¬μμ£ΌμΈμ!
μ€ν
μ€νμ μμμ μ§ν©μΌλ‘ μ¬μ©λλ μΆμ λ°μ΄ν° μ νμ΄λ€. μ€νμ μ νꡬ쑰μ΄λ€. JAVAλ 'Java.util' ν¨ν€μ§λ΄μ μ€ν ν΄λμ€λ₯Ό μ 곡νκ³ μλ€. μ°λ¦¬λ μ΄κ±Έ μ΄μ©ν¨μΌλ‘μ¨ μ€νμ λ°λ‘ ꡬννμ§μκ³ 'import'ν¨μΌλ‘μ¨ μ½κ² μ°λ¦¬κ° μνλ νλ‘κ·Έλ¨μ λ§λ€ μ μλ€.
νμ§λ§ λ μ μ΄ν΄νκΈ°μν΄μ μ€νμ νλνλ λ―μ΄μ 곡λΆν΄λ³΄λ €νλ€. λ λμκ° λλ§μ μ€νμ λ§λ€μ΄λ³΄μ.
μ€νμ μ μ
A pile of something, ususally neatly arranged
μ€νμ μμ μ¬μ μ μλ―Έμ΄λ€. κ·Έλ¦Όκ³Ό κ°μ΄ 'μλ€', 'μμ μ¬λ¦¬λ€'λΌλ μλ―Έμ΄λ€. μ€νμ ν μͺ½μμλ§ μκ³ , λΊ μ μλ€. λ¨ λ°©ν₯μ΄λ€. λ¨Όμ λ€μ΄κ°κ³ , λμ€μ λμ¨λ€ ("First in, Last out").
μ¬μ§μ²λΌ μ μΌ λ°μ μλ κ·Έλ¦μ΄ λ¨Όμ λ€μ΄κ° κ·Έλ¦μ΄κ³ , 맨 μμ μλ κ·Έλ¦μ λ§μ§λ§μ λ€μ΄κ° κ·Έλ¦μ΄λ€. λ°λ₯μ μλ κ·Έλ¦μ λΉΌλ €λ©΄ μμ μλ κ·Έλ¦λ€μ λ€ λΉΌμΌ λΊ μ μλ€.
μ΄κ²μ΄ μ€νμ΄ κ°μ§κ³ μλ ꡬ쑰μ νΉμ§μ΄λ€.
μ€νμ μλ
- μμ±μ
- new: λΉ μ€νμ λ§λ λ€.
- λ³νκΈ° (νΈλμ€ν¬λ¨Έ)
- κ΄μ°°μ
- 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
- 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.