# 스택 (Stack)

![](/files/-LDtu1_z87hA7gpuLXGa)

* LILO (Last-In-Last-Out)
* Stack Over Flow

  아이템 추가 시, 메모리 공간에 스택이 가득찼을 때 에러
* Stack Uner Flow\
  아이템 삭제 시, 더이상 삭제할 아이템이 없을 때

```javascript
class Stack {
    constructor() {
        this.dataStore = [];
        this.top = 0;
    }

    push (item){
        // 예를 들어 메모리에 최대 5개의 아이템이 들어갈 경우
        if (this.top < 3) {
            this.dataStore.push(item);
            this.top++;
        } else {
            // stack overflow 발생
            console.log("stack is full, adbort push to prevent stack overflow");
        }
    }

    pop (){
        if (this.top > 0) {
            this.dataStore.pop();
            this.top--;
        } else {
            // stack underflow 발생
            console.log("stack is emtpy, adbort pop to prevent stack underflow");
        }
    }

    peek (){
        return this.dataStore[this.top - 1];
    }

    length (){
        return this.top;
    }

    clear (){
        this.dataStore = [];
        this.top = 0;
    }

    print (){
        return this.dataStore.join(', ');
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.junojunho.com/algorithm-and-data-structure/data-structure/stack.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
