A Stack is a linear data structure that follows LIFO (Last In, First Out) principle. Think of it like a stack of plates—you add/remove from the top.
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack <int> s;
s.push(10);
s.push(20);
s.push(30);
cout << "Top: " << s.top() << endl; // 30
s.pop(); // removes 30
cout << "New Top: " << s.top() << endl; // 20
return 0;
}
A Queue is a linear data structure that follows FIFO (First In, First Out) principle. Think of it like a line at a ticket counter—first come, first served.
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
q.push(100);
q.push(200);
q.push(300);
cout << "Front: " << q.front() << endl; // 100
q.pop(); // removes 100
cout << "New Front: " << q.front() << endl; // 200
return 0;
}