Working with LinkedList [Java] | Free IT Certifications Data Structures
Solving LinkedList questions using two pointers
Working with LinkedList [Java] | Free IT Certifications Data Structures |
Description
stack: The
stack contains elements of the same data type.
You can insert and remove elements from the stack with a last-in-first-out (LIFO) policy.
declaration
stack variable_name = new Stackandlt;>();
stack stack = new Stackandlt;>();
method used in stack:
stack_name . push() - inserts an element into the stack
stack_name. peek() - Peek at the top element of the stack
stack_name. pop() - Removes the top element of the stack
stack_name. size() - Returns the number of elements in the stack
stack_name. empty() - returns true/false. Returns true if the stack is empty. If the stack is not empty, false
Stack_Name is returned. clear() - Clear the stack
stack_name. contains(value) - Returns true if the value you are checking exists on the stack, returns false.
stack_name remove(index) – Removes the value at the specified index
stack_name. search(value) - Finds a value in the stack and returns the index of the value, if any.
Iterate Stack: You can iterate over the
stack elements in two ways.
1. Pop elements
You can pop stack elements one by one to iterate over all elements.
2. For each loop
You can iterate over the stack with a for loop like this
for(String s : stack_name)
{
//Stack is iterated from bottom to top like this
}
Problem
1. If you want parentheses or expressions to be evaluated in a particular order, you can use the stack.
2. If you want to use the backspace key on the keyboard or similar situations, you can use stack
3. If you want to clear or delete recent items, you can use Stack.
. If you want to go back to something and then go forward many times, you can use Stack.
5. Stack can be used to find some peak elements (assuming the displayed values are something like chart). You can use stack.
Who should take this course:
Developers learning data structures and algorithms
Don’t miss any coupons by joining our Telegram channel
DISCLOSURE: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.
Note : Coupons might expire anytime, so enroll as soon as possible to get the courses for FREE or Huge discount