Study/DataStructure
Stack
초록색이젤다
2025. 4. 21. 20:12
DataStructure/Header/Stack.h at e1c6dde7f506c76e6fc676a15f2f92fb46fda7cf · applejin0105/DataStructure
자료구조 연습. Contribute to applejin0105/DataStructure development by creating an account on GitHub.
github.com
특징
- 배열 기반의 스택 구조.
- 후입선출(LIFO) 방식으로 데이터 관리.
- 내부 용량 자동 확장 및 축소.
- C++ 스타일 반복자 지원 (begin(), end()) : Iterato
- Resize 함수로 동적 메모리 관리.
Push(val) | bool | 스택 맨 위에 값을 추가 |
Pop(out) | bool | 맨 위 값을 제거하고 out에 반환 |
Peek(out) | bool | 삭제 없이 맨 위 값을 확인 |
IsEmpty() | bool | 스택이 비어 있는지 여부 |
IsFull() | bool | 스택이 가득 찼는지 여부 |
Clear() | void | 모든 요소 제거 |
Size() | int | 현재 스택의 요소 수 |
Capacity() | int | 현재 메모리 용량 반환 |
operator[] | T& | 인덱스로 요소 접근 |
begin() / end() | Iterator | 반복자 반환 |
Test
#include <iostream>
#include "Stack.h"
using namespace std;
int main()
{
Stack<int> s;
cout << "[1] Push Test\n";
s.Push(10); s.Push(20); s.Push(30);
for (auto val : s) cout << val << " ";
cout << "\n";
cout << "[2] Peek Test\n";
int top;
if (s.Peek(top)) cout << "Peek: " << top << "\n";
cout << "[3] Pop Test\n";
int val;
s.Pop(val); cout << "Popped: " << val << "\n";
cout << "[4] IsEmpty / Size\n";
cout << "Size: " << s.Size() << ", IsEmpty: " << (s.IsEmpty() ? "Yes" : "No") << "\n";
cout << "[5] Clear Test\n";
s.Clear();
cout << "Size after clear: " << s.Size() << "\n";
}