Filip_Markoski

[ADSx] - RemoveMiddlePoints SSL

Jan 18th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.66 KB | None | 0 0
  1. import java.util.Iterator;
  2. import java.util.NoSuchElementException;
  3.  
  4. public class RemoveMiddlePoints {
  5.  
  6.     public static SLL<Point> deleteMiddlePoints(SLL<Point> list) {
  7.         SLLNode<Point> temp = list.getFirst();
  8.         while (temp.succ != null) {
  9.             if ((temp.element.getX()) == (temp.succ.element.getX())) {
  10.                 //isti mu se x koordinatite,vertikalna linija
  11.                 if (temp.succ.succ != null) {
  12.                     if (temp.succ.succ.element.getX() == temp.element.getX()) {
  13.                         list.delete(temp.succ);
  14.                     } else {
  15.                         temp = temp.succ;
  16.                     }
  17.                 } else break;
  18.             } else if ((temp.element.getY()) == (temp.succ.element.getY())) {
  19.                 //isti mu se y koordinatite,horizontalna linija
  20.                 if (temp.succ.succ != null) {
  21.                     if (temp.succ.succ.element.getY() == temp.element.getY()) {
  22.                         list.delete(temp.succ);
  23.                     } else {
  24.                         temp = temp.succ;
  25.                     }
  26.                 } else break;
  27.             }
  28.         }
  29.         return list;
  30.     }
  31.  
  32.     public static void main(String[] args) {
  33.  
  34.         SLL<Point> points = new SLL<>();
  35.  
  36.         points.insertLast(new Point(2, 3));
  37.         points.insertLast(new Point(4, 3));
  38.         points.insertLast(new Point(6, 3));
  39.         points.insertLast(new Point(6, 3));
  40.         points.insertLast(new Point(10, 3));
  41.         points.insertLast(new Point(12, 3));
  42.  
  43.         System.out.printf("Original: %s\n", points);
  44.         SLL<Point> result = deleteMiddlePoints(points);
  45.         System.out.printf("Result: %s\n", result);
  46.     }
  47.  
  48. }
  49.  
  50.  
  51. /**
  52.  * Given a linked list of line segments, remove middle points
  53.  * Given a linked list of co-ordinates where adjacent points either form a vertical line or a horizontal line.
  54.  * Delete points from the linked list which are in the middle of a horizontal or vertical line.
  55.  * Examples:
  56.  * <p>
  57.  * Input:  (0,10)->(1,10)->(5,10)->(7,10)
  58.  * |
  59.  * (7,5)->(20,5)->(40,5)
  60.  * Output: Linked List should be changed to following
  61.  * (0,10)->(7,10)
  62.  * |
  63.  * (7,5)->(40,5)
  64.  * The given linked list represents a horizontal line from (0,10)
  65.  * to (7, 10) followed by a vertical line from (7, 10) to (7, 5),
  66.  * followed by a horizontal line from (7, 5) to (40, 5).
  67.  * <p>
  68.  * Input:     (2,3)->(4,3)->(6,3)->(10,3)->(12,3)
  69.  * Output: Linked List should be changed to following
  70.  * (2,3)->(12,3)
  71.  * There is only one vertical line, so all middle points are removed.
  72.  */
  73. class SLLNode<E> {
  74.     protected E element;
  75.     protected SLLNode<E> succ;
  76.  
  77.     public SLLNode(E elem, SLLNode<E> succ) {
  78.         this.element = elem;
  79.         this.succ = succ;
  80.     }
  81.  
  82.     @Override
  83.     public String toString() {
  84.         return element.toString();
  85.     }
  86. }
  87.  
  88. class SLL<E> {
  89.     private SLLNode<E> first;
  90.  
  91.     public SLL() {
  92.         // Construct an empty SLL
  93.         this.first = null;
  94.     }
  95.  
  96.     public void deleteList() {
  97.         first = null;
  98.     }
  99.  
  100.     public int length() {
  101.         int ret;
  102.         if (first != null) {
  103.             SLLNode<E> tmp = first;
  104.             ret = 1;
  105.             while (tmp.succ != null) {
  106.                 tmp = tmp.succ;
  107.                 ret++;
  108.             }
  109.             return ret;
  110.         } else
  111.             return 0;
  112.  
  113.     }
  114.  
  115.     @Override
  116.     public String toString() {
  117.         String ret = new String();
  118.         if (first != null) {
  119.             SLLNode<E> tmp = first;
  120.             ret += tmp + "->";
  121.             while (tmp.succ != null) {
  122.                 tmp = tmp.succ;
  123.                 ret += tmp + "->";
  124.             }
  125.         } else
  126.             ret = "Prazna lista!!!";
  127.         return ret;
  128.     }
  129.  
  130.     public void insertFirst(E o) {
  131.         SLLNode<E> ins = new SLLNode<E>(o, first);
  132.         first = ins;
  133.     }
  134.  
  135.     public void insertAfter(E o, SLLNode<E> node) {
  136.         if (node != null) {
  137.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  138.             node.succ = ins;
  139.         } else {
  140.             System.out.println("Dadenot jazol e null");
  141.         }
  142.     }
  143.  
  144.     public void insertBefore(E o, SLLNode<E> before) {
  145.  
  146.         if (first != null) {
  147.             SLLNode<E> tmp = first;
  148.             if (first == before) {
  149.                 this.insertFirst(o);
  150.                 return;
  151.             }
  152.             //ako first!=before
  153.             while (tmp.succ != before)
  154.                 tmp = tmp.succ;
  155.             if (tmp.succ == before) {
  156.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  157.                 tmp.succ = ins;
  158.             } else {
  159.                 System.out.println("Elementot ne postoi vo listata");
  160.             }
  161.         } else {
  162.             System.out.println("Listata e prazna");
  163.         }
  164.     }
  165.  
  166.     public void insertLast(E o) {
  167.         if (first != null) {
  168.             SLLNode<E> tmp = first;
  169.             while (tmp.succ != null)
  170.                 tmp = tmp.succ;
  171.             SLLNode<E> ins = new SLLNode<E>(o, null);
  172.             tmp.succ = ins;
  173.         } else {
  174.             insertFirst(o);
  175.         }
  176.     }
  177.  
  178.     public E deleteFirst() {
  179.         if (first != null) {
  180.             SLLNode<E> tmp = first;
  181.             first = first.succ;
  182.             return tmp.element;
  183.         } else {
  184.             System.out.println("Listata e prazna");
  185.             return null;
  186.         }
  187.     }
  188.  
  189.     public E delete(SLLNode<E> node) {
  190.         if (first != null) {
  191.             SLLNode<E> tmp = first;
  192.             if (first == node) {
  193.                 return this.deleteFirst();
  194.             }
  195.             while (tmp.succ != node && tmp.succ.succ != null)
  196.                 tmp = tmp.succ;
  197.             if (tmp.succ == node) {
  198.                 tmp.succ = tmp.succ.succ;
  199.                 return node.element;
  200.             } else {
  201.                 System.out.println("Elementot ne postoi vo listata");
  202.                 return null;
  203.             }
  204.         } else {
  205.             System.out.println("Listata e prazna");
  206.             return null;
  207.         }
  208.  
  209.     }
  210.  
  211.     public SLLNode<E> getFirst() {
  212.         return first;
  213.     }
  214.  
  215.     public SLLNode<E> find(E o) {
  216.         if (first != null) {
  217.             SLLNode<E> tmp = first;
  218.             while (tmp.element != o && tmp.succ != null)
  219.                 tmp = tmp.succ;
  220.             if (tmp.element == o) {
  221.                 return tmp;
  222.             } else {
  223.                 System.out.println("Elementot ne postoi vo listata");
  224.             }
  225.         } else {
  226.             System.out.println("Listata e prazna");
  227.         }
  228.         return first;
  229.     }
  230.  
  231.     public Iterator<E> iterator() {
  232.         // Return an iterator that visits all elements of this list, in left-to-right order.
  233.         return new LRIterator<E>();
  234.     }
  235.  
  236.     // //////////Inner class ////////////
  237.  
  238.     private class LRIterator<E> implements Iterator<E> {
  239.  
  240.         private SLLNode<E> place, curr;
  241.  
  242.         private LRIterator() {
  243.             place = (SLLNode<E>) first;
  244.             curr = null;
  245.         }
  246.  
  247.         public boolean hasNext() {
  248.             return (place != null);
  249.         }
  250.  
  251.         public E next() {
  252.             if (place == null)
  253.                 throw new NoSuchElementException();
  254.             E nextElem = place.element;
  255.             curr = place;
  256.             place = place.succ;
  257.             return nextElem;
  258.         }
  259.  
  260.         public void remove() {
  261.             //Not implemented
  262.         }
  263.     }
  264.  
  265.     public void mirror() {
  266.         if (first != null) {
  267.             //m=nextsucc, p=tmp,q=next
  268.             SLLNode<E> tmp = first;
  269.             SLLNode<E> newsucc = null;
  270.             SLLNode<E> next;
  271.  
  272.             while (tmp != null) {
  273.                 next = tmp.succ;
  274.                 tmp.succ = newsucc;
  275.                 newsucc = tmp;
  276.                 tmp = next;
  277.             }
  278.             first = newsucc;
  279.         }
  280.  
  281.     }
  282.  
  283.     public void merge(SLL<E> in) {
  284.         if (first != null) {
  285.             SLLNode<E> tmp = first;
  286.             while (tmp.succ != null)
  287.                 tmp = tmp.succ;
  288.             tmp.succ = in.getFirst();
  289.         } else {
  290.             first = in.getFirst();
  291.         }
  292.     }
  293. }
  294.  
  295. class Point {
  296.  
  297.     private int koordinata_x;
  298.     private int koordinata_y;
  299.  
  300.     public Point(int x, int y) {
  301.         koordinata_x = x;
  302.         koordinata_y = y;
  303.     }
  304.  
  305.     public int getX() {
  306.         return koordinata_x;
  307.     }
  308.  
  309.     public void setX(int x) {
  310.         koordinata_x = x;
  311.     }
  312.  
  313.     public int getY() {
  314.         return koordinata_y;
  315.     }
  316.  
  317.     public void setY(int y) {
  318.         koordinata_y = y;
  319.     }
  320.  
  321.     @Override
  322.     public String toString() {
  323.         return "[" + koordinata_x + " , " + koordinata_y + "]";
  324.     }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment