I've implemented a singly linked list in Java and would like a code review as I am still learning and this is a different method I've found to do this. The logic is still fuzzy as well to me and writing it out took ages and I'm sure I may have messed up a bit but still have time to mess around with this.
Instructions
- private data members in list class
- public methods must correspond to operations in data structure
- data members include the head, there will be a get size method and elementcounter for the number of elements in the linked list.
- only the link class can be referenced.
- find method must generate a new list of integers to refer to all positions where given item was found
- no user input output
- Inside the find and deleteItem methods, use the 'equals' method of the data entries to compare values.
Inside the toString method, use the toString methods of the data entries (implicitly) to display the properties of each entry.
public class List { private Link head; private Link tail; private int elementCount; public List() { head = null; tail = head; elementCount = 0; } public void insert(int position, Object data) { if (position < 1 || position > elementCount + 1) { System.out.println("Sorry could not insert due to position problems" + "Position can't be less than 1, or position is " + "greater than element exists"); return; } // insert head if (isEmpty() && position == 1) { head = tail = new Link(data, null); elementCount++; return; } if (!isEmpty() && position == 1) { Link temp = head; head = new Link(data, temp); elementCount++; return; } // insert middle if (position <= elementCount) { int i = 1; Link current = head; Link previous = null; while (i != position) { previous = current; current = current.next; i++; } previous.next = new Link(data, current); elementCount++; return; } // insert at end insertAtEnd(data); } public void insertAtEnd(Object data) { tail.next = new Link(data, null); tail = tail.next; elementCount++; } // public List find(Object toFind) { if (isEmpty()) { System.out.println("This List is empty"); return null; } List listOfpositions = new List(); Link current = head; int position = 1; while (current != null) { if (isEmpty(listOfpositions)) { if (current.data.equals(toFind)) { listOfpositions.insert(1, position); } } else { if (current.data.equals(toFind)) { listOfpositions.insertAtEnd(position); } } position++; current = current.next; } return listOfpositions; } public void deleteRange(int start, int end) { if (isEmpty()) { System.out.println("The list is empty"); return; } if (start > end) { System.out.println("Your range is inverted"); return; } if (start == end) { deleteAtIndex(start); System.out.println("Operation Complete..."); return; } int deleteCount = end - start + 1; for (int i = 0; i < deleteCount; i++) { deleteAtIndex(start); } System.out.println("Operation Complete..."); } public Object getSize() { return elementCount; } public void deleteItem(Object item) { if (isEmpty()) { System.out.println("This List is empty"); return; } Link current = head; int index = 1; while (current != null) { if (current.data.equals(item)) { deleteAtIndex(index); index--; } current = current.next; index++; } System.out.println("Operation Complete..."); } public Link retrieve(int index) { if (isEmpty()) { System.out.println("List is empty"); return null; } if (index > elementCount || index < 1) { System.out.println("Out of bound"); return null; } if (index == 1) { return head; } if (index == elementCount) { return tail; } Link current = head; int i = 1; while (current != null && i != index) { current = current.next; i++; } return current; } @Override public String toString() { String linkedList = "This is your list" + "\n["; Link node = head; while (node != null) { linkedList += (node); linkedList += ","; node = node.next; } int lastComma = linkedList.lastIndexOf(","); if (lastComma != -1) { linkedList = linkedList.substring(0, linkedList.length() - 1); } linkedList += ']'; return linkedList; } private void deleteAtIndex(int index) { if (isEmpty()) { return; } if (index < 1) { return; } if (index > elementCount) { return; } if (index == 1) { head = head.next; elementCount--; return; } if (index == elementCount) { Link currentLink = head; while (currentLink.next != tail) { currentLink = currentLink.next; } tail = currentLink; currentLink.next = null; elementCount--; return; } int i = 1; Link current = head; Link previous = null; while (i != index) { previous = current; current = current.next; i++; } previous.next = current.next; elementCount--; } private boolean isEmpty(List listOfpositions) { return listOfpositions.head == null; } private boolean isEmpty() { return head == null; } }
link class
public class Link {
public Object data;
public Link next;
/*
* OR USE THIS CODE
* public Object data;
* public Node next;
*
* public Node (Object _data) {
* data = _data
* next = null
* }
*/
// have each link include a reference to an object
public Link(Object _data, Link node) {
data = _data;
next = node;
}
@Override
public String toString() {
return "Links data:" + data;
}
}
this is a different method I've found
Please clarify, in your question, whether you yourself coded an algorithm you found vs. present code you found. See What topics can I ask about here? \$\endgroup\$empty list represented as null
will exclude Java instance methods. \$\endgroup\$only the link class can be referenced
. \$\endgroup\$