Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Date;
- import java.util.LinkedList;
- import java.util.Scanner;
- class Node<T> {
- T element;
- Node<T> next;
- Node<T> prev;
- public Node(T element, Node<T> next, Node<T> prev) {
- this.element = element;
- this.next = next;
- this.prev = prev;
- }
- }
- class Stack<T> {
- Node<T> first;
- Node<T> current;
- public Stack() {
- first = null;
- current = first;
- }
- public void push(T element) {
- if (first == null) {
- first = new Node<T>(element, null, null);
- current = first;
- } else {
- current.next = new Node<T>(element, null, current);
- current = current.next;
- }
- }
- public T peek() {
- return current.element;
- }
- public T pop() {
- if (current != null) {
- T tmp = current.element;
- if (current.prev != null) {
- current = current.prev;
- current.next = null;
- } else {
- current = null;
- }
- return tmp;
- }
- return null;
- }
- public boolean isEmpty() {
- if (current == null)
- return true;
- return false;
- }
- }
- public class StackTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner jin = new Scanner(System.in);
- int k = jin.nextInt();
- if (k == 0) {
- int n = jin.nextInt();
- Stack<Integer> stack = new Stack<Integer>();
- for (int i = 1; i <= n; ++i) {
- if (i > 1)
- System.out.print(" ");
- System.out.print(i);
- stack.push(i);
- }
- System.out.println();
- for (int i = n; i >= 1; --i) {
- if (i < n)
- System.out.print(" ");
- System.out.print(stack.pop());
- }
- System.out.println();
- }
- if (k == 1) {
- int n = jin.nextInt();
- Stack<String> stack = new Stack<String>();
- for (int i = 0; i < n; ++i) {
- if (i > 0)
- System.out.print(" ");
- String next = jin.next();
- System.out.print(next);
- stack.push(next);
- }
- System.out.println();
- for (int i = 0; i < n; ++i) {
- if (i > 0)
- System.out.print(" ");
- System.out.print(stack.pop());
- }
- }
- if (k == 2) {
- Stack<Double> stack = new Stack<Double>();
- while (jin.hasNextDouble()) {
- stack.push(jin.nextDouble());
- }
- int i = 0;
- while (!stack.isEmpty()) {
- if (i > 0)
- System.out.print(" ");
- ++i;
- System.out.print(stack.pop());
- }
- }
- if (k == 3) {
- int n = jin.nextInt();
- Stack<Long> stack = new Stack<Long>();
- LinkedList<Long> control_stack = new LinkedList<Long>();
- boolean exact = true;
- for (int i = 0; exact && i < n; ++i) {
- if (Math.random() < 0.5) {// add
- long to_add = (long) (Math.random() * 456156168);
- stack.push(to_add);
- control_stack.addFirst(to_add);
- } else {
- exact &= control_stack.isEmpty() == stack.isEmpty();
- if (exact && !stack.isEmpty()) {
- if (Math.random() > 0.7)
- exact &= control_stack.removeFirst().equals(
- stack.pop());
- else
- exact &= control_stack.peekFirst().equals(
- stack.peek());
- }
- }
- }
- System.out.println(exact);
- }
- if (k == 4) {
- Stack<Date> test_stack = new Stack<Date>();
- System.out.println(test_stack.isEmpty());
- try {
- test_stack.pop();
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- try {
- test_stack.peek();
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement