L2. S - Data Structures and Algorithms
L2. S - Data Structures and Algorithms
Stacks
In C++, a stack can be implemented using the Standard Template Library (STL) stack
container, which follows the LIFO principle.
Example:
cpp
#include <iostream>
#include <stack> // Required for usingstack
using namespcace std;
// This line tells the compiler to use the `std` namespace for
all standard library elements.
// This avoids having to write `std::` before every standard
library component we use.
using namespace std;
int main() {
// Create a stack to store integers
stack<int> myStack;
myStack.push(10);
myStack.push(20);
myStack.push(30);
return 0;
}
Key Operations:
3. Queues
In C++, a queue can be implemented using the STL queue container, which follows the
FIFO principle.
Example:
cpp
#include <iostream>
#include <queue> // Required for using std::queue
int main() {
// Create a queue to store integers
queue<int> myQueue;
return 0;
}
Key Operations: