import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
private int pointer;
private int[] stack;
private int size;
this(20);
}
public Stack(int capacity
) { size = capacity;
stack = new int[size];
pointer = -1;
}
public void push(int element) {
stack[++pointer] = element;
}
public int pop() {
int i = stack[pointer];
stack[pointer--] = 0;
return i;
}
public int back() {
return stack[pointer];
}
public void clear() {
pointer = -1;
}
public boolean isEmpty() {
if (pointer < 0)
return true;
else
return false;
}
public int size() {
return this.pointer + 1;
}
public static void main
(String[] args
) {
Scanner scanner
= new Scanner
(System.
in); System.
out.
println("enter your command"); String command
= scanner.
nextLine();
while (!command.equals("exit")) {
if (command.matches("^push [0-9]")) {
String[] comm
= command.
split(" "); stack.
push(Integer.
parseInt(comm
[1])); } else if (command.equals("pop")) {
if (stack.isEmpty()) {
System.
out.
println("Error. Stack is empty"); } else {
System.
out.
println(stack.
pop()); }
} else if (command.equals("size")) {
System.
out.
println(stack.
size()); } else if (command.equals("back")) {
if (stack.isEmpty()) {
System.
out.
println("Error. Stack is empty"); } else {
System.
out.
println(stack.
back()); }
} else if (command.equals("clear")) {
stack.clear();
} else {
System.
out.
println("Command doesn't exist"); }
command = scanner.nextLine();
}
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuQXJyYXlzOwppbXBvcnQgamF2YS51dGlsLlNjYW5uZXI7CgpjbGFzcyBTdGFjayB7CgogICAgcHJpdmF0ZSBpbnQgcG9pbnRlcjsKICAgIHByaXZhdGUgaW50W10gc3RhY2s7CiAgICBwcml2YXRlIGludCBzaXplOwoKCiAgICBwdWJsaWMgU3RhY2soKSB7CiAgICAgICAgdGhpcygyMCk7CiAgICB9CgogICAgcHVibGljIFN0YWNrKGludCBjYXBhY2l0eSkgewogICAgICAgICAgICBzaXplID0gY2FwYWNpdHk7CiAgICAgICAgICAgIHN0YWNrID0gbmV3IGludFtzaXplXTsKICAgICAgICAgICAgcG9pbnRlciA9IC0xOwogICAgfQoKICAgIHB1YmxpYyB2b2lkIHB1c2goaW50IGVsZW1lbnQpIHsKICAgICAgICBzdGFja1srK3BvaW50ZXJdID0gZWxlbWVudDsKICAgIH0KCiAgICBwdWJsaWMgaW50IHBvcCgpIHsKICAgICAgICBpbnQgaSA9IHN0YWNrW3BvaW50ZXJdOwogICAgICAgIHN0YWNrW3BvaW50ZXItLV0gPSAwOwogICAgICAgIHJldHVybiBpOwogICAgfQoKICAgIHB1YmxpYyBpbnQgYmFjaygpIHsKICAgICAgICByZXR1cm4gc3RhY2tbcG9pbnRlcl07CiAgICB9CgogICAgcHVibGljIHZvaWQgY2xlYXIoKSB7CiAgICAgICAgQXJyYXlzLmZpbGwoc3RhY2ssIDApOwogICAgICAgIHBvaW50ZXIgPSAtMTsKICAgIH0KCiAgICBwdWJsaWMgYm9vbGVhbiBpc0VtcHR5KCkgewogICAgICAgIGlmIChwb2ludGVyIDwgMCkKICAgICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgZWxzZQogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICB9CgogICAgcHVibGljIGludCBzaXplKCkgewogICAgICAgIHJldHVybiB0aGlzLnBvaW50ZXIgKyAxOwogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTdGFjayBzdGFjayA9IG5ldyBTdGFjaygpOwoKICAgICAgICBTY2FubmVyIHNjYW5uZXIgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiZW50ZXIgeW91ciBjb21tYW5kIik7CiAgICAgICAgU3RyaW5nIGNvbW1hbmQgPSBzY2FubmVyLm5leHRMaW5lKCk7CgogICAgICAgIHdoaWxlICghY29tbWFuZC5lcXVhbHMoImV4aXQiKSkgewogICAgICAgICAgICBpZiAoY29tbWFuZC5tYXRjaGVzKCJecHVzaCBbMC05XSIpKSB7CiAgICAgICAgICAgICAgICBTdHJpbmdbXSBjb21tID0gY29tbWFuZC5zcGxpdCgiICIpOwogICAgICAgICAgICAgICAgc3RhY2sucHVzaChJbnRlZ2VyLnBhcnNlSW50KGNvbW1bMV0pKTsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiT0siKTsKICAgICAgICAgICAgfSBlbHNlIGlmIChjb21tYW5kLmVxdWFscygicG9wIikpIHsKICAgICAgICAgICAgICAgIGlmIChzdGFjay5pc0VtcHR5KCkpIHsKICAgICAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkVycm9yLiBTdGFjayBpcyBlbXB0eSIpOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oc3RhY2sucG9wKCkpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9IGVsc2UgaWYgKGNvbW1hbmQuZXF1YWxzKCJzaXplIikpIHsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihzdGFjay5zaXplKCkpOwogICAgICAgICAgICB9IGVsc2UgaWYgKGNvbW1hbmQuZXF1YWxzKCJiYWNrIikpIHsKICAgICAgICAgICAgICAgIGlmIChzdGFjay5pc0VtcHR5KCkpIHsKICAgICAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkVycm9yLiBTdGFjayBpcyBlbXB0eSIpOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oc3RhY2suYmFjaygpKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfSBlbHNlIGlmIChjb21tYW5kLmVxdWFscygiY2xlYXIiKSkgewogICAgICAgICAgICAgICAgc3RhY2suY2xlYXIoKTsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiT0siKTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiQ29tbWFuZCBkb2Vzbid0IGV4aXN0Iik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY29tbWFuZCA9IHNjYW5uZXIubmV4dExpbmUoKTsKICAgICAgICB9CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJieWUiKTsKCiAgICB9Cn0K