package com.dw.algorithmlib;

import java.util.Iterator;
import java.util.ListIterator;
import java.util.Stack;

public class StackHelper {
	
	public void StackIterateExample() {
		Stack<String> stackOfPlates = new Stack<>();

        stackOfPlates.add("Plate 1");
        stackOfPlates.add("Plate 2");
        stackOfPlates.add("Plate 3");
        stackOfPlates.add("Plate 4");
        
        stackOfPlates.forEach(plate -> {
            System.out.println(plate);
        });
        
        System.out.println("\n=== Iterate over a Stack using iterator() ===");
        Iterator<String> platesIterator = stackOfPlates.iterator();
        while (platesIterator.hasNext()) {
            String plate = platesIterator.next();
            System.out.println(plate);
        }

        System.out.println("\n=== Iterate over a Stack using iterator() and Java 8 forEachRemaining() method ===");
        platesIterator = stackOfPlates.iterator();
        platesIterator.forEachRemaining(plate -> {
            System.out.println(plate);
        });
        
        System.out.println("\n=== Iterate over a Stack from TOP to BOTTOM using listIterator() ===");
        // ListIterator allows you to traverse in both forward and backward directions.
        // We'll start from the top of the stack and traverse backwards.
        ListIterator<String> platesListIterator = stackOfPlates.listIterator(stackOfPlates.size());
        while (platesListIterator.hasPrevious()) {
            String plate = platesListIterator.previous();
            System.out.println(plate);
        }
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StackHelper s = new StackHelper();
		s.StackIterateExample();
	}

}

@@ -135,16 +135,637 @@
 per %7B%0A%09%0A
+%09public boolean validateStackSequences(int%5B%5D pushed, int%5B%5D popped) %7B%0A%09%09if(popped.length == 0) return true;%0A%09%09Stack%3CInteger%3E stack = new Stack%3CInteger%3E();%0A%09%09int index = 0;%0A%09%09int i = 0;%0A%09%09while(true) %7B%0A%09%09%09if(stack.size() == 0) %7B%0A%09%09%09%09stack.push(pushed%5Bi++%5D);%0A%09%09%09%09continue;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09if(stack.peek() == popped%5Bindex%5D) %7B%0A%09%09%09%09stack.pop();%0A%09%09%09%09index++;%0A%09%09%09%7D else %7B%0A%09%09%09%09%0A%09%09%09%09if(i %3E= pushed.length) break;%0A%09%09%09%09%0A%09%09%09%09stack.push(pushed%5Bi++%5D);%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09if(index %3E= popped.length) %7B%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09System.out.println(index);%0A%09%09System.out.println(i);%0A%09%09%0A%09%09if(stack.size() == 0) return true;%0A%09%09%0A%09%09return false;%0A%09%09%0A    %7D%0A%09%0A
 %09public 
@@ -2339,14 +2339,168 @@
 mple();%0A
+%09%09%0A%09%09int%5B%5D pushed = %7B1,2,3,4,5%7D;%0A%09%09int%5B%5D popped = %7B4,3,5,2,1%7D;%0A%09%09boolean result = s.validateStackSequences(pushed, popped);%0A%09%09System.out.println(result);%0A
 %09%7D%0A%0A%7D%0A

package validateStackSequences as lib