Stack
A stack is a linear data structure in which the insertion of a new element and removal of an existing element takes place at the same end represented as the top of the stack.
The stack data structure follows the LIFO (Last In First Out) principle.
LIFO( Last In First Out )
This strategy states that the element that is inserted last will come out first. You can take a pile of plates kept on top of each other as a real-life example. The plate which we put last is on the top and since we remove the plate that is at the top, we can say that the plate that was put last comes out first.
Syntax
Type : is the Type of element contained in the std::stack. It can be any valid C++ type or even a user-defined type.
Container : is the Type of underlying container object.
value_type: The first template parameter, T. It denotes the element types.
container_type: The second template parameter, Container. It denotes the underlying container type.
size_type: Unsigned integral type.
Basic Operations on Stack
Method | Definition |
---|---|
push() | To insert an element into the stack. |
pop() | To remove an element from the stack. |
top() | Returns the top element of the stack. |
empty() | Returns true if stack is empty else false. |
size() | Returns the size of stack. |
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <iostream> #include <stack> using namespace std; int main() { stack<int> stack; stack.push(21);// The values pushed in the stack should be of the same data which is written during declaration of stack stack.push(22); stack.push(24); stack.push(25); int num=0; stack.push(num); stack.pop(); stack.pop(); stack.pop(); while (!stack.empty()) { cout << stack.top() <<" "; stack.pop(); } } |
Output:
1 | 22 21 |
Queue
Queue is a linear data structure which follows FIFO (First In First Out) method.
FIFO (First In First Out)
FIFO is an abbreviation for first in, first out. It is a method for handling data structures where the first element is processed first and the newest element is processed last.
Syntax
Type : is the Type of element contained in the std::queue. It can be any valid C++ type or even a user-defined type.
Container : is the Type of underlying container object.
value_type: The first template parameter, T. It denotes the element types.
container_type: The second template parameter, Container. It denotes the underlying container type.
size_type: Unsigned integral type.
Basic Operations on Queue
Method | Definition |
---|---|
push() | Adds the element ‘g’ at the end of the queue. |
pop() | Deletes the first element of the queue. |
front() | Returns a reference to the first element of the queue. |
back() | Returns a reference to the last element of the queue. |
empty() | Returns true if stack is empty else false. |
size() | Returns the size of stack. |
Ex:
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 36 37 38 39 40 41 42 43 44 45 46 47 | #include <iostream> #include <queue> using namespace std; // Print the queue void print_queue(queue<int> q) { queue<int> temp = q; while (!temp.empty()) { cout << temp.front()<<" "; temp.pop(); } cout << '\n'; } // Driver Code int main() { queue<int> q1; q1.push(1); q1.push(2); q1.push(3); cout << "The first queue is : "; print_queue(q1); queue<int> q2; q2.push(4); q2.push(5); q2.push(6); cout << "The second queue is : "; print_queue(q2); q1.swap(q2); cout << "After swapping, the first queue is : "; print_queue(q1); cout << "After swapping the second queue is : "; print_queue(q2); cout<<q1.empty(); //returns false since q1 is not empty return 0; } |
Output:
1 2 3 4 5 | The first queue is : 1 2 3 The second queue is : 4 5 6 After swapping, the first queue is : 4 5 6 After swapping the second queue is : 1 2 3 0 |