When working with data in programming, we need efficient ways to store and manage it. Two of the most commonly used data structures are Queue and Stack. In this post, we’ll explore what they are, how they work, and where they are used in real-world applications.
1️⃣ What is a Queue?
A Queue follows the FIFO (First In, First Out) principle, meaning the first element added is the first one to be removed. Imagine waiting in line for a bus—whoever arrives first gets on first.
✅ Key Features of a Queue
- Data is processed in the order it arrives.
- The first item added is the first to be removed.
- Works like a waiting line in real life.
🛠 Queue Operations
enqueue() | Adds an element to the rear of the queue. |
dequeue() | Removes an element from the front of the queue. |
📌 Real-World Applications of a Queue
- Print Queue: The first document sent to the printer gets printed first.
- Network Packet Processing: The first data packet received is processed first.
- BFS (Breadth-First Search) Algorithm: Used in graph traversal.
2️⃣ What is a Stack?
A Stack follows the LIFO (Last In, First Out) principle, meaning the last element added is the first one to be removed. Think of a stack of plates—whichever plate is placed last will be the first one taken.
✅ Key Features of a Stack
- The last item added is the first to be removed.
- Works like a pile of books or plates.
🛠 Stack Operations
push() | Adds an element to the top of the stack. |
pop() | Removes the top element from the stack. |
📌 Real-World Applications of a Stack
- Function Calls (Call Stack): The most recently called function is the first to complete.
- Web Browser History: The most recently visited page is the first one to go back to.
- DFS (Depth-First Search) Algorithm: Used in graph traversal.
🔥 Key Differences Between Queue and Stack
Order | FIFO (First In, First Out) | LIFO (Last In, First Out) |
Real-World Example | Waiting in line for a bus | Stack of plates |
Usage | Task scheduling, BFS, real-time processing | Function calls, undo/redo, DFS |
🚀 Conclusion
Both Queue and Stack are fundamental data structures that help in efficiently managing data. Understanding their principles and applications can improve your problem-solving skills in programming.
'프로그래밍' 카테고리의 다른 글
Bilinear Interpolation (양선형 보간) 이란? (0) | 2025.04.03 |
---|---|
YUV422란? (0) | 2025.04.02 |
큐(Queue), 스택(Stack)의 개념과 차이점 (0) | 2025.03.27 |
(빅데이터 분석기사) 1. 실기 학습 준비 및 교재 선정 (0) | 2022.04.06 |