Crazy

Windows Explorer

Dec 3rd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.55 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.*;
  4.  
  5. interface Tree<E> {
  6.     ////////////Accessors ////////////
  7.  
  8.     public Node<E> root();
  9.  
  10.     public Node<E> parent(Node<E> node);
  11.  
  12.     public int childCount(Node<E> node);
  13.  
  14.     ////////////Transformers ////////////
  15.     public void makeRoot(E elem);
  16.  
  17.     public Node<E> addChild(Node<E> node, E elem);
  18.  
  19.     public void remove(Node<E> node);
  20.  
  21.     ////////////Iterator ////////////
  22.     public Iterator<E> children(Node<E> node);
  23.  
  24. }
  25.  
  26. interface Node<E> {
  27.  
  28.     public E getElement();
  29.  
  30.     public void setElement(E elem);
  31. }
  32.  
  33.  
  34. class SLLTree<E> implements Tree<E> {
  35.  
  36.     public SLLNode<E> root;
  37.  
  38.     public SLLTree() {
  39.         root = null;
  40.     }
  41.  
  42.     public Node<E> root() {
  43.         return root;
  44.     }
  45.  
  46.     public Node<E> parent(Node<E> node) {
  47.         return ((SLLNode<E>) node).parent;
  48.     }
  49.  
  50.     public int childCount(Node<E> node) {
  51.         SLLNode<E> tmp = ((SLLNode<E>) node).firstChild;
  52.         int num = 0;
  53.         while (tmp != null) {
  54.             tmp = tmp.sibling;
  55.             num++;
  56.         }
  57.         return num;
  58.     }
  59.  
  60.     public void makeRoot(E elem) {
  61.         root = new SLLNode<E>(elem);
  62.     }
  63.  
  64.     public Node<E> addChild(Node<E> node, E elem) {
  65.         SLLNode<E> tmp = new SLLNode<E>(elem);
  66.         SLLNode<E> curr = (SLLNode<E>) node;
  67.         tmp.sibling = curr.firstChild;
  68.         curr.firstChild = tmp;
  69.         tmp.parent = curr;
  70.         return tmp;
  71.     }
  72.     public Node<E> addChild(Node<E> node, SLLNode<E> tmp) {
  73.         SLLNode<E> curr = (SLLNode<E>) node;
  74.         tmp.sibling = curr.firstChild;
  75.         curr.firstChild = tmp;
  76.         tmp.parent = curr;
  77.         return tmp;
  78.     }
  79.     public void remove(Node<E> node) {
  80.         SLLNode<E> curr = (SLLNode<E>) node;
  81.         if (curr.parent != null) {
  82.             if (curr.parent.firstChild == curr) {
  83.                 // The node is the first child of its parent
  84.                 // Reconnect the parent to the next sibling
  85.                 curr.parent.firstChild = curr.sibling;
  86.             } else {
  87.                 // The node is not the first child of its parent
  88.                 // Start from the first and search the node in the sibling list and remove it
  89.                 SLLNode<E> tmp = curr.parent.firstChild;
  90.                 while (tmp.sibling != curr) {
  91.                     tmp = tmp.sibling;
  92.                 }
  93.                 tmp.sibling = curr.sibling;
  94.             }
  95.         } else {
  96.             root = null;
  97.         }
  98.     }
  99.  
  100.     class SLLTreeIterator<T> implements Iterator<T> {
  101.  
  102.         SLLNode<T> start, current;
  103.  
  104.         public SLLTreeIterator(SLLNode<T> node) {
  105.             start = node;
  106.             current = node;
  107.         }
  108.  
  109.         public boolean hasNext() {
  110.             return (current != null);
  111.         }
  112.  
  113.         public T next() throws NoSuchElementException {
  114.             if (current != null) {
  115.                 SLLNode<T> tmp = current;
  116.                 current = current.sibling;
  117.                 return tmp.getElement();
  118.             } else {
  119.                 throw new NoSuchElementException();
  120.             }
  121.         }
  122.  
  123.         public void remove() {
  124.             if (current != null) {
  125.                 current = current.sibling;
  126.             }
  127.         }
  128.     }
  129.  
  130.     public Iterator<E> children(Node<E> node) {
  131.         return new SLLTreeIterator<E>(((SLLNode<E>) node).firstChild);
  132.     }
  133.  
  134.     void printTreeRecursive(Node<E> node, int level) {
  135.         if (node == null)
  136.             return;
  137.         int i;
  138.         SLLNode<E> tmp;
  139.  
  140.         for (i=0;i<level;i++)
  141.             System.out.print(" ");
  142.         System.out.println(node.getElement().toString());
  143.         tmp = ((SLLNode<E>)node).firstChild;
  144.         while (tmp != null) {
  145.             printTreeRecursive(tmp, level+1);
  146.             tmp = tmp.sibling;
  147.         }
  148.     }
  149.  
  150.     public void printTree() {
  151.         printTreeRecursive(root, 0);
  152.     }
  153.  
  154. }
  155.  
  156. class SLLNode<P> implements Node<P> {
  157.  
  158.     // Holds the links to the needed nodes
  159.     public SLLNode<P> parent, sibling, firstChild;
  160.     // Hold the data
  161.     public P element;
  162.  
  163.     public SLLNode(P o) {
  164.         element = o;
  165.         parent = sibling = firstChild = null;
  166.     }
  167.  
  168.     public P getElement() {
  169.         return element;
  170.     }
  171.  
  172.     public void setElement(P o) {
  173.         element = o;
  174.     }
  175. }
  176.  
  177. public class WindowsExplorer {
  178.  
  179.     public static void main(String[] args) throws Exception {
  180.         int i,j,k;
  181.  
  182.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  183.  
  184.         int N = Integer.parseInt(br.readLine());
  185.         String commands[] = new String[N];
  186.  
  187.         for (i=0;i<N;i++)
  188.             commands[i] = br.readLine();
  189.  
  190.         br.close();
  191.  
  192.         SLLTree<String> tree = new SLLTree<String>();
  193.         tree.makeRoot("c:");
  194.  
  195.         // vasiot kod stoi ovde
  196.         SLLNode<String> opened=tree.root;
  197.         for(int q=0;q<commands.length;q++){
  198.             String [] pom=commands[q].split("\\s+");
  199.             if(pom[0].equals("CREATE")){
  200.                 tree.addChild(opened,pom[1]);
  201.                 ArrayList<String> lista=new ArrayList<>();
  202.                 SLLNode<String> pomosen=opened.firstChild;
  203.                 while(pomosen!=null){
  204.                     lista.add(pomosen.element);
  205.                     pomosen=pomosen.sibling;
  206.                 }
  207.                 Collections.sort(lista);
  208.                 for(int w=lista.size()-1;w>=0;w--){
  209.                     SLLNode<String> pomosen1;
  210.                     pomosen1=null;
  211.                      pomosen=opened.firstChild;
  212.                      while(pomosen!=null){
  213.                          if(pomosen.element.equals(lista.get(w))){
  214.                              pomosen1=pomosen;
  215.                              tree.remove(pomosen);
  216.                              break;
  217.                          }
  218.                          pomosen=pomosen.sibling;
  219.                      }
  220.                     tree.addChild(opened,pomosen1);
  221.                 }
  222.  
  223.             }
  224.             else if(pom[0].equals("OPEN")){
  225.                 SLLNode<String> pomosen=opened.firstChild;
  226.                 while(pomosen!=null){
  227.                     if(pomosen.element.equals(pom[1])){
  228.                         opened=pomosen;
  229.                         break;
  230.                     }
  231.                     pomosen=pomosen.sibling;
  232.                 }
  233.  
  234.  
  235.             }
  236.             else if(pom[0].equals("DELETE")){
  237.                 SLLNode<String> pomosen=opened.firstChild;
  238.                 while(pomosen!=null){
  239.                     if(pomosen.element.equals(pom[1])){
  240.                         tree.remove(pomosen);
  241.                         break;
  242.                     }
  243.                     pomosen=pomosen.sibling;
  244.                 }
  245.             }
  246.             else if(pom[0].equals("BACK")){
  247.                 if(tree.parent(opened)!=null)
  248.                 opened=(SLLNode<String>)tree.parent(opened);
  249.             }
  250.             else if(pom[0].equals("PATH")){
  251.                 ArrayList<String> rez=new ArrayList<>();
  252.                 SLLNode<String> pomosen=opened;
  253.                 while(pomosen!=null){
  254.                     rez.add(pomosen.element);
  255.                     pomosen=(SLLNode<String>)tree.parent(pomosen);
  256.                 }
  257.                 Collections.reverse(rez);
  258.                 for(int w=0;w<rez.size();w++){
  259.                     System.out.print(rez.get(w)+"\\");
  260.                 }
  261.                 System.out.println();
  262.             }
  263.             else if(pom[0].equals("PRINT")){
  264.                 tree.printTree();
  265.             }
  266.  
  267.  
  268.         }
  269.  
  270.     }
  271.  
  272. }
Add Comment
Please, Sign In to add comment