1- class LinkedListNode {
2- public byte data ;
3- protected LinkedListNode next ;
1+ import java .lang .reflect .Array ;
42
5- public LinkedListNode (byte data , LinkedListNode next ) {
3+ class LinkedListNode <T > {
4+ public T data ;
5+ protected LinkedListNode <T > next ;
6+
7+ public LinkedListNode (T data , LinkedListNode <T > next ) {
68 this .data = data ;
79 this .next = next ;
810 }
@@ -20,7 +22,7 @@ public LinkedListNode(byte data, LinkedListNode next) {
2022 * <li>Delete data at an index: O(n)</li>
2123 * </ul>
2224 */
23- public class LinkedList {
25+ public class LinkedList < T > {
2426 /**
2527 * The head of the linked list which is a <code>LinkedListNode</code> that
2628 * points to the first node of the linked list.
@@ -29,26 +31,26 @@ public class LinkedList {
2931 * Having the head being an empty node rather than the first node in the
3032 * actual list helps clean the code and reduces if statements.
3133 */
32- private LinkedListNode head ;
34+ private LinkedListNode < T > head ;
3335 /**
3436 * The number of nodes in this linked list, excluding the head.
3537 */
3638 private int length ;
3739
3840 public LinkedList () {
39- this .head = new LinkedListNode (( byte ) 0 , null );
41+ this .head = new LinkedListNode < T >( null , null );
4042 this .length = 0 ;
4143 }
4244
43- public LinkedList (byte [] data ) {
44- this .head = new LinkedListNode (( byte ) 0 , null );
45+ public LinkedList (T [] data ) {
46+ this .head = new LinkedListNode < T >( null , null );
4547 this .length = 0 ;
4648
47- for (byte d : data )
49+ for (T d : data )
4850 this .insert (d , this .length );
4951 }
5052
51- public LinkedListNode getHead () {
53+ public LinkedListNode < T > getHead () {
5254 return this .head ;
5355 }
5456
@@ -71,10 +73,10 @@ public boolean isEmpty() {
7173 * @param index
7274 * @return The data stored at the index.
7375 */
74- public byte get (int index ) {
76+ public T get (int index ) {
7577 if (index < 0 || index >= this .length ) throw new IndexOutOfBoundsException ();
7678
77- LinkedListNode node = this .head .next ;
79+ LinkedListNode < T > node = this .head .next ;
7880
7981 for (int i = 0 ; i < index ; i ++)
8082 node = node .next ;
@@ -91,8 +93,8 @@ public byte get(int index) {
9193 * @param data
9294 * @return The new node inserted that contains the data.
9395 */
94- public LinkedListNode insertAtHead (byte data ) {
95- LinkedListNode newNode = new LinkedListNode (data , this .head .next );
96+ public LinkedListNode < T > insertAtHead (T data ) {
97+ LinkedListNode < T > newNode = new LinkedListNode < T > (data , this .head .next );
9698 this .head .next = newNode ;
9799
98100 this .length ++;
@@ -111,11 +113,11 @@ public LinkedListNode insertAtHead(byte data) {
111113 * <code>this.length</code> (inclusive).
112114 * @return The new node inserted that contains the data.
113115 */
114- public LinkedListNode insert (byte data , int index ) {
116+ public LinkedListNode < T > insert (T data , int index ) {
115117 if (index < 0 || index > this .length ) throw new IndexOutOfBoundsException ();
116118
117- LinkedListNode newNode = new LinkedListNode (data , null );
118- LinkedListNode prevNode = this .head ;
119+ LinkedListNode < T > newNode = new LinkedListNode < T > (data , null );
120+ LinkedListNode < T > prevNode = this .head ;
119121
120122 for (int i = 0 ; i < index ; i ++)
121123 prevNode = prevNode .next ;
@@ -136,15 +138,15 @@ public LinkedListNode insert(byte data, int index) {
136138 * @param index
137139 * @return The deleted node.
138140 */
139- public LinkedListNode delete (int index ) {
141+ public LinkedListNode < T > delete (int index ) {
140142 if (index < 0 || index >= this .length ) throw new IndexOutOfBoundsException ();
141143
142- LinkedListNode prevNode = this .head ;
144+ LinkedListNode < T > prevNode = this .head ;
143145
144146 for (int i = 0 ; i < index ; i ++)
145147 prevNode = prevNode .next ;
146148
147- LinkedListNode node = prevNode .next ;
149+ LinkedListNode < T > node = prevNode .next ;
148150 if (index != this .length ) prevNode .next = node .next ;
149151
150152 this .length --;
@@ -156,9 +158,10 @@ public LinkedListNode delete(int index) {
156158 *
157159 * @return
158160 */
159- public byte [] toArray () {
160- byte [] array = new byte [this .length ];
161- LinkedListNode node = this .head ;
161+ @ SuppressWarnings ("unchecked" )
162+ public T [] toArray (Class <T > c ) {
163+ T [] array = (T []) Array .newInstance (c , this .length );
164+ LinkedListNode <T > node = this .head ;
162165
163166 for (int i = 0 ; i < this .length ; i ++) {
164167 node = node .next ;
@@ -167,19 +170,4 @@ public byte[] toArray() {
167170
168171 return array ;
169172 }
170-
171- public static void main (String [] args ) {
172- LinkedList list = new LinkedList ();
173-
174- for (byte i = 0 ; i < 10 ; i ++)
175- list .insert (i , list .getLength ());
176-
177- list .delete (list .getLength ());
178-
179- byte [] array = list .toArray ();
180-
181- for (byte b : array ) {
182- System .out .println (b );
183- }
184- }
185173}
0 commit comments