Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.20 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.util.StringTokenizer;
  5. import java.util.Iterator;
  6. import java.util.NoSuchElementException;
  7.  
  8. interface Tree<E> {
  9. ////////////Accessors ////////////
  10.  
  11. public Node<E> root();
  12.  
  13. public Node<E> parent(Node<E> node);
  14.  
  15. public int childCount(Node<E> node);
  16.  
  17. ////////////Transformers ////////////
  18. public void makeRoot(E elem);
  19.  
  20. public Node<E> addChild(Node<E> node, E elem);
  21.  
  22. public void remove(Node<E> node);
  23.  
  24. ////////////Iterator ////////////
  25. public Iterator<E> children(Node<E> node);
  26.  
  27. }
  28.  
  29. interface Node<E> {
  30.  
  31. public E getElement();
  32.  
  33. public void setElement(E elem);
  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. interface Stack<E> {
  173.  
  174. // Elementi na stekot se objekti od proizvolen tip.
  175.  
  176. // Metodi za pristap:
  177.  
  178. public boolean isEmpty ();
  179. // Vrakja true ako i samo ako stekot e prazen.
  180.  
  181. public E peek ();
  182. // Go vrakja elementot na vrvot od stekot.
  183.  
  184. // Metodi za transformacija:
  185.  
  186. public void clear ();
  187. // Go prazni stekot.
  188.  
  189. public void push (E x);
  190. // Go dodava x na vrvot na stekot.
  191.  
  192. public E pop ();
  193. // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  194. }
  195.  
  196. class ArrayStack<E> implements Stack<E> {
  197. private E[] elems;
  198. private int depth;
  199.  
  200. @SuppressWarnings("unchecked")
  201. public ArrayStack (int maxDepth) {
  202. // Konstrukcija na nov, prazen stek.
  203. elems = (E[]) new Object[maxDepth];
  204. depth = 0;
  205. }
  206.  
  207.  
  208. public boolean isEmpty () {
  209. // Vrakja true ako i samo ako stekot e prazen.
  210. return (depth == 0);
  211. }
  212.  
  213.  
  214. public E peek () {
  215. // Go vrakja elementot na vrvot od stekot.
  216. if (depth == 0)
  217. throw new NoSuchElementException();
  218. return elems[depth-1];
  219. }
  220.  
  221.  
  222. public void clear () {
  223. // Go prazni stekot.
  224. for (int i = 0; i < depth; i++) elems[i] = null;
  225. depth = 0;
  226. }
  227.  
  228.  
  229. public void push (E x) {
  230. // Go dodava x na vrvot na stekot.
  231. elems[depth++] = x;
  232. }
  233.  
  234.  
  235. public E pop () {
  236. // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  237. if (depth == 0)
  238. throw new NoSuchElementException();
  239. E topmost = elems[--depth];
  240. elems[depth] = null;
  241. return topmost;
  242. }
  243. }
  244.  
  245.  
  246. public class WindowsExplorer {
  247. public static int sortString(String no1, String no2){
  248.  
  249. int min = Math.min(no1.length(), no2.length());
  250. for (int i = 0; i < min; i++) {
  251. if(no1.charAt(i) > no2.charAt(i)){
  252. return 2;
  253. }
  254. if(no1.charAt(i) < no2.charAt(i)){
  255. return 1;
  256. }
  257. }
  258. if(no1.length()==min){
  259. return 1;
  260. } else{
  261. return 2;
  262. }
  263.  
  264. }
  265. public static void main(String[] args) throws Exception {
  266. int i,j,k;
  267.  
  268. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  269.  
  270. int N = Integer.parseInt(br.readLine());
  271. String commands[] = new String[N];
  272.  
  273. for (i=0;i<N;i++)
  274. commands[i] = br.readLine();
  275.  
  276. br.close();
  277.  
  278. SLLTree<String> tree = new SLLTree<String>();
  279. tree.makeRoot("c:");
  280.  
  281.  
  282. SLLNode<String> tmp = tree.root;
  283.  
  284. for (int l = 0; l < N; l++) {
  285. String s = commands[l];
  286. if(s.contains("CREATE")){
  287. s = s.substring(7);
  288. SLLNode<String> thed = new SLLNode<String>(s);
  289. SLLNode<String> frst=null;
  290. SLLNode<String> scnd = tmp.firstChild;
  291. if(scnd == null){
  292. tree.addChild(tmp, s);
  293. }else{
  294. int g = sortString(scnd.element,s);
  295. while(((scnd!=null)&&g==1)){
  296. frst = scnd;
  297. scnd = scnd.sibling;
  298. if(scnd != null){
  299. g = sortString(scnd.element,s);
  300. }
  301.  
  302. }
  303.  
  304. if(frst == null){
  305. tree.addChild(tmp, s);
  306. }
  307. else{
  308. thed.parent = tmp;
  309. thed.sibling = scnd;
  310. frst.sibling = thed;
  311. }
  312. }
  313. }
  314. if(s.contains("OPEN")){
  315. s =s.substring(5);
  316. int h = tree.childCount(tmp);
  317. tmp = tmp.firstChild;
  318. while(!tmp.element.equals(s)){
  319. tmp = tmp.sibling;
  320. }
  321. }
  322. if(s.contains("BACK")){
  323. tmp = tmp.parent;
  324. }
  325. if(s.contains("DELETE")){
  326. s = s.substring(7);
  327. SLLNode<String> tht = new SLLNode<String>(s);
  328. SLLNode<String> tmp1 = tmp.firstChild;
  329. while(!tmp1.element.equals(s)){
  330. tmp1 = tmp1.sibling;
  331. }
  332. tree.remove(tmp1);
  333. }
  334. if(s.contains("PATH")){
  335. ArrayStack<SLLNode<String>> str = new ArrayStack<SLLNode<String>>(N);
  336. SLLNode<String> tmp1 = tmp;
  337. while(tmp1!=null){
  338. str.push(tmp1);
  339. tmp1 = tmp1.parent;
  340. }
  341. while(!str.isEmpty()){
  342. SLLNode<String> t = str.pop();
  343. System.out.print(t.element + "\\");
  344. }
  345. System.out.println();
  346. }
  347. if(s.contains("PRINT")){
  348. tree.printTree();
  349. }
  350. }
  351.  
  352. }
  353.  
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement