2016-03-15 LinkedList

posted Mar 15, 2016, 8:43 AM by Samuel Konstantinovich   [ updated Mar 15, 2016, 10:10 AM ]

Goal:  My Linked List


08MyLinkedList

Should Include:

-MyLinkedList.java

                -LNode(inner class)

Methods That MyLinkedList.java should have:

            variables: (start with these and add more as we need them)

            LNode start;

            int size;

            methods:

        Hint: Start by writing add(), and toString(),then get and set. Test these methods before writing new ones!

int get(int index) 

    - get the value of the element at the specified index (0 based)

int set(int index,int newValue)

    - change the value of the element at the specified index to the newValue, return the old value

int size() 

    - return the number of elements in the list

int remove(int index) 

    - remove the element at the specified index, returns the value removed

boolean add(int index, int value)    

    - insert a new element at the specified index, 0 at the front, size() at the end.

boolean add(int value) 

    - adds to end

int indexOf(int value) 

    - returns the index of the 1st occurrence of the value in the linked list, -1 if not found.

toString() 

    - returns a list formatted like: [ v1, v2, v3, ... vn-1, vn ] , empty list is just []

Things to be added later:

-Any exceptions that the actual LinkedList contains




How to loop through the elements:

Make a temporary reference to the node when you iterate through them (you can call it current)    

    LNode current = start;

In the loop use this piece of code to reference to the next of the temporary LNode or current

    current = current.getNext();



Comments