The goal of Computer Science is to build something that will last at least until we have finished building it. Anonymous
Stack is a data structure store and remove the items based on last in and first out. Priority queues are implemented based on container.
![]() |
Last-in, first-out (LIFO) data structure. |
![]() |
The stack insert into the top and removed from the top. |
![]() |
Stack can be implemented using array or linked list (dynamic memory) |
![]() |
If the stack is full and does not contain enough space to accept an entity to be pushed, the stack is then considered to be in an overflow state. |
![]() |
If the stack is empty, it goes into underflow state. |
#include "stdafx.h" #include < iostream > #include < stack > using namespace std; int _tmain(int argc, _TCHAR* argv[]) { stack < int > st; cout << "Store items to stack." << endl; for (int i = 0; i < 10;i++) st.push(i); cout << "Stack size : " << st.size() << endl; cout << "Display items from stack : " << st.size() << endl; while (!st.empty()) { int w = st.top(); cout << w << " "; st.pop(); } cout << endl; return 0; }
Store items to stack. Stack size : 10 Display items from stack : 10 9 8 7 6 5 4 3 2 1 0
If you want insert and remove the items using last in first out (LIFO) order, you can use Stack.
0 Comments