jweinst1
10/9/2015 - 4:27 AM

Basic stack operations in java as a collection.

Basic stack operations in java as a collection.

import java.util.*;

public class stacktest {

    public static void main(String[] args) {
        Stack thing = createstack(5, 6, 4, 3, 2);
        addtostack(thing, 7, 6, 5, 4, 3);
        System.out.println(thing.toString());
    }
    public static Stack createstack(int...values) {
        Stack pile = new Stack();
        for(int elem:values) {
            pile.push(elem);
        }
        return pile;
    }
    public static Stack addtostack(Stack container,int...values) {
        for(int elem: values) {
            container.push(elem);
        }
        return container;
    }
    public static boolean search(Stack container, int value) {
        while (container.size() > 0) {
            if (container.lastElement() == value) {
                return true;
            }
            container.pop();
        }
        return false;
    }


}