Advertisement
jovanovski

НЛ Лаб4

Nov 12th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. import java.util.Date;
  2. import java.util.LinkedList;
  3. import java.util.Scanner;
  4.  
  5. class Node<T> {
  6.     T element;
  7.     Node<T> next;
  8.     Node<T> prev;
  9.  
  10.     public Node(T element, Node<T> next, Node<T> prev) {
  11.         this.element = element;
  12.         this.next = next;
  13.         this.prev = prev;
  14.     }
  15. }
  16.  
  17. class Stack<T> {
  18.     Node<T> first;
  19.     Node<T> current;
  20.  
  21.     public Stack() {
  22.         first = null;
  23.         current = first;
  24.     }
  25.  
  26.     public void push(T element) {
  27.         if (first == null) {
  28.             first = new Node<T>(element, null, null);
  29.             current = first;
  30.         } else {
  31.             current.next = new Node<T>(element, null, current);
  32.             current = current.next;
  33.         }
  34.     }
  35.  
  36.     public T peek() {
  37.         return current.element;
  38.     }
  39.  
  40.     public T pop() {
  41.         if (current != null) {
  42.             T tmp = current.element;
  43.             if (current.prev != null) {
  44.                 current = current.prev;
  45.                 current.next = null;
  46.             } else {
  47.                 current = null;
  48.             }
  49.             return tmp;
  50.         }
  51.         return null;
  52.     }
  53.  
  54.     public boolean isEmpty() {
  55.         if (current == null)
  56.             return true;
  57.         return false;
  58.     }
  59. }
  60.  
  61. public class StackTest {
  62.  
  63.     /**
  64.      * @param args
  65.      */
  66.     public static void main(String[] args) {
  67.         // TODO Auto-generated method stub
  68.         Scanner jin = new Scanner(System.in);
  69.         int k = jin.nextInt();
  70.         if (k == 0) {
  71.             int n = jin.nextInt();
  72.             Stack<Integer> stack = new Stack<Integer>();
  73.             for (int i = 1; i <= n; ++i) {
  74.                 if (i > 1)
  75.                     System.out.print(" ");
  76.                 System.out.print(i);
  77.                 stack.push(i);
  78.             }
  79.             System.out.println();
  80.             for (int i = n; i >= 1; --i) {
  81.                 if (i < n)
  82.                     System.out.print(" ");
  83.                 System.out.print(stack.pop());
  84.             }
  85.             System.out.println();
  86.         }
  87.         if (k == 1) {
  88.             int n = jin.nextInt();
  89.             Stack<String> stack = new Stack<String>();
  90.             for (int i = 0; i < n; ++i) {
  91.                 if (i > 0)
  92.                     System.out.print(" ");
  93.                 String next = jin.next();
  94.                 System.out.print(next);
  95.                 stack.push(next);
  96.             }
  97.             System.out.println();
  98.             for (int i = 0; i < n; ++i) {
  99.                 if (i > 0)
  100.                     System.out.print(" ");
  101.                 System.out.print(stack.pop());
  102.             }
  103.         }
  104.         if (k == 2) {
  105.             Stack<Double> stack = new Stack<Double>();
  106.             while (jin.hasNextDouble()) {
  107.                 stack.push(jin.nextDouble());
  108.             }
  109.             int i = 0;
  110.             while (!stack.isEmpty()) {
  111.                 if (i > 0)
  112.                     System.out.print(" ");
  113.                 ++i;
  114.                 System.out.print(stack.pop());
  115.             }
  116.         }
  117.         if (k == 3) {
  118.             int n = jin.nextInt();
  119.             Stack<Long> stack = new Stack<Long>();
  120.             LinkedList<Long> control_stack = new LinkedList<Long>();
  121.             boolean exact = true;
  122.             for (int i = 0; exact && i < n; ++i) {
  123.                 if (Math.random() < 0.5) {// add
  124.                     long to_add = (long) (Math.random() * 456156168);
  125.                     stack.push(to_add);
  126.                     control_stack.addFirst(to_add);
  127.                 } else {
  128.                     exact &= control_stack.isEmpty() == stack.isEmpty();
  129.                     if (exact && !stack.isEmpty()) {
  130.                         if (Math.random() > 0.7)
  131.                             exact &= control_stack.removeFirst().equals(
  132.                                     stack.pop());
  133.                         else
  134.                             exact &= control_stack.peekFirst().equals(
  135.                                     stack.peek());
  136.                     }
  137.                 }
  138.             }
  139.             System.out.println(exact);
  140.         }
  141.         if (k == 4) {
  142.             Stack<Date> test_stack = new Stack<Date>();
  143.             System.out.println(test_stack.isEmpty());
  144.             try {
  145.                 test_stack.pop();
  146.             } catch (Exception e) {
  147.                 System.out.println(e.getClass().getSimpleName());
  148.             }
  149.             try {
  150.                 test_stack.peek();
  151.             } catch (Exception e) {
  152.                 System.out.println(e.getClass().getSimpleName());
  153.             }
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement