12 publicbooleancontains(E e);
13
14 /** Return the element from this list at thespecified index */
15 publicE get(intindex);
16
17 /** Return the index of the first matchingelement in this list.
18 * Return -1 if no match. */
19 public intindexOf(E e);
20
21 /** Return true if this list doesn't containany elements */
22 publicbooleanisEmpty();
23
24 /** Return the index of the last matchingelement in this list
25 * Return -1 if no match. */
26 public intlastIndexOf(E e);
27
28 /** Remove the first occurrence of theelement e from this list.
29 * Shift any subsequent elements to the left.
30 * Return true if the element is removed. */
31 public booleanremove(E e);
32
33 /**Remove the element at the specified position in this list.
34 *Shift any subsequent elements to the left.
35 *Return the element that was removed from the list. */
36 publicE remove(intindex);
37
38 /**Replace the element at the specified position in this list
39 *with the specified element and return the old element. */
40 publicObject set(intindex, E e);
41
42 /**Return the number of elements in this list */
43 public intsize();
44}
Step 3: Create the second node and append itinto the list, as shown in Figure 24.9a. To append the second node to the list,link the first node with the new node. The new node is now the tail node, soyou should move tailto point to this new node, as shown in Figure24.9b.
Step 4: Create the third node and append itto the list, as shown in Figure 24.10a. To append the new node to the list,link the last node in the list with the new node. The new node is now the tailnode, so you should move tailto point to this new node, as shown in Figure24.10b.
24.4.2 TheMyLinkedList Class
LISTING24.5TestMyLinkedList.java
1 public classTestMyLinkedList{
2 /** Main method */
3 public staticvoidmain(String[] args) {
4 // Create a list for strings
5 MyLinkedList<String> list = newMyLinkedList<>();
6
7 // Add elements to the list
8 list.add("America"); // Add it to the list
9 System.out.println("(1) "+ list);
24.4.4MyArrayList vs. MyLinkedList
24.4.5Variations of Linked Lists
The linked list introduced in the preceding sections isknown as a singly linked list.、 A circular, singly linked listis like a singly linked list, except that thepointer of the last node points back to the first node, as shown in Figure24.18a. Note that tailis not needed for circular linked lists. headpoints to the current node in the list. Insertion and deletion take placeat the current node. A good application of a circular linked list is in theoperating system that serves multiple users in a timesharing fashion. Thesystem picks a user from a circular list and grants a small amount of CPU time,then moves on to the next user in the list.