The Linked List Class
The Linked List class extends Abstract Sequential List and implements the List interface. It provides a linked-list data structure. It has the two constructors, shown here': Linked List( ) , Linked List(Collection c) The first constructor builds an empty linked list. The second constructor builds a linked list that is initialized with the elements of the collection c. In addition to the methods that it inherits, the Linked list class defines some useful methods of its own for manipulating and-accessing lists. To-add elements to the start of the list, use add First( ); to add elements to the end, use add Last( ). Their signatures are shown here:
. void add First(Object obj)
void add Last(Object obj)
Here, obj is the item being added. .
To obtain the first element, call get First(). To retrieve the last element, call get Last( ). Their signatures are shown here:
Object get First( )
Object get Last( )
To remove the first element, use removefirst ); to remove the last element, call removal asst ). They are shown here:
Object remove First( )
Object remove Last( )
The following program states several of the methods supported by Linked List: The output from this program is shown here:
11 after deleting first and last: [A2, D, E, C) 11 after change: [A2, D, E chanted, C)
Because Linked List implements the List interface, calls to add(Object) append items to the end of the list, as does add Last ). To insert items at a specific location, use the add(int, Object) form of add( ), as illustrated by the call to add (1, II A2") in the example. Notice how the third element in 11 is changed by employing calls to gct( ) and sct( I. To obtain the current value of an clement, pass gett l'the index at which the element is stored. To assign a new value to that index, pass scott ) the in and its new value.