Created
September 20, 2014 03:55
-
-
Save ggreenleaf/ec945bf9187c18495ae6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class GenLinkedList <T> { | |
| /*====================================== | |
| = members specific to list = | |
| ======================================*/ | |
| /*============================================= | |
| = Node Definition (element of List) = | |
| =============================================*/ | |
| private static class Node <T> { | |
| /** | |
| * constructor for node with T type | |
| * param d - val for storage | |
| * param n - node will point to | |
| **/ | |
| public Node(T val, Node<T> n) { | |
| data = val; | |
| next = n; | |
| } | |
| public T data; | |
| public Node<T> next; | |
| } | |
| private Node<T> head; | |
| private Node<T> tail; | |
| public int length; | |
| /*======================================= | |
| = List Methods = | |
| =======================================*/ | |
| /** | |
| * | |
| * Constructor clear list and set head, tail to node with null data | |
| * | |
| **/ | |
| public GenLinkedList () { | |
| head = new Node<T> (null, null); | |
| tail = new Node<T> (null, null); | |
| length = 0; | |
| } | |
| /** | |
| * | |
| * Push an Item to end of list | |
| * | |
| **/ | |
| public void addEnd(T val) { | |
| Node<T> newNode = new Node<>(val, null); | |
| this.tail.next = newNode; | |
| this.tail = newNode; | |
| length++; | |
| } | |
| /** | |
| * | |
| * add item with val to front of list | |
| * | |
| **/ | |
| public void addFront(T val) { | |
| Node<T> newNode = new Node<>(val, head); | |
| this.head = newNode; | |
| length++; | |
| } | |
| /** | |
| * | |
| * remove front node from list | |
| * possibly add later value for return of data removed. | |
| **/ | |
| public void removeFront() { | |
| this.head = this.head.next; | |
| length--; | |
| } | |
| /** | |
| * | |
| * remove from tail of list | |
| * possible add later value for return of data removed. | |
| **/ | |
| public void removeEnd() { | |
| Node<T> temp = new Node<>(null,head.next); | |
| //transverse list until temp.next is tail | |
| //then remove tail and set what | |
| while(temp.next != this.tail) | |
| temp = temp.next; | |
| tail = temp; | |
| tail.next = null; | |
| length--; | |
| } | |
| /** | |
| * | |
| * set value T at index | |
| * | |
| **/ | |
| public void set(T value, int index ) { | |
| if (index < 0 || index >= length) | |
| throw new IndexOutOfBoundsException("for index, " + index); | |
| else { | |
| int indexCount = 0; | |
| Node<T> temp = new Node<>(null,head); | |
| while(indexCount - 1 < index) { | |
| temp = temp.next; | |
| indexCount++; | |
| } | |
| temp.next.data = value; | |
| } | |
| } | |
| /** | |
| * | |
| * get value at index | |
| * | |
| **/ | |
| public T get (int index) { | |
| if (index < 0 || index >= length) | |
| throw new IndexOutOfBoundsException("for index, " + index); | |
| else { | |
| int indexCount = 0; | |
| Node<T> temp = new Node<>(null,head.next); | |
| while(indexCount < index) { | |
| temp = temp.next; | |
| indexCount++; | |
| } | |
| return temp.data; | |
| } | |
| } | |
| /** | |
| * | |
| * convert list to string for printing | |
| * | |
| **/ | |
| public String toString( ) { | |
| StringBuilder sb = new StringBuilder( "[ " ); | |
| for(Integer i = 0; i < length; i++) | |
| sb.append( get(i) + " " ); | |
| sb.append( "]" ); | |
| return new String( sb ); | |
| } | |
| // g. swap | |
| // receives two index positions as parameters, and swaps the nodes at | |
| // these positions, provided both positions are within the current length | |
| // h. shift | |
| // receives an integer as a parameter, and shifts the list forward or | |
| // backward this number of nodes, provided it is within the current length | |
| // 11 points each (i-l) | |
| // i. removeMatching | |
| // receives a value of the generic type as a parameter and removes all | |
| // occurrences of this value from the list. | |
| // j. erase | |
| // receives an index position and number of elements as parameters, and | |
| // removes elements beginning at the index position for the number of | |
| // elements specified, provided the index position is within the length | |
| // and together with the number of elements does not exceed the length | |
| // k. insertList | |
| // receives a generic List (a Java List) and an index position as parameters, | |
| // and copies each value of the passed list into the current list starting | |
| // at the index position, provided the index position does not exceed the length. | |
| // For example, if list has a,b,c and another list having 1,2,3 is inserted at | |
| // position 2, the list becomes a,b,1,2,3,c | |
| // l. main | |
| // add code to the main method to demonstrate each of your methods | |
| public static void main (String[] args) | |
| { | |
| GenLinkedList<Integer> myList = new GenLinkedList<>(); | |
| myList.addFront(1); | |
| myList.addFront(2); | |
| System.out.println(myList.toString()); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment