Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Stacki {
- public static void main(String[] args) throws IOException {
- Stack stack = new Stack(5000);
- BufferedReader in = new BufferedReader(new InputStreamReader (new FileInputStream(new File("input.txt"))));
- StreamTokenizer input = new StreamTokenizer(in);
- PrintWriter out = new PrintWriter(new File("output.txt"));
- while (true) {
- input.nextToken();
- String a = input.sval;
- if (a.equals("push")) {
- input.nextToken();
- stack.push((int) input.nval);
- out.println("ok");
- }
- if (a.equals("front")) {
- if (stack.size()==0){
- out.println("error");
- } else {
- out.println(stack.top()) ;
- }
- }
- if (a.equals("size")) {
- out.println(stack.size()) ;
- }
- if (a.equals("clear")) {
- stack.clear() ;
- out.println("ok") ;
- }
- if (a.equals("exit")) {
- out.println("bye") ;
- out.close();
- return;
- }
- if (a.equals("pop")) {
- if (stack.size()==0){
- out.println("error");
- } else {
- out.println(stack.pop()) ;
- }
- }
- }
- }
- }
- class Stack {
- int[] stack;
- int head = 0;
- int tail = 0;
- public Stack(int length){
- stack = new int[length];
- }
- void clear() {
- head = tail;
- }
- int size() {
- return tail-head;
- }
- int top() {
- return stack[head%stack.length] ;
- }
- int pop(){
- head++;
- return stack[(head-1)%stack.length];
- }
- void push(int n) {
- stack[tail%stack.length] = n;
- tail++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement