0% found this document useful (0 votes)
4 views

Notes

Uploaded by

Mayank Jindal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Notes

Uploaded by

Mayank Jindal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Stacks: Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new

element is added at one end (top) and an element is removed from that end only.
Syntax: stack<int> stack_name;
Stack Functions:
Function Definition Time Complexity Space Complexity
empty() Returns whether the stack is empty O(1) O(1)
size() Returns the size of the stack O(1) O(1)
top() Returns a reference to the topmost element O(1) O(1)
of the stack
push(element) Adds the element ‘element’ at the top of the O(1) O(1)
stack
pop() Deletes the most recent entered element of O(1) O(1)
the stack
swap() swap the contents of one stack with another O(1) O(1)
stack of same type but the size may vary
emplace(value) used to insert a new element into the stack O(1) O(1)
container, the new element is added on top
of the stack
Note: TC is for one single operation on a single element in Stack.
Stack Traversal:
while (!stack.empty()) {
cout << stack.top() <<" ";
stack.pop();
}

Queue: Queues are a type of container adaptor that operates in a first in first out (FIFO) type of
arrangement. Elements are inserted at the back (end) and are deleted from the front.
Syntax: queue<int> queue_name;
Queue Functions:
Function Definition Time Complexity Space Complexity
empty() Returns whether the queue is empty. It O(1) O(1)
return true if the queue is empty otherwise
it returns false.
size() Returns the size of the stack O(1) O(1)
top() Returns a reference to the topmost element O(1) O(1)
of the stack
push(element) Adds the element ‘element’ at the top of the O(1) O(1)
stack
pop() Deletes the most recent entered element of O(1) O(1)
the stack
swap() swap the contents of one stack with another O(1) O(1)
stack of same type but the size may vary
front() Returns a reference to the first element of O(1) O(1)
the queue.
back() Returns a reference to the last element of O(1) O(1)
the queue.
emplace(value) used to insert a new element into the stack O(1) O(1)
container, the new element is added on top
of the stack

You might also like