Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Collections;
- public class Army
- {
- public static class DLLNode<E>
- {
- protected E element;
- protected DLLNode<E> pred;
- protected DLLNode<E> succ;
- public DLLNode(E e, DLLNode<E> p, DLLNode<E> s)
- {
- this.element = e;
- this.pred = p;
- this.succ = s;
- }
- }
- public static class DLL<E>
- {
- private DLLNode<E> first;
- private DLLNode<E> last;
- public DLL()
- {
- this.first = null;
- this.last = null;
- }
- public DLLNode<E> getFirst()
- {
- return this.first;
- }
- public DLLNode<E> getLast()
- {
- return this.last;
- }
- public void deleteList()
- {
- this.first = null;
- }
- public int length()
- {
- int length = 0;
- if (first != null)
- {
- DLLNode<E> temp = this.first;
- length = 1;
- while (temp.succ != null)
- {
- temp = temp.succ;
- length++;
- }
- return length;
- }
- else {
- return 0;
- }
- }
- public DLLNode<E> find(E element)
- {
- if (first != null)
- {
- DLLNode<E> temp = first;
- while (!temp.element.equals(element) && temp.succ != null)
- {
- temp = temp.succ;
- }
- if (temp.element.equals(element))
- {
- return temp;
- }
- else
- {
- System.out.println("The element is not in the list");
- }
- }
- else
- {
- System.out.println("The list is empty");
- }
- return first;
- }
- public void insertFirst(E element)
- {
- DLLNode<E> iFirst = new DLLNode<E>(element, null, first);
- if (this.first == null)
- {
- this.last = iFirst;
- }
- else
- {
- this.first.pred = iFirst;
- }
- this.first = iFirst;
- }
- public void insertLast(E element)
- {
- if (this.first == null)
- {
- insertFirst(element);
- }
- else
- {
- DLLNode<E> iLast = new DLLNode<E>(element, last, null);
- this.last.succ = iLast;
- this.last = iLast;
- }
- }
- public void insertAfter(E element, DLLNode<E> node)
- {
- if(node == this.last)
- {
- insertLast(element);
- return;
- }
- DLLNode<E> iAfter = new DLLNode<E>(element, node, node.succ);
- node.succ.pred = iAfter;
- node.succ = iAfter;
- }
- public void insertBefore(E element, DLLNode<E> node)
- {
- if(node == this.first)
- {
- insertFirst(element);
- return;
- }
- DLLNode<E> iBefore = new DLLNode<E>(element, node.pred, node);
- node.pred.succ = iBefore;
- node.pred = iBefore;
- }
- public E deleteFirst()
- {
- if (this.first != null)
- {
- DLLNode<E> temp = this.first;
- this.first = first.succ;
- this.first.pred = null;
- if (first == null)
- {
- this.last = null;
- }
- return temp.element;
- }
- else
- {
- return null;
- }
- }
- public E deleteLast()
- {
- if (this.first != null)
- {
- if (this.first.succ == null)
- {
- return deleteFirst();
- }
- else
- {
- DLLNode<E> temp = this.last;
- this.last = last.pred;
- this.last.succ = null;
- return temp.element;
- }
- }
- return null;
- }
- public E delete(DLLNode<E> node)
- {
- if(node == this.first)
- {
- deleteFirst();
- return node.element;
- }
- if(node == this.last)
- {
- deleteLast();
- return node.element;
- }
- node.pred.succ = node.succ;
- node.succ.pred = node.pred;
- return node.element;
- }
- @Override
- public String toString()
- {
- String DLList = new String();
- if (this.last != null)
- {
- DLLNode<E> temp = this.last;
- DLList += temp + "<->";
- while (temp.pred != null)
- {
- temp = temp.pred;
- DLList += temp + "->";
- }
- }
- else
- {
- DLList = "The list is empty";
- }
- return DLList;
- }
- }
- private static DLL<Integer> army(DLL<Integer> list, int a, int b, int c, int d)
- {
- DLL<Integer> army = new DLL<Integer>();
- DLLNode<Integer> temp = list.getFirst();
- while(temp.element != a)
- {
- army.insertLast(temp.element);
- temp = temp.succ;
- }
- temp = list.find(c);
- while(temp.element != d)
- {
- army.insertLast(temp.element);
- temp = temp.succ;
- }
- army.insertLast(temp.element);
- temp = list.find(b).succ;
- while(temp.element != c)
- {
- army.insertLast(temp.element);
- temp = temp.succ;
- }
- temp = list.find(a);
- while(temp.element != b)
- {
- army.insertLast(temp.element);
- temp = temp.succ;
- }
- army.insertLast(temp.element);
- temp = list.find(d).succ;
- while(temp != null)
- {
- army.insertLast(temp.element);
- temp = temp.succ;
- }
- return army;
- }
- public static void main(String[] args) throws IOException
- {
- DLL<Integer> list = new DLL<Integer>();
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String s = br.readLine();
- int n = Integer.parseInt(s);
- String[] elements = br.readLine().split(" ");
- for (int i = 0; i < n; i++)
- {
- list.insertLast(Integer.parseInt(elements[i]));
- }
- s = br.readLine();
- String interval[] = s.split(" ");
- int a = Integer.parseInt(interval[0]);
- int b = Integer.parseInt(interval[1]);
- s = br.readLine();
- interval = s.split(" ");
- int c = Integer.parseInt(interval[0]);
- int d = Integer.parseInt(interval[1]);
- DLL<Integer> result = army(list, a, b, c, d);
- DLLNode<Integer> node = result.getFirst();
- System.out.print(node.element);
- node = node.succ;
- while(node != null)
- {
- System.out.print(" " + node.element);
- node = node.succ;
- }
- }
- }
Add Comment
Please, Sign In to add comment