Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 207f582

Browse files
author
Ram swaroop
committed
remove middle points in line segments: comments + test case added
1 parent de2a295 commit 207f582

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/me/ramswaroop/linkedlists/RemoveMiddlePointsFromLineSegments.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@
1212
*/
1313
public class RemoveMiddlePointsFromLineSegments {
1414

15+
/**
16+
* Given a linked list of co-ordinates representing line segments, we have
17+
* to remove those nodes which represent the middle points.
18+
* <p/>
19+
* Example:
20+
* Input:
21+
* (0,10)-> (1,10)-> (3,10)-> (10,10)-> (10,8)-> (10,5)-> (20,5)-> (40,5)
22+
* Output:
23+
* (0,10)-> (10,10)-> (10,5)-> (40,5)
24+
* <p/>
25+
* Input:
26+
* (2,3)->(4,3)->(6,3)->(10,3)->(12,3)
27+
* Output:
28+
* (2,3)->(12,3)
29+
*
30+
* @param node
31+
*/
1532
public static void removeMiddlePointsFromLineSegments(SingleLinkedNode<Point> node) {
1633

1734
SingleLinkedNode<Point> curr1 = node, curr2 = node;
@@ -29,13 +46,15 @@ public static void removeMiddlePointsFromLineSegments(SingleLinkedNode<Point> no
2946
}
3047
curr1.next = curr2;
3148
} else {
49+
System.out.println("Linked list doesn't represent line segments!");
3250
return;
3351
}
3452
curr1 = curr1.next;
3553
}
3654
}
3755

3856
public static void main(String a[]) {
57+
// test case 1
3958
SingleLinkedList<Point> linkedList = new SingleLinkedList<>();
4059
linkedList.add(new Point(0, 10));
4160
linkedList.add(new Point(1, 10));
@@ -48,6 +67,17 @@ public static void main(String a[]) {
4867
linkedList.printList();
4968
removeMiddlePointsFromLineSegments(linkedList.head);
5069
linkedList.printList();
70+
71+
// test case 2
72+
SingleLinkedList<Point> linkedList2 = new SingleLinkedList<>();
73+
linkedList2.add(new Point(2, 3));
74+
linkedList2.add(new Point(4, 3));
75+
linkedList2.add(new Point(6, 3));
76+
linkedList2.add(new Point(10, 3));
77+
linkedList2.add(new Point(12, 3));
78+
linkedList2.printList();
79+
removeMiddlePointsFromLineSegments(linkedList2.head);
80+
linkedList2.printList();
5181
}
5282
}
5383

0 commit comments

Comments
 (0)