Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- public class RemoveMiddlePoints {
- public static SLL<Point> deleteMiddlePoints(SLL<Point> list) {
- SLLNode<Point> temp = list.getFirst();
- while (temp.succ != null) {
- if ((temp.element.getX()) == (temp.succ.element.getX())) {
- //isti mu se x koordinatite,vertikalna linija
- if (temp.succ.succ != null) {
- if (temp.succ.succ.element.getX() == temp.element.getX()) {
- list.delete(temp.succ);
- } else {
- temp = temp.succ;
- }
- } else break;
- } else if ((temp.element.getY()) == (temp.succ.element.getY())) {
- //isti mu se y koordinatite,horizontalna linija
- if (temp.succ.succ != null) {
- if (temp.succ.succ.element.getY() == temp.element.getY()) {
- list.delete(temp.succ);
- } else {
- temp = temp.succ;
- }
- } else break;
- }
- }
- return list;
- }
- public static void main(String[] args) {
- SLL<Point> points = new SLL<>();
- points.insertLast(new Point(2, 3));
- points.insertLast(new Point(4, 3));
- points.insertLast(new Point(6, 3));
- points.insertLast(new Point(6, 3));
- points.insertLast(new Point(10, 3));
- points.insertLast(new Point(12, 3));
- System.out.printf("Original: %s\n", points);
- SLL<Point> result = deleteMiddlePoints(points);
- System.out.printf("Result: %s\n", result);
- }
- }
- /**
- * Given a linked list of line segments, remove middle points
- * Given a linked list of co-ordinates where adjacent points either form a vertical line or a horizontal line.
- * Delete points from the linked list which are in the middle of a horizontal or vertical line.
- * Examples:
- * <p>
- * Input: (0,10)->(1,10)->(5,10)->(7,10)
- * |
- * (7,5)->(20,5)->(40,5)
- * Output: Linked List should be changed to following
- * (0,10)->(7,10)
- * |
- * (7,5)->(40,5)
- * The given linked list represents a horizontal line from (0,10)
- * to (7, 10) followed by a vertical line from (7, 10) to (7, 5),
- * followed by a horizontal line from (7, 5) to (40, 5).
- * <p>
- * Input: (2,3)->(4,3)->(6,3)->(10,3)->(12,3)
- * Output: Linked List should be changed to following
- * (2,3)->(12,3)
- * There is only one vertical line, so all middle points are removed.
- */
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class SLL<E> {
- private SLLNode<E> first;
- public SLL() {
- // Construct an empty SLL
- this.first = null;
- }
- public void deleteList() {
- first = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- SLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- SLLNode<E> tmp = first;
- ret += tmp + "->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + "->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public void insertFirst(E o) {
- SLLNode<E> ins = new SLLNode<E>(o, first);
- first = ins;
- }
- public void insertAfter(E o, SLLNode<E> node) {
- if (node != null) {
- SLLNode<E> ins = new SLLNode<E>(o, node.succ);
- node.succ = ins;
- } else {
- System.out.println("Dadenot jazol e null");
- }
- }
- public void insertBefore(E o, SLLNode<E> before) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if (first == before) {
- this.insertFirst(o);
- return;
- }
- //ako first!=before
- while (tmp.succ != before)
- tmp = tmp.succ;
- if (tmp.succ == before) {
- SLLNode<E> ins = new SLLNode<E>(o, before);
- tmp.succ = ins;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- }
- public void insertLast(E o) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- SLLNode<E> ins = new SLLNode<E>(o, null);
- tmp.succ = ins;
- } else {
- insertFirst(o);
- }
- }
- public E deleteFirst() {
- if (first != null) {
- SLLNode<E> tmp = first;
- first = first.succ;
- return tmp.element;
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public E delete(SLLNode<E> node) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if (first == node) {
- return this.deleteFirst();
- }
- while (tmp.succ != node && tmp.succ.succ != null)
- tmp = tmp.succ;
- if (tmp.succ == node) {
- tmp.succ = tmp.succ.succ;
- return node.element;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- return null;
- }
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public SLLNode<E> getFirst() {
- return first;
- }
- public SLLNode<E> find(E o) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.element != o && tmp.succ != null)
- tmp = tmp.succ;
- if (tmp.element == o) {
- return tmp;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- return first;
- }
- public Iterator<E> iterator() {
- // Return an iterator that visits all elements of this list, in left-to-right order.
- return new LRIterator<E>();
- }
- // //////////Inner class ////////////
- private class LRIterator<E> implements Iterator<E> {
- private SLLNode<E> place, curr;
- private LRIterator() {
- place = (SLLNode<E>) first;
- curr = null;
- }
- public boolean hasNext() {
- return (place != null);
- }
- public E next() {
- if (place == null)
- throw new NoSuchElementException();
- E nextElem = place.element;
- curr = place;
- place = place.succ;
- return nextElem;
- }
- public void remove() {
- //Not implemented
- }
- }
- public void mirror() {
- if (first != null) {
- //m=nextsucc, p=tmp,q=next
- SLLNode<E> tmp = first;
- SLLNode<E> newsucc = null;
- SLLNode<E> next;
- while (tmp != null) {
- next = tmp.succ;
- tmp.succ = newsucc;
- newsucc = tmp;
- tmp = next;
- }
- first = newsucc;
- }
- }
- public void merge(SLL<E> in) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- tmp.succ = in.getFirst();
- } else {
- first = in.getFirst();
- }
- }
- }
- class Point {
- private int koordinata_x;
- private int koordinata_y;
- public Point(int x, int y) {
- koordinata_x = x;
- koordinata_y = y;
- }
- public int getX() {
- return koordinata_x;
- }
- public void setX(int x) {
- koordinata_x = x;
- }
- public int getY() {
- return koordinata_y;
- }
- public void setY(int y) {
- koordinata_y = y;
- }
- @Override
- public String toString() {
- return "[" + koordinata_x + " , " + koordinata_y + "]";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment