|  | 
| 1 | 1 | /** | 
| 2 | 2 |  * This class implements a SinglyLinked List. This is done | 
| 3 | 3 |  * using SinglyLinkedList class and a LinkForLinkedList Class. | 
| 4 |  | - * | 
|  | 4 | + * <p> | 
| 5 | 5 |  * A linked list is similar to an array, it hold values. | 
| 6 | 6 |  * However, links in a linked list do not have indexes. With | 
| 7 | 7 |  * a linked list you do not need to predetermine it's size as | 
| 8 | 8 |  * it grows and shrinks as it is edited. This is an example of | 
| 9 | 9 |  * a singly linked list. Elements can only be added/removed | 
| 10 | 10 |  * at the head/front of the list. | 
| 11 | 11 |  * | 
| 12 |  | - * @author Unknown | 
| 13 |  | - * | 
|  | 12 | + * @author yanglbme | 
| 14 | 13 |  */ | 
| 15 |  | -class SinglyLinkedList{ | 
| 16 |  | -	/**Head refered to the front of the list */ | 
| 17 |  | -	private Node head; | 
| 18 |  | - | 
| 19 |  | -	/** | 
| 20 |  | -	 * Constructor of SinglyLinkedList | 
| 21 |  | -	 */ | 
| 22 |  | -	public SinglyLinkedList(){ | 
| 23 |  | -		head = null; | 
| 24 |  | -	} | 
| 25 |  | - | 
| 26 |  | -	/** | 
| 27 |  | -	 * This method inserts an element at the head | 
| 28 |  | -	 * | 
| 29 |  | -	 * @param x Element to be added | 
| 30 |  | -	 */ | 
| 31 |  | -	public void insertHead(int x){ | 
| 32 |  | -		Node newNode = new Node(x); //Create a new link with a value attached to it | 
| 33 |  | -		newNode.next = head; 		//Set the new link to point to the current head | 
| 34 |  | -		head = newNode; 			//Now set the new link to be the head | 
| 35 |  | -        Node.indexCount++;          //Count the all indexes of inserted values | 
| 36 |  | -	} | 
|  | 14 | +class SinglyLinkedList { | 
|  | 15 | +    /** | 
|  | 16 | +     * Head refer to the front of the list | 
|  | 17 | +     */ | 
|  | 18 | +    private Node head; | 
|  | 19 | + | 
| 37 | 20 |     /** | 
| 38 |  | -     * Insert values at spesific position | 
| 39 |  | -     * @param number    inserted value | 
| 40 |  | -     * @param position  spesific position of inserted value | 
|  | 21 | +     * Count of nodes | 
| 41 | 22 |      */ | 
| 42 |  | -    public void addToSpecifiedPosition(int number, int position) { | 
| 43 |  | -            InsertNth(head, number, position); | 
|  | 23 | +    private int count; | 
|  | 24 | + | 
|  | 25 | +    /** | 
|  | 26 | +     * This method inserts an element at the head | 
|  | 27 | +     * | 
|  | 28 | +     * @param x Element to be added | 
|  | 29 | +     */ | 
|  | 30 | +    public void insertHead(int x) { | 
|  | 31 | +        Node newNode = new Node(x); | 
|  | 32 | +        newNode.next = head; | 
|  | 33 | +        head = newNode; | 
|  | 34 | +        ++count; | 
| 44 | 35 |     } | 
| 45 | 36 | 
 | 
| 46 |  | -	/** | 
|  | 37 | +    /** | 
| 47 | 38 |      * Inserts a new node at a specified position | 
| 48 |  | -     * @param head     head node of the linked list | 
|  | 39 | +     * | 
| 49 | 40 |      * @param data     data to be stored in a new node | 
| 50 | 41 |      * @param position position at which a new node is to be inserted | 
| 51 |  | -     * @return  reference of the head of the linked list | 
| 52 | 42 |      */ | 
| 53 | 43 | 
 | 
| 54 |  | -    Node InsertNth(Node head, int data, int position) { | 
| 55 |  | - | 
| 56 |  | -        Node newNode = new Node(data); | 
| 57 |  | -        Node current = head; | 
| 58 |  | -        int temp = position - Node.getIndexCount(); | 
|  | 44 | +    public void insertNth(int data, int position) { | 
|  | 45 | +        if (position < 0 || position > count) { | 
|  | 46 | +            throw new RuntimeException("position less than zero or position more than the count of list"); | 
|  | 47 | +        } | 
|  | 48 | +        Node node = new Node(data); | 
|  | 49 | +        Node dummy = new Node(-1); | 
|  | 50 | +        dummy.next = head; | 
|  | 51 | +        Node cur = dummy; | 
|  | 52 | +        for (int i = 0; i < position; ++i) { | 
|  | 53 | +            cur = cur.next; | 
|  | 54 | +        } | 
|  | 55 | +        node.next = cur.next; | 
|  | 56 | +        cur.next = node; | 
|  | 57 | +        ++count; | 
|  | 58 | +    } | 
| 59 | 59 | 
 | 
| 60 |  | -        while (temp-- > 0) { | 
| 61 |  | -            insertHead(0); | 
| 62 |  | -            System.out.println("Do something " + Node.indexCount); | 
|  | 60 | +    /** | 
|  | 61 | +     * This method deletes an element at the head | 
|  | 62 | +     * | 
|  | 63 | +     * @return The element deleted | 
|  | 64 | +     */ | 
|  | 65 | +    public Node deleteHead() { | 
|  | 66 | +        if (isEmpty()) { | 
|  | 67 | +            throw new RuntimeException("The list is empty!"); | 
| 63 | 68 |         } | 
| 64 | 69 | 
 | 
| 65 |  | -        newNode.next = current; | 
| 66 |  | -        head = newNode; | 
| 67 |  | -        insertHead(newNode.value); | 
| 68 |  | -        return head; | 
|  | 70 | +        Node temp = head; | 
|  | 71 | +        head = head.next; | 
|  | 72 | +        --count; | 
|  | 73 | +        return temp; | 
|  | 74 | +    } | 
|  | 75 | + | 
|  | 76 | +    /** | 
|  | 77 | +     * Checks if the list is empty | 
|  | 78 | +     * | 
|  | 79 | +     * @return true is list is empty | 
|  | 80 | +     */ | 
|  | 81 | +    public boolean isEmpty() { | 
|  | 82 | +        return count == 0; | 
| 69 | 83 |     } | 
| 70 | 84 | 
 | 
| 71 |  | -	/** | 
| 72 |  | -	 * This method deletes an element at the head | 
| 73 |  | -	 * | 
| 74 |  | -	 * @return The element deleted | 
| 75 |  | -	 */ | 
| 76 |  | -	public Node deleteHead(){ | 
| 77 |  | -		Node temp = head; | 
| 78 |  | -		head = head.next; //Make the second element in the list the new head, the Java garbage collector will later remove the old head | 
| 79 |  | -        --Node.indexCount; | 
| 80 |  | -		return temp; | 
| 81 |  | -	} | 
| 82 |  | - | 
| 83 |  | -	/** | 
| 84 |  | -	 * Checks if the list is empty | 
| 85 |  | -	 * | 
| 86 |  | -	 * @return true is list is empty | 
| 87 |  | -	 */ | 
| 88 |  | -	public boolean isEmpty(){ | 
| 89 |  | -		return(head == null); | 
| 90 |  | -	} | 
| 91 |  | - | 
| 92 |  | -	/** | 
| 93 |  | -	 * Prints contents of the list | 
| 94 |  | -	 */ | 
| 95 |  | -	public void display(){ | 
| 96 |  | -		Node current = head; | 
| 97 |  | -		while(current!=null){ | 
| 98 |  | -			System.out.print(current.getValue()+" "); | 
| 99 |  | -			current = current.next; | 
| 100 |  | -		} | 
| 101 |  | -		System.out.println(); | 
| 102 |  | -	} | 
| 103 |  | - | 
| 104 |  | -	/** | 
| 105 |  | -	 * Main method | 
| 106 |  | -	 * | 
| 107 |  | -	 * @param args Command line arguments | 
| 108 |  | -	 */ | 
| 109 |  | -	public static void main(String args[]){ | 
| 110 |  | -		SinglyLinkedList myList = new SinglyLinkedList(); | 
| 111 |  | - | 
| 112 |  | -		System.out.println(myList.isEmpty()); //Will print true | 
| 113 |  | - | 
| 114 |  | -		myList.insertHead(5); | 
| 115 |  | -		myList.insertHead(7); | 
| 116 |  | -		myList.insertHead(10); | 
| 117 |  | - | 
| 118 |  | -		myList.display(); // 10(head) --> 7 --> 5 | 
| 119 |  | - | 
| 120 |  | -		myList.deleteHead(); | 
| 121 |  | - | 
| 122 |  | -		myList.display(); // 7(head) --> 5 | 
| 123 |  | -	} | 
|  | 85 | +    /** | 
|  | 86 | +     * Prints contents of the list | 
|  | 87 | +     */ | 
|  | 88 | +    public void display() { | 
|  | 89 | +        Node current = head; | 
|  | 90 | +        while (current != null) { | 
|  | 91 | +            System.out.print(current.value + " "); | 
|  | 92 | +            current = current.next; | 
|  | 93 | +        } | 
|  | 94 | +        System.out.println(); | 
|  | 95 | +    } | 
|  | 96 | + | 
|  | 97 | +    /** | 
|  | 98 | +     * Main method | 
|  | 99 | +     * | 
|  | 100 | +     * @param args Command line arguments | 
|  | 101 | +     */ | 
|  | 102 | +    public static void main(String args[]) { | 
|  | 103 | +        SinglyLinkedList myList = new SinglyLinkedList(); | 
|  | 104 | + | 
|  | 105 | +        assert myList.isEmpty(); | 
|  | 106 | + | 
|  | 107 | +        myList.insertHead(5); | 
|  | 108 | +        myList.insertHead(7); | 
|  | 109 | +        myList.insertHead(10); | 
|  | 110 | + | 
|  | 111 | +        myList.display(); // 10 -> 7 -> 5 | 
|  | 112 | + | 
|  | 113 | +        myList.deleteHead(); | 
|  | 114 | + | 
|  | 115 | +        myList.display(); // 7 -> 5 | 
|  | 116 | + | 
|  | 117 | +        myList.insertNth(11, 2); | 
|  | 118 | + | 
|  | 119 | +        myList.display(); // 7 -> 5 -> 11 | 
|  | 120 | +    } | 
| 124 | 121 | } | 
| 125 | 122 | 
 | 
| 126 | 123 | /** | 
| 127 | 124 |  * This class is the nodes of the SinglyLinked List. | 
| 128 | 125 |  * They consist of a value and a pointer to the node | 
| 129 | 126 |  * after them. | 
| 130 | 127 |  * | 
| 131 |  | - * @author Unknown | 
| 132 |  | - * | 
|  | 128 | + * @author yanglbme | 
| 133 | 129 |  */ | 
| 134 |  | -class Node{ | 
| 135 |  | -	/** The value of the node */ | 
| 136 |  | -	public int value; | 
|  | 130 | +class Node { | 
| 137 | 131 |     /** | 
| 138 |  | -     * The count of Indexes | 
|  | 132 | +     * The value of the node | 
| 139 | 133 |      */ | 
| 140 |  | -    public static int indexCount; | 
| 141 |  | -	/** Point to the next node */ | 
| 142 |  | -	public Node next; //This is what the link will point to | 
| 143 |  | - | 
| 144 |  | -	/** | 
| 145 |  | -	 * Constructor | 
| 146 |  | -	 * | 
| 147 |  | -	 * @param valuein Value to be put in the node | 
| 148 |  | -	 */ | 
| 149 |  | -	public Node(int valuein){ | 
| 150 |  | -		value = valuein; | 
| 151 |  | -	} | 
| 152 |  | - | 
| 153 |  | -	/** | 
| 154 |  | -	 * Returns value of the node | 
| 155 |  | -	 */ | 
| 156 |  | -	public int getValue(){ | 
| 157 |  | -		return value; | 
| 158 |  | -	} | 
|  | 134 | +    int value; | 
|  | 135 | + | 
|  | 136 | +    /** | 
|  | 137 | +     * Point to the next node | 
|  | 138 | +     */ | 
|  | 139 | +    Node next; | 
|  | 140 | + | 
| 159 | 141 |     /** | 
| 160 |  | -     * @return the count of indexes | 
|  | 142 | +     * Constructor | 
|  | 143 | +     * | 
|  | 144 | +     * @param value Value to be put in the node | 
| 161 | 145 |      */ | 
| 162 |  | -    public static int getIndexCount() { | 
| 163 |  | -        return indexCount; | 
|  | 146 | +    Node(int value) { | 
|  | 147 | +        this.value = value; | 
| 164 | 148 |     } | 
| 165 | 149 | } | 
0 commit comments