C++ STL-এ stack হলো LIFO (Last In First Out) কন্টেইনার। বইয়ের ঢেরের মত: শেষে যোগ করা উপাদানটি প্রথমে বের হয়।
প্রধান অপারেশন
| ফাংশন | ব্যবহার |
|---|---|
push(x) | এলিমেন্ট পুশ করুন (টপে) |
pop() | টপ এলিমেন্ট মুছে দিন |
top() | টপ এলিমেন্ট রিটার্ন করুন |
size() | এলিমেন্ট সংখ্যা |
empty() | খালি কিনা চেক করুন |
উদাহরণ
#include <bits/stdc++.h>
using namespace std;
int main() {
stack<int> st;
st.push(10); st.push(20); st.push(30); st.push(40);
if (!st.empty()) cout << "Stack size: " << st.size() << endl;
cout << "Elements: ";
while (!st.empty()) {
cout << st.top() << " ";
st.pop();
}
// Output: 40 30 20 10
}
আউটপুট
Stack size is: 4 Elements in the stack are: 40 30 20 10