/** * * @author nickgrifasi * @version 10/11/21 * LinkedList used for storing points. * Deals with duplication logic and has access * to point data. * */ public class LinkedList { /** * points to first node */ private Node headNode; /** * boolean flag if duplicates exist * in the List */ private boolean ifDuplicate; /** * integer representing non duplicate points */ private int nonDupe; /** * size of LinkedList */ private int size; /** * default constructor */ public LinkedList() { headNode = null; ifDuplicate = true; nonDupe = 0; size = 0; } /** * Constructor that takes input of a point, * internal node created with point data. * @param point point to set first node */ public LinkedList(Point point) { headNode = new Node(point); ifDuplicate = true; nonDupe = 1; size = 1; } /** * returns boolean flag that determines if list contains * duplicates * * @return boolean, true if no dupes */ public boolean duplicate() { return ifDuplicate; } /** * Inserts a point into the LinkedList. Insertion * occurs at end of LinkedList for easier access * to duplicates. * @param point to insert */ public void insert(Point point) { Node node = new Node(point); /** * condition if List is empty */ if (headNode == null) { headNode = node; size = size + 1; nonDupe = nonDupe + 1; } else { Node pointer = headNode; while (pointer.getNode() != null) { if (!pointer.getPoint().equals(point)) { ifDuplicate = false; } if (point.compareTo(pointer.getPoint()) >= 0) { node.setNode(pointer.getNode()); pointer.setNode(node); size++; return; } pointer = pointer.getNode(); } pointer.setNode(node); size++; } } /** * Removes a point from LinkedList. * Removes by name or by point comparison. * @param comparison point to be removed * @param flag if removal by name of point * @return Point to be removed */ public Point remove(Point comparison, boolean flag) { Node node = headNode; if (node == null) { return null; } if (node.getPoint().equals(comparison)) { this.duplicateFix(); return removeHeadNode(); } while (node.getNode() != null) { if (node.getNode().getPoint().equals(comparison)) { if (!flag) { Node tempNode = node.getNode(); node.setNode(node.getNode().getNode()); tempNode.setNode(null); this.duplicateFix(); return tempNode.getPoint(); } else if (node.getNode().getPoint().getName() .compareTo(comparison.getName()) == 0) { Node tempNode = node.getNode(); node.setNode(node.getNode().getNode()); tempNode.setNode(null); this.duplicateFix(); return tempNode.getPoint(); } } node = node.getNode(); } return null; } /** * getter method that returns headNode * @return headNode */ public Node getHeadNode() { return headNode; } /** * removes first node from linked list * @return point from first node in list */ public Point removeFirst() { if (headNode.getNode() == null) { Point point = headNode.getPoint(); headNode = null; return point; } else { Point point = headNode.getPoint(); headNode = headNode.getNode(); return point; } } /** * getter method that returns size of LinkedList, excluding * duplicates from total size. * * @return size of list */ public int getSize() { return size; } /** * getter method returns nonDuplicate points * @return nonDuplicate points */ public int getNonDupe() { return nonDupe; } /** * Prints duplicate points within the LinkedList to * System.out */ public void printDupe() { String systemLine = ""; Node node = headNode; if (headNode != null) { while (node.getNode() != null) { if (node.getPoint().equals(node.getNode().getPoint()) && !systemLine.contains(node.getNode().getPoint() .toStringCoord())) { systemLine = systemLine + node.getNode() .getPoint().toStringCoord(); System.out.println(node.getNode() .getPoint().toStringCoord()); } node = node.getNode(); } } } /** * changes LinkedList depending on total number of * non-duplicate points within LinkedList */ public void duplicateFix() { if (headNode == null) { size = 0; nonDupe = 0; } else { Node node = headNode; int tempSize = 1; int tempNonDupe = 1; while (node.getNode() != null) { if (!node.getPoint().equals(node.getNode().getPoint())) { tempNonDupe = tempNonDupe + 1; } tempSize = tempSize + 1; node = node.getNode(); } this.size = tempSize; this.nonDupe = tempNonDupe; } } /** * private method that removes HeadNode from LinkedList * @return removed node */ private Point removeHeadNode() { if (headNode.getNode() == null) { Point point = headNode.getPoint(); headNode = null; return point; } else { Point point = headNode.getPoint(); headNode = headNode.getNode(); return point; } } }