Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package linkedList;
- public class SinglyLinkedList<T extends Comparable<T>> implements List<T> {
- private Node<T> top;
- private int listSize;
- //Creates a node and sets the size of the list
- public SinglyLinkedList(){
- top = new Node<T>(null);
- listSize = 0;
- }
- //Inserts element at a specific position
- public void add(int index, T value) throws ListAccessError {
- Node<T> addNode = new Node<T>(value);
- Node<T> nextE = top;
- if(index > (listSize + 1)) throw new ListAccessError("You can't add an element to an index greater than the list size");
- //For each element less than index skip over then move each element one
- //more pointer
- for (int i = 1; i < index && nextE.next() != null; i++) {
- nextE = nextE.next();
- }
- //Set the pointer to the next node
- addNode.setNext(nextE.next());
- //Set this node to the old node pointer
- nextE.setNext(addNode);
- //Increment the size
- listSize++;
- }
- //Gets the element at a specific index
- public T get(int index) throws ListAccessError{
- //Index needs to be more than 1 since index 0 is top
- if (index <= 0)
- throw new ListAccessError("This is the top index");
- //Starts any counting from the next element after the top
- Node <T> nextE = top.next();
- //Each element less than the index is ignored
- for (int i = 1; i < index; i++) {
- if (nextE.next() == null)
- throw new ListAccessError("There is no node to get");
- nextE = nextE.next();
- }
- //Returns the value of the next index from index - 1 i.e index
- return nextE.value();
- }
- //Removes specific element
- public T remove(int index) throws ListAccessError{
- //If the index is the same as top or greater than the list size throw an error
- if (index < 1 || index > listSize)
- throw new ListAccessError ("Index not within specified list");
- //Each element less than the specific index is ignored
- Node<T> nextE = top;
- for (int i = 1; i < index; i++) {
- //If there isn't pointer to the next node throw error
- if (nextE.next() == null)
- throw new ListAccessError ("There is no node to remove");
- nextE = nextE.next();
- }
- //Assigns value of removed item to elementValue
- T elementValue = nextE.next().value();
- //Sets the current node pointer to the pointer after the removed node
- nextE.setNext(nextE.next().next());
- //Reduces list size by 1
- listSize--;
- return elementValue;
- }
- //Return True or False depending on whether list is empty
- public boolean isEmpty(){
- return(top == null);
- }
- //For outputing the test
- public String toString() {
- Node<T> indexE = top.next();
- String testOutput = "";
- while (indexE != null) {
- testOutput += "[" + indexE.value().toString() + "]";
- indexE = indexE.next();
- }
- return testOutput;
- }
- //Main method
- public static void main(String[] args) throws ListAccessError {
- SinglyLinkedList<String> newList = new SinglyLinkedList<String>();
- //Add elements to SinglyLinkedList
- newList.add(1,"1");
- newList.add(2,"2");
- newList.add(3,"3");
- newList.add(4,"4");
- newList.add(5,"5");
- newList.add(2, "6");
- //Test that methods work
- try{
- System.out.println("List: " + newList);
- System.out.println("Get 2nd element: " + newList.get(2));
- System.out.println("Remove 2nd element: " + newList.remove(2));
- System.out.println("List size: " + newList.listSize);
- System.out.println("Update list: " + newList);
- } finally {
- System.out.println("Is list empty?: " + newList.isEmpty());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement