Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.11 KB | None | 0 0
  1. package third;
  2.  
  3. import java.util.*;
  4.  
  5. interface IList<E> extends Iterable<E> {
  6. boolean add(E e); // qdd element to the end of list
  7.  
  8. void add(int index, E element) throws NoSuchElementException; // add element on position index
  9.  
  10. void clear(); // delete all elements
  11.  
  12. boolean contains(E element); // is list containing an element (equals())
  13.  
  14. E get(int index) throws NoSuchElementException; //get element from position
  15.  
  16. E set(int index, E element) throws NoSuchElementException; // set new value on position
  17.  
  18. int indexOf(E element); // where is element (equals())
  19.  
  20. boolean isEmpty();
  21.  
  22. Iterator<E> iterator();
  23.  
  24. ListIterator<E> listIterator() throws UnsupportedOperationException; // for ListIterator
  25.  
  26. E remove(int index) throws NoSuchElementException; // remove element from position index
  27.  
  28. boolean remove(E e); // remove element
  29.  
  30. int size();
  31. }
  32.  
  33. class TwoWayLinkedListWithHead<E> implements IList<E> {
  34.  
  35. private class Element {
  36. public Element(E e) {
  37. this.object = e;
  38. }
  39.  
  40. public Element(E e, Element next, Element prev) {
  41. this.object = e;
  42. this.next = next;
  43. this.prev = prev;
  44. }
  45.  
  46. E object;
  47. Element next = null;
  48. Element prev = null;
  49.  
  50. public Element getNext() {
  51. return next;
  52. }
  53.  
  54. public E getValue() {
  55. return object;
  56. }
  57.  
  58. public void setNext(Element next) {
  59. this.next = next;
  60. }
  61.  
  62. public void setPrev(Element prev) {
  63. this.prev = prev;
  64. }
  65.  
  66. public Element getPrev() {
  67. return prev;
  68. }
  69.  
  70. public void setValue(E element) {
  71. object = element;
  72. }
  73. }
  74.  
  75. Element head;
  76. Integer size;
  77.  
  78. private class InnerIterator implements Iterator<E> {
  79. Element pos;
  80.  
  81.  
  82. public InnerIterator() {
  83. pos = head;
  84. }
  85.  
  86. @Override
  87. public boolean hasNext() {
  88. return pos.getNext() != null;
  89. }
  90.  
  91. @Override
  92. public E next() {
  93. Element temp = pos;
  94. pos = pos.getNext();
  95. return temp.getValue();
  96. }
  97. }
  98.  
  99. private class InnerListIterator implements ListIterator<E> {
  100. Element p;
  101.  
  102. public InnerListIterator() {
  103. p = head;
  104. }
  105.  
  106. @Override
  107. public void add(E e) {
  108. throw new UnsupportedOperationException();
  109.  
  110. }
  111.  
  112. @Override
  113. public boolean hasNext() {
  114. return p.getNext() != null;
  115. }
  116.  
  117. @Override
  118. public boolean hasPrevious() {
  119. return p.getPrev() != null;
  120. }
  121.  
  122. @Override
  123. public E next() {
  124. return p.getNext().getValue();
  125. }
  126.  
  127. @Override
  128. public int nextIndex() {
  129. throw new UnsupportedOperationException();
  130. }
  131.  
  132. @Override
  133. public E previous() {
  134. return p.getPrev().getValue();
  135. }
  136.  
  137. @Override
  138. public int previousIndex() {
  139. throw new UnsupportedOperationException();
  140. }
  141.  
  142. @Override
  143. public void remove() {
  144. throw new UnsupportedOperationException();
  145.  
  146. }
  147.  
  148. @Override
  149. public void set(E e) {
  150. p.object = e;
  151. }
  152. }
  153.  
  154. public TwoWayLinkedListWithHead() {
  155. head = null;
  156. }
  157.  
  158. @Override
  159. public boolean add(E e) {
  160. Element newElem = new Element(e);
  161. if (head == null) {
  162. head = newElem;
  163. size = 1;
  164. } else {
  165. Element endElem = head;
  166. while (endElem.getNext() != null)
  167. endElem = endElem.getNext();
  168. endElem.setNext(newElem);
  169. newElem.setPrev(endElem);
  170. size++;
  171. }
  172. return true;
  173. }
  174.  
  175. @Override
  176. public void add(int index, E element) {
  177. if (index < 0 || index > size + 1) {
  178. throw new NoSuchElementException();
  179. }
  180. Element newElem = new Element(element);
  181. if (index == 0) {
  182. head.setPrev(newElem);
  183. newElem.setNext(head);
  184. head = newElem;
  185. } else {
  186. Element current = getElement(index - 1);
  187. newElem.setNext(current.getNext());
  188. current.getNext().setPrev(newElem);
  189. current.setNext(newElem);
  190. newElem.setPrev(current);
  191. }
  192. }
  193.  
  194. private Element getElement(int i) {
  195. Element actElem = head;
  196. while (i > 0 && actElem != null) {
  197. actElem = actElem.getNext();
  198. i--;
  199. }
  200. if (actElem == null) {
  201. throw new NoSuchElementException();
  202. }
  203. return actElem;
  204. }
  205.  
  206. @Override
  207. public void clear() {
  208. head = null;
  209. }
  210.  
  211. @Override
  212. public boolean contains(E element) {
  213. return indexOf(element) != 1;
  214. }
  215.  
  216. @Override
  217. public E get(int index) {
  218. if (index < 0 || index > size || isEmpty()) {
  219. throw new NoSuchElementException();
  220. }
  221. return getElement(index).getValue();
  222. }
  223.  
  224. @Override
  225. public E set(int index, E element) {
  226. if (index < 0 || index > size || isEmpty()) {
  227. throw new NoSuchElementException();
  228. }
  229. Element actElem = getElement(index);
  230. E elemValue = actElem.getValue();
  231. actElem.setValue(element);
  232. return elemValue;
  233. }
  234.  
  235. @Override
  236. public int indexOf(E element) {
  237. int counter = 0;
  238. Iterator<E> iterator = iterator();
  239. while (iterator.hasNext()) {
  240. if (iterator.next().equals(element)) {
  241. return counter;
  242. }
  243. counter++;
  244. }
  245. return -1;
  246. }
  247.  
  248. @Override
  249. public boolean isEmpty() {
  250. return head == null;
  251. }
  252.  
  253. @Override
  254. public Iterator<E> iterator() {
  255. return new InnerIterator();
  256. }
  257.  
  258. @Override
  259. public ListIterator<E> listIterator() {
  260. throw new UnsupportedOperationException();
  261. }
  262.  
  263. @Override
  264. public E remove(int index) {
  265. Element actElem;
  266. if (index < 0 || index > size || isEmpty()) {
  267. throw new NoSuchElementException();
  268. } else if (index == 0) {
  269. actElem = head;
  270. Element hold = head.getNext();
  271. hold.setPrev(null);
  272. head = hold;
  273. size--;
  274. } else {
  275. actElem = getElement(index);
  276. getElement(index - 1).setNext(actElem.getNext());
  277. actElem.getNext().setPrev(actElem);
  278. size--;
  279. }
  280. return actElem.getValue();
  281. }
  282.  
  283. @Override
  284. public boolean remove(E e) {
  285. if (head == null) {
  286. return false;
  287. }
  288. if (head.getValue().equals(e)) {
  289. Element hold = head.getNext();
  290. hold.setPrev(null);
  291. head = hold;
  292. size--;
  293. return true;
  294. }
  295. Element actElem = head;
  296. while (actElem.getNext() != null && !(actElem.getNext().getValue().equals(e))) {
  297. actElem = actElem.getNext();
  298. }
  299. if (actElem.getNext() == null) {
  300. return false;
  301. }
  302. actElem.setNext(actElem.getNext().getNext());
  303. actElem.getNext().setPrev(actElem);
  304. size--;
  305. return true;
  306. }
  307.  
  308. @Override
  309. public int size() {
  310. if (size == null) {
  311. size = 0;
  312. Iterator<E> iterator = iterator();
  313. while (iterator.hasNext()) {
  314. iterator.next();
  315. size++;
  316. }
  317. }
  318. return size;
  319. }
  320.  
  321. public String toStringReverse() {
  322. ListIterator<E> iter = new InnerListIterator();
  323. while (iter.hasNext())
  324. iter.next();
  325. StringBuffer retStr = new StringBuffer();
  326. while (iter.hasPrevious()) {
  327. retStr.append("\n").append(iter.previous());
  328. }
  329. return retStr.toString();
  330. }
  331.  
  332. public void add(TwoWayLinkedListWithHead<E> other) {
  333. //Element tail;
  334. //tail.setNext(other.head);
  335. //other.head.setPrev(tail);
  336. //other.clear();
  337. }
  338. }
  339.  
  340.  
  341. class Link {
  342. public String ref;
  343.  
  344. public Link(String ref) {
  345. this.ref = ref;
  346. }
  347.  
  348. @Override
  349. public boolean equals(Object object) {
  350. if (!(object instanceof Link))
  351. return false;
  352. Link temp = (Link) object;
  353. return ref.equals(temp.ref);
  354. }
  355.  
  356. public String getRef() {
  357. return ref;
  358. }
  359. // in the future there will be more fields
  360. }
  361.  
  362. class Document {
  363. public String name;
  364. public TwoWayLinkedListWithHead<Link> link;
  365.  
  366. public Document(String name, Scanner scan) {
  367. this.name = name;
  368. link = new TwoWayLinkedListWithHead<Link>();
  369. load(scan);
  370. }
  371.  
  372. public void load(Scanner scan) {
  373. String input;
  374. while (!(input = scan.nextLine()).equalsIgnoreCase("eod")) {
  375. String[] arr = input.split("\\s+");
  376. for (String word : arr) {
  377. if (correctLink(word)) {
  378. Link temp = new Link(word.substring(5).toLowerCase());
  379. link.add(temp);
  380. }
  381. }
  382. }
  383. }
  384.  
  385. // accepted only small letters, capitalic letter, digits nad '_' (but not on the begin)
  386. private static boolean correctLink(String link) {
  387. return link.toLowerCase().matches("link=[a-z]\\w*");
  388. }
  389.  
  390. @Override
  391. public String toString() {
  392. StringBuffer output = new StringBuffer();
  393. Iterator<Link> str = link.iterator();
  394. output.append("Document: " + name);
  395. while (str.hasNext()) {
  396. output.append("\n" + str.next().getRef());
  397. }
  398. return output.toString();
  399. }
  400.  
  401. }
  402.  
  403. public class Main {
  404.  
  405.  
  406. static Scanner scan; // for input stream
  407.  
  408.  
  409. public static void main(String[] args) {
  410. System.out.println("START");
  411. scan = new Scanner(System.in);
  412. Document[] doc = null;
  413. int currentDocNo = 0;
  414. int maxNo = -1;
  415. boolean halt = false;
  416. while (!halt) {
  417. String line = scan.nextLine();
  418. // empty line and comment line - read next line
  419. if (line.length() == 0 || line.charAt(0) == '#')
  420. continue;
  421. // copy line to output (it is easier to find a place of a mistake)
  422. System.out.println("!" + line);
  423. String word[] = line.split(" ");
  424. // go n - start with array of the length n
  425. if (word[0].equalsIgnoreCase("go") && word.length == 2) {
  426. maxNo = Integer.parseInt(word[1]);
  427. doc = new Document[maxNo];
  428. continue;
  429. }
  430. //ch - change index
  431. if (word[0].equalsIgnoreCase("ch") && word.length == 2) {
  432. currentDocNo = Integer.parseInt(word[1]);
  433. continue;
  434. }
  435.  
  436. // ld documentName
  437. if (word[0].equalsIgnoreCase("ld") && word.length == 2) {
  438. doc[currentDocNo] = new Document(word[1], scan);
  439. continue;
  440. }
  441. // ha
  442. if (word[0].equalsIgnoreCase("ha") && word.length == 1) {
  443. halt = true;
  444. continue;
  445. }
  446. // clear
  447. if (word[0].equalsIgnoreCase("clear") && word.length == 1) {
  448. doc[currentDocNo].link.clear();
  449. continue;
  450. }
  451. // show
  452. if (word[0].equalsIgnoreCase("show") && word.length == 1) {
  453. System.out.println(doc[currentDocNo].toString());
  454. continue;
  455. }
  456. // reverse
  457. if (word[0].equalsIgnoreCase("reverse") && word.length == 1) {
  458. System.out.println(doc[currentDocNo].link.toStringReverse());
  459. continue;
  460. }
  461. // size
  462. if (word[0].equalsIgnoreCase("size") && word.length == 1) {
  463. System.out.println(doc[currentDocNo].link.size());
  464. continue;
  465. }
  466. // add str
  467. if (word[0].equalsIgnoreCase("add") && word.length == 2) {
  468. System.out.println(doc[currentDocNo].link.add(new Link(word[1])));
  469. continue;
  470. }
  471. // addi index str
  472. if (word[0].equalsIgnoreCase("addi") && word.length == 3) {
  473. int index = Integer.parseInt(word[1]);
  474. try {
  475. doc[currentDocNo].link.add(index, new Link(word[2]));
  476. } catch (NoSuchElementException e) {
  477. System.out.println("error");
  478. }
  479. continue;
  480. }
  481. // get index
  482. if (word[0].equalsIgnoreCase("get") && word.length == 2) {
  483. int index = Integer.parseInt(word[1]);
  484. try {
  485. Link l = doc[currentDocNo].link.get(index);
  486. System.out.println(l.ref);
  487. } catch (NoSuchElementException e) {
  488. System.out.println("error");
  489. }
  490. continue;
  491. }
  492. // set index str
  493. if (word[0].equalsIgnoreCase("set") && word.length == 3) {
  494. int index = Integer.parseInt(word[1]);
  495. try {
  496. Link l = doc[currentDocNo].link.set(index, new Link(word[2]));
  497. System.out.println(l.ref);
  498. } catch (NoSuchElementException e) {
  499. System.out.println("error");
  500. }
  501.  
  502. continue;
  503. }
  504. // index str
  505. if (word[0].equalsIgnoreCase("index") && word.length == 2) {
  506. int index = doc[currentDocNo].link.indexOf(new Link(word[1]));
  507. System.out.println(index);
  508. continue;
  509. }
  510. // remi index
  511. if (word[0].equalsIgnoreCase("remi") && word.length == 2) {
  512. int index = Integer.parseInt(word[1]);
  513. try {
  514. Link l = doc[currentDocNo].link.remove(index);
  515. System.out.println(l.ref);
  516. } catch (NoSuchElementException e) {
  517. System.out.println("error");
  518. }
  519. continue;
  520. }
  521. // rem str
  522. if (word[0].equalsIgnoreCase("rem") && word.length == 2) {
  523. System.out.println(doc[currentDocNo].link.remove(new Link(word[1])));
  524. continue;
  525. }
  526. // addl <indexOfListArray>
  527. if (word[0].equalsIgnoreCase("addl") && word.length == 2) {
  528. int number = Integer.parseInt(word[1]);
  529. doc[currentDocNo].link.add(doc[number].link);
  530. continue;
  531. }
  532. System.out.println("Wrong command");
  533. }
  534. System.out.println("END OF EXECUTION");
  535. scan.close();
  536.  
  537. }
  538.  
  539.  
  540. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement