Advertisement
Scylla7

APS Lab 7 Windows Explorer

Dec 3rd, 2016
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.46 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.     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;
  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.  
  187.         br.close();
  188.  
  189.         SLLTree<String> tree = new SLLTree<String>();
  190.         tree.makeRoot("c:");
  191.         SLLNode<String> curr = (SLLNode<String>) tree.root();
  192.         for(i = 0; i < commands.length; i++) {
  193.             String str [] = commands[i].split("\\s+");
  194.             switch(str[0]) {
  195.             case "CREATE" : {
  196.                 String data = str[1];
  197.                 SLLNode<String> tmp = curr.firstChild;
  198.                 if(tmp == null || tmp.element.compareTo(data) > 0)
  199.                     tree.addChild(curr, data);
  200.                 else {
  201.                     SLLNode<String> toBeInserted = new SLLNode<String>(data);
  202.                     while(tmp.sibling != null) {
  203.                         if(tmp.sibling.element.compareTo(toBeInserted.element) > 0) {
  204.                             toBeInserted.sibling = tmp.sibling;
  205.                             tmp.sibling = toBeInserted;
  206.                             toBeInserted.parent = curr;
  207.                             break;
  208.                         }
  209.                         tmp = tmp.sibling;
  210.                     }
  211.                     toBeInserted.parent = curr;
  212.                     tmp.sibling = toBeInserted;
  213.                 }
  214.                 break;
  215.             }
  216.  
  217.             case "OPEN" : {
  218.                 SLLNode<String> toOpen = curr.firstChild;
  219.                 while(toOpen != null) {
  220.                     if(toOpen.element.equals(str[1]))
  221.                         curr = toOpen;
  222.                     toOpen = toOpen.sibling;
  223.                 }
  224.                 break;
  225.             }
  226.             case "PATH" : {
  227.                 SLLNode<String> temp = curr;
  228.                 StringBuilder sb = new StringBuilder();
  229.  
  230.                 while (temp != tree.root) {
  231.                     sb.insert(0, "\\");
  232.                     sb.insert(0, temp.element);
  233.                     temp = temp.parent;
  234.                 }
  235.                 sb.insert(0,"\\");
  236.                 sb.insert(0, tree.root.element);
  237.                 System.out.println(sb.toString());
  238.                 break;
  239.             }
  240.  
  241.             case "BACK" : {
  242.                     curr = curr.parent;
  243.                 break;
  244.             }
  245.  
  246.             case "PRINT" : {
  247.                 tree.printTree();
  248.                 break;
  249.             }
  250.  
  251.             case "DELETE" : {
  252.                 SLLNode<String> tmp = curr;
  253.                 String data = str[1];
  254.                 if(tmp.element.equals(data)){
  255.                     curr = curr.parent;
  256.                     tree.remove(curr.firstChild);
  257.                 }
  258.                 else{
  259.                     tmp = tmp.firstChild;
  260.                     while(tmp != null){
  261.                         if(tmp.element.equals(data)){
  262.                             tree.remove(tmp);
  263.                         }
  264.                         tmp = tmp.sibling;
  265.                     }
  266.                 }
  267.                 break;
  268.             }
  269.             }
  270.         }
  271.     }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement