#include <iostream>
#include <stack>
int main() {
std::stack<int> myStack;
// Push elements onto the stack
myStack.push(10);
myStack.push(20);
myStack.push(30);
// Access the top element
std::cout << "Top element: " << myStack.top() << std::endl;
// Pop the top element
myStack.pop();
// Check if the stack is empty
if (myStack.empty()) {
std::cout << "Stack is empty" << std::endl;
} else {
std::cout << "Stack size: " << myStack.size() << std::endl;
}
return 0;
}
queue
#include <iostream>
#include <queue>
int main() {
std::queue<int> myQueue;
// Push elements into the queue
myQueue.push(10);
myQueue.push(20);
myQueue.push(30);
// Access the front element
std::cout << "Front element: " << myQueue.front() << std::endl;
// Access the back element
std::cout << "Back element: " << myQueue.back() << std::endl;
// Pop the front element
myQueue.pop();
// Check if the queue is empty
if (myQueue.empty()) {
std::cout << "Queue is empty" << std::endl;
} else {
std::cout << "Queue size: " << myQueue.size() << std::endl;
}
return 0;
}