java stack remove elements which equals the top element

The solution for “java stack remove elements which equals the top element” can be found here. The following code will assist you in solving the problem.

public static void removeElem(Stack stack) {
Stack newStack = new Stack(7); //7 is a size of stack
int temp = stack.readTop();
int e;
newStack.push(temp); //we dont remove first element

while(!stack.isEmpty()) {
e = stack.pop();

if (e != temp) {
newStack.push(e);
}
}

while (!newStack.isEmpty()) {
e = newStack.pop();
stack.push(e);
}
}

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in