Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues

Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第1张图片

12  publicboolean contains(E e);

13

14  /** Return the element from this list at thespecified index */

15  public E get(int index);

16

17  /** Return the index of the first matchingelement in this list.

18  * Return -1 if no match. */

19  public int indexOf(E e);

20

21  /** Return true if this list doesn't containany elements */

22  publicboolean isEmpty();

23

24  /** Return the index of the last matchingelement in this list

25  * Return -1 if no match. */

26  public int lastIndexOf(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 boolean remove(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  public E remove(int index);

37

38  /**Replace the element at the specified position in this list

39  *with the specified element and return the old element. */

40  public Object set(int index, E e);

41

42  /**Return the number of elements in this list */

43  public int size();

44}

Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第2张图片Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第3张图片Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第4张图片Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第5张图片Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第6张图片Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第7张图片Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第8张图片

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 tail to point to this new node, as shown in Figure24.9b.

Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第9张图片

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 tail to point to this new node, as shown in Figure24.10b.

Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第10张图片

24.4.2 TheMyLinkedList Class

Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第11张图片

LISTING 24.5 TestMyLinkedList.java

1 public class TestMyLinkedList{

2  /** Main method */

public staticvoid main(String[] args) {

4     // Create a list for strings

5     MyLinkedList list = new MyLinkedList<>();

6

7     // Add elements to the list

8     list.add("America"); // Add it to the list

9     System.out.println("(1) " + list);

Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第12张图片

Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第13张图片Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第14张图片Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第15张图片Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第16张图片Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第17张图片

24.4.4MyArrayList vs. MyLinkedList

Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第18张图片

24.4.5Variations of Linked Lists

The linked list introduced in the preceding sections isknown as a singly linked list.、 A circular, singly linked list is 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 tail is not needed for circular linked lists. head points 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.

Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第19张图片

A doubly linked list contains nodes with twopointers. One points to the next node and the other to the previous node, asshown in Figure 24.18b. These two pointers are conveniently called a forward pointer and a backward pointer. Thus, a doubly linkedlist can be traversed forward and backward. 

Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第20张图片Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第21张图片Java学习笔记(24)Implementing Lists,Stacks,Queues,and Priority Queues_第22张图片

你可能感兴趣的:(Java学习笔记)