On the unique methods of LinkedList

Posted by whizard on Sat, 22 Jan 2022 02:07:45 +0100

1, What is a LinkedList collection

LinkedList: the LinkedList class implements the List interface and the Collection interface. It is the link List implementation of the List interface. All elements (including null) are allowed. These operations allow a linked List to be used as a stack, queue, or double ended queue. This class provides queue operations, as well as other stack and double ended queue operations. All operations are performed as required by the double linked List. Indexing in a List traverses the List from the beginning or end (from the end near the specified index).
Note: (1) LinkedList is the implementation of linked list of collection. The linked list is a double linked list (with a next pointer and a prev pointer). The methods in LinkedList can realize the operation of stack and queue
(2) In the LinkedList set, not only the set elements can be obtained sequentially through the linked list, but also the set elements can be obtained through the index.

2, LinkedList collection specific methods

  1. Void addfirst (E) inserts the specified element at the beginning of this list.
    Void addlast (E) adds the specified element to the end of this list.
 		LinkedList<Integer> linked=new LinkedList<>();
        linked.add(1);
        linked.add(2);
        System.out.println("linked: "+linked);
        linked.addFirst(3);
        System.out.println("use addFirst Method after adding elements linked: "+linked);
        linked.addLast(4);
        System.out.println("use addLast Method after adding an element linked: "+linked);

Operation results:

  1. (1) E element() gets but does not remove the header element of this list.
    Note: this method is to get the header element. It has the same result as the peek () method and is equivalent to getFirst(). The only difference between this method and peek () is that if the double ended queue is empty, it will throw an exception.
    (2) E getFirst() returns the first element of this list.
    Note: this method is to obtain the header element. It has the same result as the peekFirst() method and is equivalent to element(). The only difference between this method and peekFirst() is that if the double ended queue is empty, it will throw an exception.
    (3) E getLast() returns the last element of this list.
    Note: this method is to get the tail element, which has the same result as the peekLast() method. The only difference between this method and peekLast() is that if the double ended queue is empty, it will throw an exception.
  		LinkedList<Integer> linked=new LinkedList<>();
        linked.add(1);
        linked.add(2);
        linked.add(3);
        linked.add(4);
        System.out.println("linked: "+linked);
        Integer el = linked.element();
        System.out.println("element Method to get the header element of the collection as:"+el);
        System.out.println("linked: "+linked);
        Integer fir = linked.getFirst();
        System.out.println("getFirst Method to get the header element of the collection as:"+fir);
        Integer last = linked.getLast();
        System.out.println("getLast Method to get the tail element of the collection as:"+last);

Operation results:

Note: E peek() gets but does not remove the header (first element) of this list. E peekFirst() gets but does not remove the first element of this list; If this list is empty, null is returned. E peekLast() gets but does not remove the last element of this list; If this list is empty, null is returned. The operation effect is the same as that of the corresponding E element(), E getFirst() and E getLast() methods, so I won't repeat it

  1. (1) E poll() gets and removes the header element (the first element) of this list
    Note: this method will get the header element and remove it, which is equivalent to the pollFirst method. If the list is empty, it will return null.
    (2) E pollFirst() gets and removes the first element of this list; If this list is empty, null is returned.
    Note: this method will get the header element and remove it. It is equivalent to the poll method. If the list is empty, it will return null.
    (3) E pollLast() gets and removes the last element of this list; If this list is empty, null is returned.
    Description: this method will get the tail element and remove it. If the list is empty, it will return null.
  		LinkedList<Integer> linked=new LinkedList<>();
        linked.add(1);
        linked.add(2);
        linked.add(3);
        linked.add(4);
        System.out.println("linked: "+linked);
        Integer el = linked.poll();
        System.out.println("poll Method to get the header element of the collection as:"+el);
        System.out.println("linked: "+linked);
        Integer fir = linked.pollFirst();
        System.out.println("pollFirst Method to get the header element of the collection as:"+fir);
        System.out.println("linked: "+linked);
        Integer last = linked.pollLast();
        System.out.println("pollLast Method to get the tail element of the collection as:"+last);
        System.out.println("linked: "+linked);

Operation results:

Note: E remove() removes and returns the first element of this list. E removeFirst() removes and returns the first element of this list. E removeLast() removes and returns the last element of this list. The only difference between these methods and the corresponding poll method is that if the double ended queue is empty, it will throw an exception. I won't repeat it here

  1. (1) int lastIndexOf(Object o) returns the index of the last specified element in the list. If the element is not included in the list, it returns - 1.
    (2) int indexOf(Object o) returns the index of the specified element that appears for the first time in this list. If this element is not included in this list, it returns - 1.
		LinkedList<Integer> linked=new LinkedList<>();
        linked.add(1);
        linked.add(2);
        linked.add(3);
        linked.add(1);
        System.out.println("linked: "+linked);
        int fir = linked.indexOf(1);
        System.out.println("linked Index of the first occurrence of 1 in the collection:"+fir);
        int las = linked.lastIndexOf(1);
        System.out.println("linked Index of the last 1 in the collection:"+las);

Operation results:

  1. (1) boolean offer(E e) adds the specified element to the end of this list (the last element).
    Description: insert the specified element into the tail of this LinkedList set; If the addition does not violate the capacity limit, the addition is successful and returns true. If there is no available space, it returns false. When using a LinkedList collection with capacity constraints, this method is usually better than the add method, which may not be able to insert elements, but just throw an exception. This method is equivalent to offerLast().
    (2) boolean offerFirst(E e) inserts the specified element at the beginning of this list.
    Description: insert the specified element into the header of this LinkedList set; If the addition does not violate the capacity limit, the addition is successful and returns true. If there is no available space, it returns false. When using a LinkedList collection with capacity constraints, this method is usually better than the addFirst method, which may not be able to insert elements, but just throw an exception.
    (3) boolean offerLast(E e) inserts the specified element at the end of this list.
    Description: insert the specified element into the tail of this LinkedList set; If the addition does not violate the capacity limit, the addition is successful and returns true. If there is no available space, it returns false. When using a LinkedList set with a capacity limit, this method is usually better than the addLast method, which may not be able to insert elements but just throw an exception. This method is equivalent to offer().
		LinkedList<Integer> linked=new LinkedList<>();
        linked.add(1);
        linked.add(2);
        System.out.println("linked: "+linked);
        linked.offer(3);
        System.out.println("use offer Method after adding 3 elements linked: "+linked);
        linked.offerLast(4);
        System.out.println("use offerLast Method after adding 4 elements linked: "+linked);
        linked.offerFirst(0);
        System.out.println("use offerLast Add 0 element after method linked: "+linked);

Operation results:

  1. (1) E pop() pops an element from the stack represented by this list. That is, return and remove the element
    (2) void push(E e) pushes the element onto the stack represented by this list
    Note: these two methods are only used according to the nature of the stack, which also shows that Linkedlis can simulate a stack, not that LinkedList is a stack implementation.
    When using these two methods, the small end of the index is the Top of the stack, and the stack pressing and out operations start from this time.
		LinkedList<Integer> linked=new LinkedList<>();
        linked.add(1);
        linked.add(2);
        linked.add(3);
        System.out.println("linked: "+linked);
        Integer pop = linked.pop();
        System.out.println("use pop Method gets the element as: "+pop);
        System.out.println("use pop Method gets the element linked: "+linked);
        linked.push(9);
        System.out.println("use push Method after adding an element linked: "+linked);

Operation results:

  1. (1) boolean removeFirstOccurrence(Object o) removes the first occurrence of the specified element from this collection. If the element does not exist, no change will be made. If the removal fails, false will be returned. If the removal is successful, true will be returned
    (2) boolean removeLastOccurrence(Object o) removes the last occurrence of the specified element from this collection (when traversing the list from head to tail). If the element does not exist, no change will be made. If the removal fails, false will be returned. If the removal is successful, true will be returned
		LinkedList<Integer> linked=new LinkedList<>();
        linked.add(2);
        linked.add(1);
        linked.add(3);
        linked.add(1);
        linked.add(4);
        linked.add(1);
        System.out.println("linked: "+linked);
        linked.removeFirstOccurrence(1);
        System.out.println("Remove after the first occurrence of 1 linked: "+linked);
        linked.removeLastOccurrence(1);
        System.out.println("Remove after the last occurrence of 1 linked: "+linked);

Operation results:

  1. E set(int index, E element) replaces the element at the specified position in this set with the specified element. The index position must be within the range of the collection index. If the replacement is successful, the replaced element will be returned, otherwise an exception will be thrown.
 		LinkedList<Integer> list=new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println("list:"+list);
        Integer repalce = list.set(1, 6);
        System.out.println("The replaced elements are:"+repalce);
        System.out.println("use set Method after replacing an element list: "+list);

Operation results:

Topics: Java data structure linked list