본문 바로가기

Computer Science/Data Structure

1. Stack (Linear Data Structure-LIFO)

https://www.geeksforgeeks.org/stack-data-structure/

 

* Module(import, Package(Source-Files/Code) Units)

* Access Control (Encapsulation)

- open(상속/오버라이드)/public: 모듈 외부 사용

> internal(default): 특정 모듈(앱) 내 사용

> firePrivate(같은 소스 파일 내) / private: 같은 클래스, 구조체 내 사용

// mutating 키워드: 인스턴스에서 내부 값 수정 가능

// Generic T(Type Parameter: Any)

public struct Stack<T> {
  fileprivate var arr = [T]()

  public var isEmpty: Bool {
    return arr.isEmpty
  }

  public var count: Int {
    return arr.count
  }

  public mutating func push(_ element: T) {
    arr.append(element)
  }

  public mutating func pop() -> T? {
    return arr.popLast()
  }

  public var top: T? {
    return arr.last
  }
}

* LinkedList / Array로 구현 가능 (동적/정적)

- count  isEmpty  peek(top)

- push  pop

Key Operations on Stack Data Structures

  • Push: Adds an element to the top of the stack.
  • Pop: Removes the top element from the stack.
  • Peek: Returns the top element without removing it.
  • IsEmpty: Checks if the stack is empty.
  • IsFull: Checks if the stack is full (in case of fixed-size arrays).

Applications of Stack Data Structures

  • Recursion
  • Expression Evaluation and Parsing
  • Depth-First Search (DFS)
  • Undo/Redo Operations
  • Browser History
  • Function Calls

 

[References]

https://www.geeksforgeeks.org/introduction-to-stack-data-structure-and-algorithm-tutorials/

https://github.com/kodecocodes/swift-algorithm-club/

'Computer Science > Data Structure' 카테고리의 다른 글

2. Queue (Linear Data Structure-FIFO)  (0) 2024.05.13
Data Structure & Algorithm Overview  (0) 2024.05.13