mvCode

WindowsExplorer AIPS

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