diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-14 02:03:23 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-14 02:03:23 +0000 |
| commit | b7eaedf3966c6e55b67a90af662a396ca524b5c0 (patch) | |
| tree | 4adde59a10d348d5ec5d3bd21e0dae62cbd9b9aa /challenge-095/ziameraj16/java/DemoStack.java | |
| parent | 64c688637441ca8ef6a86fa7df5d51a8363f0af9 (diff) | |
| parent | e890ff8e60d665c217d032de328c687f246a5b69 (diff) | |
| download | perlweeklychallenge-club-b7eaedf3966c6e55b67a90af662a396ca524b5c0.tar.gz perlweeklychallenge-club-b7eaedf3966c6e55b67a90af662a396ca524b5c0.tar.bz2 perlweeklychallenge-club-b7eaedf3966c6e55b67a90af662a396ca524b5c0.zip | |
Merge pull request #3249 from ziameraj16/ziameraj16-challenge-095
Java solution for Challenge 095
Diffstat (limited to 'challenge-095/ziameraj16/java/DemoStack.java')
| -rw-r--r-- | challenge-095/ziameraj16/java/DemoStack.java | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/challenge-095/ziameraj16/java/DemoStack.java b/challenge-095/ziameraj16/java/DemoStack.java new file mode 100644 index 0000000000..1fce9e3d83 --- /dev/null +++ b/challenge-095/ziameraj16/java/DemoStack.java @@ -0,0 +1,88 @@ +import java.util.*; + +public class DemoStack { + + private static Scanner scanner = new Scanner(System.in); + + private static final int MAX_SIZE = 1000; + int arr[] = new int[MAX_SIZE]; + int top = -1; + + public static void main(String[] args) { + DemoStack demoStack = new DemoStack(); + System.out.println("Stack Example"); + while (true) { + iterate(demoStack); + } + } + + private static void iterate(DemoStack demoStack) { + System.out.println(String.join("\n", + "\n####################################", + "To push into stack - `push <number>`", + "To pop from the stack - `pop`", + "To find the top element - `top`", + "To find the minimum element - `min`", + "To exit - `exit`", + "####################################\n")); + + final String[] options = scanner.nextLine().toLowerCase().split(" "); + if (options[0].equals("push")) { + demoStack.push(Integer.parseInt(options[1])); + } else if (options[0].equals("pop")) { + demoStack.pop(); + } else if (options[0].equals("top")) { + demoStack.top(); + } else if (options[0].equals("min")) { + demoStack.min(); + } else if (options[0].equals("exit")) { + System.out.println("Bye!!!"); + System.exit(1); + } else { + System.out.println("Invalid option: " + options[0]); + } + } + + public boolean push(int number) { + if (top == MAX_SIZE - 1) { + System.err.println("Stack is full"); + return false; + } + arr[++top] = number; + System.out.println(String.format("Number %s added to the stack", number)); + return true; + } + + public int pop() { + if (top < 0) { + System.err.println("No element to pop"); + return Integer.MIN_VALUE; + } + int num = arr[top--]; + System.out.println("Element popped: " + num); + return num; + } + + public int top() { + if (top < 0) { + System.err.println("Stack is empty"); + return Integer.MIN_VALUE; + } + int num = arr[top]; + System.out.println("Top element: " + num); + return num; + } + + public int min() { + if (top < 0) { + System.err.println("Stack is empty"); + return Integer.MIN_VALUE; + } + final int[] copyArr = Arrays.copyOf(arr, top + 1); + Arrays.sort(copyArr); + int min = copyArr[0]; + System.out.println("Minimum number in stack: " + min); + return min; + } +} + |
