Stack
- LIFO - Last In First Out
- stacks are implemented as containers adaptors
- default underlying container is deque
container classes that could be used as underlying container are vector, deque, list and any class that implements the following methods
- empty
- size
- back
- push_back
- pop_back
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #include <stack>
stack<int> st;
st.push(1); st.push(2); st.push(3); st.push(4);
cout << "top = " << st.top() << "\n";
cout << "size = " << st.size() << "\n";
st.pop();
if (st.empty()) cout << "stack is empty" << "\n"; else cout << "stack is not empty" << "\n";
while(!st.empty()) { cout << st.top() << "\n"; st.pop(); }
|
Queue
- FIFO - First In First Out
- queues are implemented as container adaptors
- default underlying container is deque
container classes that could be used as underlying container are deque, list and any class that implements the following methods
- empty
- size
- front
- back
- push_back
- pop_back
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include <queue>
queue<int> q;
q.push(1); q.push(2); q.push(3); q.push(4);
cout << "front = " << q.front() << "\n";
cout << "back = " << q.back() << "\n";
cout << "size = " << q.size() << "\n";
q.pop();
if (q.empty()) cout << "queue is empty" << "\n"; else cout << "queue is not empty" << "\n";
while (!q.empty()) { cout << q.front() << "\n"; q.pop(); }
|
References