Home > Enterprise >  How to scan a stack without damaging the structure of the stack
How to scan a stack without damaging the structure of the stack

Time:05-17

The mission is to check if there's any number in the stack that his units digit is the same as the number I pick. And after I do that I need to print the same stack again, but when I checked for the units digit the stack remains empty. So I need your help to keep the stack as it was in the beginning. Btw the teacher said I can use a linked list to keep the stack as it was.

Here's the code:

   public static boolean isExist(int num,Stack <Integer> stk) {
       int count=0;
       while(!stk.isEmpty()) {
           
           if(stk.pop()==num) {
               
               count  ;
           }
           else {
               count=count;
           }
       }
       if(count>0) {
           return true;
       }
       else {
           return false;
       }
       
       
   }
   public static void main(String[] args) {
       Stack stk=new Stack();
       for(int i=0;i<10;i  ) {
           stk.push(i*24);
       }
       System.out.println(stk);
       System.out.println(isExist(8, stk));
       System.out.println(stk);
   }

}

CodePudding user response:

The idea is to take the elements from the stack and put them into the linked list. When our search is complete, we put them back in reverse order.

In order to be type-safe I added <Integer> to the stack and to the list.

import java.util.Stack;
import java.util.LinkedList;

public class StackCheck {
    public static boolean isExist(int num, Stack<Integer> stk) {
        LinkedList<Integer> list = new LinkedList<>();
        int count = 0;
        while (!stk.isEmpty()) {
            Integer element = stk.pop();
            list.addFirst(element); // add to front of list
            if (element % 10 == num) {
                count  ;
            }
            // Your else branch didn't do anything. Removed.
        }
        // Now put the popped elements back.
        // Start at front.
        for (int i = 0; i < list.size(); i  ) {
            stk.push(list.get(i));
        }

        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        Stack<Integer> stk = new Stack<>();
        for (int i = 0; i < 10; i  ) {
            stk.push(i * 24);
        }
        System.out.println(stk);
        System.out.println(isExist(8, stk));
        System.out.println(stk);
    }

}
$ java StackCheck.java
[0, 24, 48, 72, 96, 120, 144, 168, 192, 216]
true
[0, 24, 48, 72, 96, 120, 144, 168, 192, 216]
$

Run the code online at https://www.online-java.com/C341zqXmJ7

Version with improvements:

  • use a boolean variable and stop popping elements when matching element was found
  • use lambda function to restore stack
  • added variant with temporary stack as comments
import java.util.Stack;
import java.util.LinkedList;

public class StackCheck {
    public static boolean isExist(int num, Stack<Integer> stack) {
        LinkedList<Integer> list = new LinkedList<>(); // Stack<Integer> tmpStack = new Stack<>();
        boolean exists = false;
        while (!stack.isEmpty()) {
            int element = stack.pop();
            list.addFirst(element); // tmpStack.push(element);
            if (element % 10 == num) {
                exists = true;
                break;
            }
        }
        list.forEach(e -> stack.push(e)); // while (!tmpStack.isEmpty()) {stack.push(tmpStack.pop());} 
        return exists;
    }
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < 10; i  ) {
            stack.push(i * 24);
        }
        System.out.println(stack);
        System.out.println(isExist(8, stack));
        System.out.println(stack);
    }
}
  • Related