Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

java stack remove elements which equals the top element

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); 
        }
    }
 
PREVIOUS NEXT
Tagged: #java #stack #remove #elements #equals #top #element
ADD COMMENT
Topic
Name
8+4 =