Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Write a stack class from scratch to house integers using any programming language of your choosing that implements the following methods:
- push(integer n) - adds the integer to top of the stack
- pop() - removes the top-most item from the stack
- peek() - gets the integer from top of the stack without removing it
- depth() - determines the number of integers in the stack
- If a number, then the number is pushed onto the stack
- If the minus character (-), a pop operation is performed against the stack
- If the question mark character (?), a peek operation is performed
- If the hash character (#), a depth operation is performed
- Inputs will not cause the stack to underflow
- The stack must support depth d, where d < 2,000,000 without overflowing
- -1048576000000 < n < 1048576000000
- Using a pre-existing stack class provided by the programming language, library, package, or framework is not acceptable
- For an integer token, output the depth of the stack after the push operation
- For a minus character (-), output the integer popped from the stack
- For a question mark character (?), output the element peeked from the stack
- For a hash character (#), output the depth of the stack
- Before any integer is pushed onto the stack, the stack is empty, and therefore has zero elements. Per the input format specification, and "0" should be printed.
- When token 34 is encountered, it is pushed onto the stack since it is an integer. Now stack has one element, and "1" should be printed.
- When token 25 is encountered, it is pushed ont othe stack as well. Stack now has two elements, and "2" should be printed.
- When token ? is encountered, it peeks at the stack, and prints "25" since that is the top-most element in the stack.
- When token -5 is encountered, it is pusehd onto the stack and "3" is printed.
- When token - is encountered, the top-most element is popped from the stack, and "-5" is printed
- When token # is encountered, "2" is printed because the stack now has two elements.
- When token ? is encountered, "25" is printed because it is at the top of the stack.
- import java.io.*;
- import java.util.*;
- import java.lang.*;
- // stack size<= 2000000
- // it should hold integers -1048576000000 < n < 1048576000000
- public class StackThemUp {
- private static LinkedList<Long> list = new LinkedList<>();
- @SuppressWarnings("resource")
- public static void main(String[] args) {
- // Scanner scan = new Scanner(System.in);
- int stackSize;
- long lastInt;
- // String userInput = scan.nextLine();
- // scan.close();
- String[] stackEntry = null;
- String line = "";
- // String userInput = scan.nextLine();
- // List<String> result = new ArrayList<>();
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
- // while (line != null) {
- line = reader.readLine();
- // stackEntry=line.split(" ");
- // }
- } catch (IOException exc) {
- }
- // initializeStack(stackEntry.length);
- for (String s : line.split(" ")) {
- try {
- if (s.equals("?")) {
- lastInt = peek();
- System.out.println(lastInt);
- } else if (s.equals("#")) {
- stackSize = depth();
- System.out.println(stackSize);
- } else if (s.equals("-")) {
- lastInt = peek();
- System.out.println(lastInt);
- pop();
- } else if (s.matches("[-]?\d+")) {
- push(Long.parseLong(s));
- stackSize = depth();
- System.out.println(stackSize);
- } else {
- throw new IllegalArgumentException();
- }
- } catch (IllegalArgumentException ex) {
- }
- }
- }
- private static void push(Long number) {
- list.add(number);
- // stackTop++;
- }
- private static Long peek() {
- return list.getLast();
- }
- private static int depth() {
- return list.size();
- }
- private static void pop() {
- if (list.size() > 0)
- list.removeLast();
- // stackTop--;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement