Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package third;
- import java.util.*;
- interface IList<E> extends Iterable<E> {
- boolean add(E e); // qdd element to the end of list
- void add(int index, E element) throws NoSuchElementException; // add element on position index
- void clear(); // delete all elements
- boolean contains(E element); // is list containing an element (equals())
- E get(int index) throws NoSuchElementException; //get element from position
- E set(int index, E element) throws NoSuchElementException; // set new value on position
- int indexOf(E element); // where is element (equals())
- boolean isEmpty();
- Iterator<E> iterator();
- ListIterator<E> listIterator() throws UnsupportedOperationException; // for ListIterator
- E remove(int index) throws NoSuchElementException; // remove element from position index
- boolean remove(E e); // remove element
- int size();
- }
- class TwoWayLinkedListWithHead<E> implements IList<E> {
- private class Element {
- public Element(E e) {
- this.object = e;
- }
- public Element(E e, Element next, Element prev) {
- this.object = e;
- this.next = next;
- this.prev = prev;
- }
- E object;
- Element next = null;
- Element prev = null;
- public Element getNext() {
- return next;
- }
- public E getValue() {
- return object;
- }
- public void setNext(Element next) {
- this.next = next;
- }
- public void setPrev(Element prev) {
- this.prev = prev;
- }
- public Element getPrev() {
- return prev;
- }
- public void setValue(E element) {
- object = element;
- }
- }
- Element head;
- Integer size;
- private class InnerIterator implements Iterator<E> {
- Element pos;
- public InnerIterator() {
- pos = head;
- }
- @Override
- public boolean hasNext() {
- return pos.getNext() != null;
- }
- @Override
- public E next() {
- Element temp = pos;
- pos = pos.getNext();
- return temp.getValue();
- }
- }
- private class InnerListIterator implements ListIterator<E> {
- Element p;
- public InnerListIterator() {
- p = head;
- }
- @Override
- public void add(E e) {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean hasNext() {
- return p.getNext() != null;
- }
- @Override
- public boolean hasPrevious() {
- return p.getPrev() != null;
- }
- @Override
- public E next() {
- return p.getNext().getValue();
- }
- @Override
- public int nextIndex() {
- throw new UnsupportedOperationException();
- }
- @Override
- public E previous() {
- return p.getPrev().getValue();
- }
- @Override
- public int previousIndex() {
- throw new UnsupportedOperationException();
- }
- @Override
- public void remove() {
- throw new UnsupportedOperationException();
- }
- @Override
- public void set(E e) {
- p.object = e;
- }
- }
- public TwoWayLinkedListWithHead() {
- head = null;
- }
- @Override
- public boolean add(E e) {
- Element newElem = new Element(e);
- if (head == null) {
- head = newElem;
- size = 1;
- } else {
- Element endElem = head;
- while (endElem.getNext() != null)
- endElem = endElem.getNext();
- endElem.setNext(newElem);
- newElem.setPrev(endElem);
- size++;
- }
- return true;
- }
- @Override
- public void add(int index, E element) {
- if (index < 0 || index > size + 1) {
- throw new NoSuchElementException();
- }
- Element newElem = new Element(element);
- if (index == 0) {
- head.setPrev(newElem);
- newElem.setNext(head);
- head = newElem;
- } else {
- Element current = getElement(index - 1);
- newElem.setNext(current.getNext());
- current.getNext().setPrev(newElem);
- current.setNext(newElem);
- newElem.setPrev(current);
- }
- }
- private Element getElement(int i) {
- Element actElem = head;
- while (i > 0 && actElem != null) {
- actElem = actElem.getNext();
- i--;
- }
- if (actElem == null) {
- throw new NoSuchElementException();
- }
- return actElem;
- }
- @Override
- public void clear() {
- head = null;
- }
- @Override
- public boolean contains(E element) {
- return indexOf(element) != 1;
- }
- @Override
- public E get(int index) {
- if (index < 0 || index > size || isEmpty()) {
- throw new NoSuchElementException();
- }
- return getElement(index).getValue();
- }
- @Override
- public E set(int index, E element) {
- if (index < 0 || index > size || isEmpty()) {
- throw new NoSuchElementException();
- }
- Element actElem = getElement(index);
- E elemValue = actElem.getValue();
- actElem.setValue(element);
- return elemValue;
- }
- @Override
- public int indexOf(E element) {
- int counter = 0;
- Iterator<E> iterator = iterator();
- while (iterator.hasNext()) {
- if (iterator.next().equals(element)) {
- return counter;
- }
- counter++;
- }
- return -1;
- }
- @Override
- public boolean isEmpty() {
- return head == null;
- }
- @Override
- public Iterator<E> iterator() {
- return new InnerIterator();
- }
- @Override
- public ListIterator<E> listIterator() {
- throw new UnsupportedOperationException();
- }
- @Override
- public E remove(int index) {
- Element actElem;
- if (index < 0 || index > size || isEmpty()) {
- throw new NoSuchElementException();
- } else if (index == 0) {
- actElem = head;
- Element hold = head.getNext();
- hold.setPrev(null);
- head = hold;
- size--;
- } else {
- actElem = getElement(index);
- getElement(index - 1).setNext(actElem.getNext());
- actElem.getNext().setPrev(actElem);
- size--;
- }
- return actElem.getValue();
- }
- @Override
- public boolean remove(E e) {
- if (head == null) {
- return false;
- }
- if (head.getValue().equals(e)) {
- Element hold = head.getNext();
- hold.setPrev(null);
- head = hold;
- size--;
- return true;
- }
- Element actElem = head;
- while (actElem.getNext() != null && !(actElem.getNext().getValue().equals(e))) {
- actElem = actElem.getNext();
- }
- if (actElem.getNext() == null) {
- return false;
- }
- actElem.setNext(actElem.getNext().getNext());
- actElem.getNext().setPrev(actElem);
- size--;
- return true;
- }
- @Override
- public int size() {
- if (size == null) {
- size = 0;
- Iterator<E> iterator = iterator();
- while (iterator.hasNext()) {
- iterator.next();
- size++;
- }
- }
- return size;
- }
- public String toStringReverse() {
- ListIterator<E> iter = new InnerListIterator();
- while (iter.hasNext())
- iter.next();
- StringBuffer retStr = new StringBuffer();
- while (iter.hasPrevious()) {
- retStr.append("\n").append(iter.previous());
- }
- return retStr.toString();
- }
- public void add(TwoWayLinkedListWithHead<E> other) {
- //Element tail;
- //tail.setNext(other.head);
- //other.head.setPrev(tail);
- //other.clear();
- }
- }
- class Link {
- public String ref;
- public Link(String ref) {
- this.ref = ref;
- }
- @Override
- public boolean equals(Object object) {
- if (!(object instanceof Link))
- return false;
- Link temp = (Link) object;
- return ref.equals(temp.ref);
- }
- public String getRef() {
- return ref;
- }
- // in the future there will be more fields
- }
- class Document {
- public String name;
- public TwoWayLinkedListWithHead<Link> link;
- public Document(String name, Scanner scan) {
- this.name = name;
- link = new TwoWayLinkedListWithHead<Link>();
- load(scan);
- }
- public void load(Scanner scan) {
- String input;
- while (!(input = scan.nextLine()).equalsIgnoreCase("eod")) {
- String[] arr = input.split("\\s+");
- for (String word : arr) {
- if (correctLink(word)) {
- Link temp = new Link(word.substring(5).toLowerCase());
- link.add(temp);
- }
- }
- }
- }
- // accepted only small letters, capitalic letter, digits nad '_' (but not on the begin)
- private static boolean correctLink(String link) {
- return link.toLowerCase().matches("link=[a-z]\\w*");
- }
- @Override
- public String toString() {
- StringBuffer output = new StringBuffer();
- Iterator<Link> str = link.iterator();
- output.append("Document: " + name);
- while (str.hasNext()) {
- output.append("\n" + str.next().getRef());
- }
- return output.toString();
- }
- }
- public class Main {
- static Scanner scan; // for input stream
- public static void main(String[] args) {
- System.out.println("START");
- scan = new Scanner(System.in);
- Document[] doc = null;
- int currentDocNo = 0;
- int maxNo = -1;
- boolean halt = false;
- while (!halt) {
- String line = scan.nextLine();
- // empty line and comment line - read next line
- if (line.length() == 0 || line.charAt(0) == '#')
- continue;
- // copy line to output (it is easier to find a place of a mistake)
- System.out.println("!" + line);
- String word[] = line.split(" ");
- // go n - start with array of the length n
- if (word[0].equalsIgnoreCase("go") && word.length == 2) {
- maxNo = Integer.parseInt(word[1]);
- doc = new Document[maxNo];
- continue;
- }
- //ch - change index
- if (word[0].equalsIgnoreCase("ch") && word.length == 2) {
- currentDocNo = Integer.parseInt(word[1]);
- continue;
- }
- // ld documentName
- if (word[0].equalsIgnoreCase("ld") && word.length == 2) {
- doc[currentDocNo] = new Document(word[1], scan);
- continue;
- }
- // ha
- if (word[0].equalsIgnoreCase("ha") && word.length == 1) {
- halt = true;
- continue;
- }
- // clear
- if (word[0].equalsIgnoreCase("clear") && word.length == 1) {
- doc[currentDocNo].link.clear();
- continue;
- }
- // show
- if (word[0].equalsIgnoreCase("show") && word.length == 1) {
- System.out.println(doc[currentDocNo].toString());
- continue;
- }
- // reverse
- if (word[0].equalsIgnoreCase("reverse") && word.length == 1) {
- System.out.println(doc[currentDocNo].link.toStringReverse());
- continue;
- }
- // size
- if (word[0].equalsIgnoreCase("size") && word.length == 1) {
- System.out.println(doc[currentDocNo].link.size());
- continue;
- }
- // add str
- if (word[0].equalsIgnoreCase("add") && word.length == 2) {
- System.out.println(doc[currentDocNo].link.add(new Link(word[1])));
- continue;
- }
- // addi index str
- if (word[0].equalsIgnoreCase("addi") && word.length == 3) {
- int index = Integer.parseInt(word[1]);
- try {
- doc[currentDocNo].link.add(index, new Link(word[2]));
- } catch (NoSuchElementException e) {
- System.out.println("error");
- }
- continue;
- }
- // get index
- if (word[0].equalsIgnoreCase("get") && word.length == 2) {
- int index = Integer.parseInt(word[1]);
- try {
- Link l = doc[currentDocNo].link.get(index);
- System.out.println(l.ref);
- } catch (NoSuchElementException e) {
- System.out.println("error");
- }
- continue;
- }
- // set index str
- if (word[0].equalsIgnoreCase("set") && word.length == 3) {
- int index = Integer.parseInt(word[1]);
- try {
- Link l = doc[currentDocNo].link.set(index, new Link(word[2]));
- System.out.println(l.ref);
- } catch (NoSuchElementException e) {
- System.out.println("error");
- }
- continue;
- }
- // index str
- if (word[0].equalsIgnoreCase("index") && word.length == 2) {
- int index = doc[currentDocNo].link.indexOf(new Link(word[1]));
- System.out.println(index);
- continue;
- }
- // remi index
- if (word[0].equalsIgnoreCase("remi") && word.length == 2) {
- int index = Integer.parseInt(word[1]);
- try {
- Link l = doc[currentDocNo].link.remove(index);
- System.out.println(l.ref);
- } catch (NoSuchElementException e) {
- System.out.println("error");
- }
- continue;
- }
- // rem str
- if (word[0].equalsIgnoreCase("rem") && word.length == 2) {
- System.out.println(doc[currentDocNo].link.remove(new Link(word[1])));
- continue;
- }
- // addl <indexOfListArray>
- if (word[0].equalsIgnoreCase("addl") && word.length == 2) {
- int number = Integer.parseInt(word[1]);
- doc[currentDocNo].link.add(doc[number].link);
- continue;
- }
- System.out.println("Wrong command");
- }
- System.out.println("END OF EXECUTION");
- scan.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement