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

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}


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.


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.


24.4.2 TheMyLinkedList Class


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<String> 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);



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 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.


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. 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值