Advertisement
DajanaS

Windows Explorer

Nov 28th, 2015
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.31 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.StringTokenizer;
  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.     public Iterator<E> children(Node<E> node) {
  97.         return new SLLTreeIterator<E>(((SLLNode<E>) node).firstChild);
  98.     }
  99.  
  100.     void printTreeRecursive(Node<E> node, int level) {
  101.         if (node == null)
  102.             return;
  103.         int i;
  104.         SLLNode<E> tmp;
  105.  
  106.         for (i = 0; i < level; i++)
  107.             System.out.print(" ");
  108.         System.out.println(node.getElement().toString());
  109.         tmp = ((SLLNode<E>) node).firstChild;
  110.         while (tmp != null) {
  111.             printTreeRecursive(tmp, level + 1);
  112.             tmp = tmp.sibling;
  113.         }
  114.     }
  115.  
  116.     public void printTree() {
  117.         printTreeRecursive(root, 0);
  118.     }
  119.  
  120.     class SLLTreeIterator<T> implements Iterator<T> {
  121.  
  122.         SLLNode<T> start, current;
  123.  
  124.         public SLLTreeIterator(SLLNode<T> node) {
  125.             start = node;
  126.             current = node;
  127.         }
  128.  
  129.         public boolean hasNext() {
  130.             return (current != null);
  131.         }
  132.  
  133.         public T next() throws NoSuchElementException {
  134.             if (current != null) {
  135.                 SLLNode<T> tmp = current;
  136.                 current = current.sibling;
  137.                 return tmp.getElement();
  138.             } else {
  139.                 throw new NoSuchElementException();
  140.             }
  141.         }
  142.  
  143.         public void remove() {
  144.             if (current != null) {
  145.                 current = current.sibling;
  146.             }
  147.         }
  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.         // vasiot kod stoi ovde
  192.         String[] temp;
  193.         String komanda, ime;
  194.         SLLNode<String> opened = tree.root;
  195.         for (i = 0; i < N; i++) {
  196.             temp = commands[i].split(" ");
  197.             komanda = temp[0];
  198.             if (temp.length == 2) {
  199.                 ime = temp[1];
  200.                 SLLNode<String> jazol = new SLLNode<String>(ime);
  201.                 if (komanda.equals("CREATE")) {
  202.                     SLLNode<String> pom = opened.firstChild;
  203.                     SLLNode<String> pomosen = opened.firstChild;
  204.                     int brojach = 0;
  205.                     while (pom != null) {
  206.                         if (ime.compareTo(pom.element) > 0) {
  207.                             brojach++;
  208.                             pomosen = pom;
  209.                         }
  210.                         pom = pom.sibling;
  211.                     }
  212.                     if (brojach == 0)
  213.                         tree.addChild(opened, ime);
  214.                     else {
  215.                         SLLNode<String> nov = new SLLNode<String>(ime);
  216.                         nov.sibling = pomosen.sibling;
  217.                         pomosen.sibling = nov;
  218.                         nov.parent = opened;
  219.                     }
  220.                 } else if (komanda.equals("OPEN")) {
  221.                     SLLNode<String> pom = opened.firstChild;
  222.                     while (pom != null) {
  223.                         if (pom.element.compareTo(ime) == 0)
  224.                             opened = pom;
  225.                         pom = pom.sibling;
  226.                     }
  227.                 } else if (komanda.equals("DELETE")) {
  228.                     SLLNode<String> pom = opened.firstChild;
  229.                     while (pom != null) {
  230.                         if (pom.element.equals(ime)) {
  231.                             tree.remove(pom);
  232.                             break;
  233.                         }
  234.                         pom = pom.sibling;
  235.                     }
  236.                 }
  237.             } else {
  238.                 if (komanda.equals("BACK"))
  239.                     opened = opened.parent;
  240.                 else if (komanda.equals("PATH")) {
  241.                     SLLNode<String> pom = opened;
  242.                     String s = "\\";
  243.                     while (pom != tree.root) {
  244.                         s = pom.element + s;
  245.                         s = "\\" + s;
  246.                         pom = pom.parent;
  247.                     }
  248.                     System.out.print(tree.root.element);
  249.                     System.out.println(s);
  250.                 } else if (komanda.equals("PRINT"))
  251.                     tree.printTree();
  252.                 else return;
  253.             }
  254.         }
  255.     }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement