Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Stacks {
- public static void main(String [] args) throws Exception {
- new Stacks();
- }
- BufferedReader in;
- PrintWriter out;
- StringTokenizer st;
- int[] s = new int[101];
- int len = 0;
- String command = "";
- private String next() throws Exception {
- if (st == null || !st.hasMoreElements())
- st = new StringTokenizer(in.readLine());
- return st.nextToken();
- }
- private int nextInt() throws Exception {
- return Integer.parseInt(next());
- }
- public Stacks() throws Exception {
- in = new BufferedReader(new FileReader("input.txt"));
- out = new PrintWriter(new FileWriter("output.txt"));
- solve();
- out.close();
- }
- private void solve() throws Exception {
- while (true) {
- command = next();
- if (command.equals("exit")) {
- out.println("bye");
- break;
- }
- if (command.equals("push")) {
- int val = nextInt();
- push(val);
- } else if (command.equals("pop")) {
- pop();
- } else if (command.equals("back")) {
- back();
- } else if (command.equals("size")) {
- size();
- } else if (command.equals("clear")) {
- clear();
- }
- }
- }
- private void push(int n) {
- s[len] = n;
- len++;
- out.println("ok");
- }
- private void pop() {
- int res = s[len - 1];
- len--;
- out.println(res);
- }
- private void back() {
- out.println(s[len - 1]);
- }
- private void size() {
- out.println(len);
- }
- private void clear() {
- len = 0;
- out.println("ok");
- }
- }
Add Comment
Please, Sign In to add comment