patternjavaMinor
Given a linked list of line segments, remove collinear points in the middle
Viewed 0 times
thelinepointsremovesegmentslistlinkedgivenmiddlecollinear
Problem
This code removes any middle point find in line. It works only in x and y axis. This problem is a Java implementation of this.
```
package com.atleyvirdee.myDataStructures.linkedlist.problems;
import com.atleyvirdee.myDataStructures.linkedlist.ILinkedList;
import com.atleyvirdee.myDataStructures.linkedlist.ILinkedListNode;
import com.atleyvirdee.myDataStructures.linkedlist.LinkedListerTraverser;
import com.atleyvirdee.myDataStructures.linkedlist.implemtations.SinglyLinkedList;
/**
*
* @author jaspinder
*
* 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:
*
* 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).
*
* 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.
*/
public class P13RemoveMiddlePoints {
public static void main( String[] args ) {
// Test Case 1
System.out.println("Test Case One");
ILinkedList one = new SinglyLinkedList();
one.append(new Point(0, 10));
one.append(new Point(1, 10));
ILinkedListNode root = removeMiddlePoints(one.getRoot());
LinkedListerTraverser traversers =
new LinkedListerTraverser(root);
traversers.traverseList();
// Test Case 2
System.out.println("\
```
package com.atleyvirdee.myDataStructures.linkedlist.problems;
import com.atleyvirdee.myDataStructures.linkedlist.ILinkedList;
import com.atleyvirdee.myDataStructures.linkedlist.ILinkedListNode;
import com.atleyvirdee.myDataStructures.linkedlist.LinkedListerTraverser;
import com.atleyvirdee.myDataStructures.linkedlist.implemtations.SinglyLinkedList;
/**
*
* @author jaspinder
*
* 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:
*
* 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).
*
* 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.
*/
public class P13RemoveMiddlePoints {
public static void main( String[] args ) {
// Test Case 1
System.out.println("Test Case One");
ILinkedList one = new SinglyLinkedList();
one.append(new Point(0, 10));
one.append(new Point(1, 10));
ILinkedListNode root = removeMiddlePoints(one.getRoot());
LinkedListerTraverser traversers =
new LinkedListerTraverser(root);
traversers.traverseList();
// Test Case 2
System.out.println("\
Solution
Java has a very capable
Why does the
You've gone through the trouble of defining
LinkedList class already. Don't reinvent the wheel with yet another linked list implementation, which is likely incompatible with the Java Collections framework.Why does the
Point class bother to implement the Comparable interface at all if it does it altogether wrong?You've gone through the trouble of defining
.getX() and .getY() in your Point class, yet your algorithm just reaches into the object to access .x and .y directly. If you're going to do that, do it properly, with an immutable class:public final class Point {
public final int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
}Code Snippets
public final class Point {
public final int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
}Context
StackExchange Code Review Q#96681, answer score: 2
Revisions (0)
No revisions yet.