Stack - Notes
Stack - Notes
Java Collection framework provides a Stack class that models and implements a Stack
data structure.
basic principle of last-in-first-out.
basic push and pop operations
3 more functions: empty, search, and peek.
The class can also extends Vector
The class can also be referred to as the subclass of Vector.
The hierarchy of the Stack class:
To Create a Stack:
Stack stk = new Stack();
Or
Stack<E> stack = new Stack<E>();
push(E item) E The method pushes (insert) an element onto the top of the
stack.
pop() E The method removes an element from the top of the stack
and returns the same element as the value of that function.
peek() E The method looks at the top element of the stack without
removing it.
search(Object int The method searches the specified object and returns the
o) position of the object.
Iterate Elements
Iterate means to fetch the elements of the stack. We can fetch elements of the stack using three
different methods are as follows:
o Using iterator() Method
o Using forEach() Method
o Using listIterator() Method
case '*':
case '/':
return op2 == '^' || op2 == '(';
case '^':
return op2 == '(';
case '(':
return true;
default:
return false;
}
}
if (!isOperator(c))
{
postfix.append(c);
}
else
{
if (c == ')')
{
else
{
if (!stack.isEmpty() && !isLowerPrecedence(c, stack.peek()))
{
stack.push(c);
}
else
{
while (!stack.isEmpty() && isLowerPrecedence(c, stack.peek()))
{
Character pop = stack.pop();
if (pop != '(')
{
postfix.append(pop);
}
}
}
stack.push(c);
}
}
}
return postfix.toString();
}