📜  实现 LinkedList API 的Java程序

📅  最后修改于: 2022-05-13 01:54:49.241000             🧑  作者: Mango

实现 LinkedList API 的Java程序

链表是集合框架的一部分,它存在于Java.util 包中。这个类是 LinkedList 数据结构的一个实现,它是一种线性数据结构,其中元素不存储在连续的位置,每个元素都是一个单独的对象,具有数据部分和地址部分。

什么是链表 API?

  • 链表 API旨在实现链表集合,它是从Java.util 包继承的集合框架的一部分。
  • 该 API 是 list 和 deque 接口的双链表实现。
  • 该 API 实现了所有可选的列表操作,并允许所有元素(包括 null)。
  • 所有操作的执行都符合双向链表的预期。索引到列表中的操作将从开始或结束遍历列表,以更接近指定索引的为准。
  • 下面是实现 LinkedList 集合 API 的Java程序的源代码。

例子:

Java
// Java Program to Implement LinkedList API
 
// Importing utility classes from java.util package
import java.util.*;
import java.util.Collection;
 
// Class
// Main class consisting of all methods
public class LinkedListImpl {
 
    // Member variable of th9s class
    private LinkedList linkedList;
 
    // Constructors of this class
 
    // 1. Default constructor
    public LinkedListImpl()
    {
        linkedList = new LinkedList();
    }
 
    // 2. Parameterized constructor
    public LinkedListImpl(Collection c)
    {
        linkedList = new LinkedList(c);
    }
 
    // Method 1
    // To append specified element to end of this List
    public boolean add(E e)
    {
 
        // Returing the last element from the List
        return linkedList.add(e);
    }
 
    // Method 2
    // To insert specified element at
    // the specified position in this List
    public void add(int index, E element)
    {
        linkedList.add(index, element);
    }
 
    // Method 3
    // To add all the elements in this List
    public boolean addAll(Collection c)
    {
        return linkedList.addAll(c);
    }
 
    // Method 4
    // To add all the elements in this List
    public boolean addAll(int index,
                          Collection c)
    {
        return linkedList.addAll(index, c);
    }
 
    // Method 5
    // to inserts specified element at beginning of this
    // List
    public void addFirst(E e) { linkedList.addFirst(e); }
 
    // Method 6
    // To appends specified element to end of this List
    public void addLast(E e) { linkedList.addLast(e); }
 
    // Method 7
    // Removes all of the elements from this list.
    public void clear() { linkedList.clear(); }
 
    // Method 8
    // Returns a shallow copy of this ArrayList instance.
    public Object clone() { return linkedList.clone(); }
 
    // Method 9
    // Returns true if this list contains the specified
    // element.
    public boolean contains(Object o)
    {
        return linkedList.contains(o);
    }
 
    // Method 10
    // Returns an iterator over the elements in this
    // deque(reverse order)
    public Iterator descendingIterator()
    {
        return linkedList.descendingIterator();
    }
 
    // Method 11
    // Retrieves, but does not remove, the head (first
    // element) of this list.
    public E element() { return linkedList.element(); }
 
    // Method 12
    // Returns the element at the specified position in this
    // list.
    public E get(int index)
    {
        return linkedList.get(index);
    }
 
    // Method 13
    // Returns the first element in this list.
    public E getFirst() { return linkedList.getFirst(); }
 
    // Method 14
    // Returns the last element in this list.
    public E getLast() { return linkedList.getLast(); }
 
    // Method 15
    // Returns the index of the first occurrence of the
    // specified element
    public int indexOf(Object o)
    {
 
        return linkedList.indexOf(o);
    }
 
    // Method 16
    // Returns true if this list contains no elements.
    public boolean isEmpty()
    {
        return linkedList.isEmpty();
    }
 
    // Method 17
    // Returns an iterator over the elements
    //  in this list in proper sequence.
    public Iterator iterator()
    {
        return linkedList.iterator();
    }
 
    // Method 18
    public int lastIndexOf(Object o)
    {
        return linkedList.lastIndexOf(o);
    }
 
    // Method 19
    public ListIterator listIterator()
    {
        return linkedList.listIterator();
    }
 
    // Method 20
    public ListIterator listIterator(int index)
    {
        return linkedList.listIterator(index);
    }
 
    // Method 21
    // Adds the specified element as the tail (last element)
    // of this list.
    public boolean offer(E e)
    {
        return linkedList.offer(e);
    }
 
    // Method 22
    // Inserts the specified element at the front of this
    // list.
    public boolean offerFirst(E e)
    {
        return linkedList.offerFirst(e);
    }
 
    // Method 23
    // Inserts the specified element at the end of this
    // list.
    public boolean offerLast(E e)
    {
        return linkedList.offerLast(e);
    }
 
    // Method 24
    // Retrieves, but does not remove, the head (first
    // element) of this list.
    public E peek() { return linkedList.peek(); }
 
    // Method 25
    public E peekFirst() { return linkedList.peekFirst(); }
 
    // Method 26
    // Retrieves, but does not remove, the last element of
    // this list
 
    public E peekLast() { return linkedList.peekLast(); }
 
    // Method 27
    // Retrieves and removes the head (first element) of
    // this list.
    public E poll() { return linkedList.poll(); }
 
    // Method 28
    // Retrieves and removes the first element of this list,
    // or returns null
    public E pollFirst() { return linkedList.pollFirst(); }
 
    // Method 29
    // Retrieves and removes the last element of this list,
    // or returns null
    public E pollLast() { return linkedList.peekLast(); }
 
    // Method 30
    // Pops an element from the stack represented by this
    // list.
    public E pop() { return linkedList.pop(); }
 
    // Method 31
    // Pushes an element onto the stack represented by this
    // list.
    public void push(E e) { linkedList.push(e); }
 
    // Method 32
    // Removes the element at the specified position in this
    // list.
    public E remove(int index)
    {
 
        return linkedList.remove(index);
    }
 
    // Method 33
    // Removes the first occurrence of the specified element
    // from this list(if present)
    public boolean remove(Object o)
    {
        return linkedList.remove(o);
    }
 
    // Method 34
    public boolean removeAll(Collection c)
    {
 
        return linkedList.removeAll(c);
    }
 
    // Method 35
    // Removes and returns the first element from this list.
    public E removeFirst()
    {
 
        return linkedList.removeFirst();
    }
 
    // Method 36
    // To remove th first occurences in this List
    public boolean removeFirstOccurence(Object o)
    {
 
        return linkedList.removeFirstOccurrence(o);
    }
 
    // Method 37
    // Removes and returns the last element from this list.
    public E removeLast()
    {
 
        return linkedList.removeLast();
    }
 
    // Method 38
    // Removes the last occurrence of the specified element
    // in this list
    public boolean removeLastOccurence(Object o)
    {
 
        return linkedList.removeLastOccurrence(o);
    }
 
    // Method 39
    // Retains only the elements in this list
    // contained in specific position
    public boolean retainAll(Collection c)
    {
 
        return linkedList.removeAll(c);
    }
 
    // Method 40
    // Replaces the element at the specified position
    public E set(int index, E element)
    {
        return linkedList.set(index, element);
    }
 
    // Method 41
    // Returns the number of elements in this list.
    public int size() { return linkedList.size(); }
 
    // Method 42
    // Returns a view of the portion of this list
    public List subList(int fromIndex, int toIndex)
    {
 
        return linkedList.subList(fromIndex, toIndex);
    }
 
    // Method 43
    // Returns an array containing all of the elements
    // in this list(proper sequence)
    public Object[] toArray()
    {
 
        return linkedList.toArray();
    }
 
    // Method 44
    // Returns an array containing all of the elements in
    // this list
    public  T[] toArray(T[] a)
    {
        return linkedList.toArray(a);
    }
 
    // Method 45
    // Main driver method
    public static void main(String... arg)
    {
 
        // Creating an object of above class
        // User-defined
        LinkedListImpl linkedList
            = new LinkedListImpl<>();
 
        // Adding custom elements to above object
 
        // Custom input elements addition
        // using add() and addAll() methods
        linkedList.add(100);
        linkedList.add(20);
        linkedList.addFirst(101);
        linkedList.addLast(200);
 
        // Creating a Set class object of integer type
        Set set = new HashSet();
 
        // Custom input elements addition
        // using add() and addAll() methods
        set.add(101);
        set.add(30);
        set.add(32);
        linkedList.addAll(4, set);
 
        if (linkedList.contains(300))
            System.out.println(
                "the linked list contains 300");
        else
            System.out.println(
                "the linked list does not contain 300");
 
        System.out.println(
            "the elements in descending order is");
        Iterator descendingitr
            = linkedList.descendingIterator();
        while (descendingitr.hasNext()) {
            System.out.print(descendingitr.next() + "\t");
        }
        System.out.println();
 
        System.out.println("the head of this list is "
                           + linkedList.element());
       
        System.out.println("the element at index 2 is "
                           + linkedList.get(2));
       
        System.out.println("the element first pos is  "
                           + linkedList.getFirst());
       
        System.out.println("the element at last pos is"
                           + linkedList.getLast());
 
        System.out.println("the index of element 200 is "
                           + linkedList.indexOf(200));
        System.out.println(
            "the last index of element 101 is "
            + linkedList.lastIndexOf(101));
 
        System.out.println("the elements of list are");
        Iterator itr = linkedList.iterator();
       
        while (itr.hasNext()) {
            System.out.print(itr.next() + "\t");
        }
        System.out.println();
 
        linkedList.offer(45);
        linkedList.offerFirst(32);
        linkedList.offerLast(19);
 
        System.out.println("the head of the linkedlist is "
                           + linkedList.peek());
       
        System.out.println(
            "the first element of linkedList is "
            + linkedList.peekFirst());
       
        System.out.println(
            "the last element of linked List is "
            + linkedList.peekLast());
 
        System.out.println("the elements of list are");
        itr = linkedList.iterator();
       
        while (itr.hasNext()) {
            System.out.print(itr.next() + "\t");
        }
        System.out.println();
 
        System.out.println(
            "the first element of linkedList is (poll) "
            + linkedList.poll());
       
        System.out.println("the first element polled is "
                           + linkedList.pollFirst());
       
        System.out.println("the last element polled is "
                           + linkedList.pollLast());
       
        linkedList.push(36);
       
        System.out.println(
            "the element poped from linked List is "
            + linkedList.pop());
       
        System.out.println(
            "index 3 element removed from list "
            + linkedList.remove(3));
       
        System.out.println(
            "last occurence of 101 removed "
            + linkedList.removeLastOccurence(101));
       
        linkedList.clear();
       
        if (linkedList.isEmpty())
            System.out.println("the linkedList is empty");
        else
            System.out.println(
                "the linked list is not empty");
    }
}



输出 :

程序说明:

  1. 我们创建了一个 Collection 方法 LinkedListImpl 来调用 Linked List 对象。
  2. 我们创建了一个布尔方法 add() 将指定的元素附加到此列表的末尾。
  3. 我们创建了一个 void add() 方法来在此列表中的指定位置插入指定元素。
  4. 我们创建了一个布尔 addAll() 方法来将指定集合中的所有元素追加到此列表的末尾,按照它们由指定集合的迭代器返回的顺序。
  5. 我们创建了一个 void addFirst() 方法来在此列表的开头插入指定的元素。
  6. 我们创建了一个 void addLast() 方法来将指定的元素追加到此列表的末尾。
  7. 我们创建 clear() 方法以从此列表中删除所有元素。
  8. 我们创建了一个 clone() 方法,它返回这个 ArrayList 实例的浅拷贝。
  9. 我们创建了一个 contains() 方法,如果此列表包含指定的元素,则该方法返回 true
  10. 我们创建了一个降序迭代器(),它以相反的顺序返回这个双端队列中元素的迭代器。
  11. 我们创建 element() 方法,该方法检索但不删除此列表的头部(第一个元素)。
  12. 我们创建 getFirst() 方法,该方法返回此列表中的第一个元素。
  13. 我们创建 getLast() 方法,该方法返回此列表中的最后一个元素。
  14. 我们使用对象作为参数创建 indexOf() 方法,该方法返回列表中指定元素第一次出现的索引,如果此列表不包含该元素,则返回 -1。
  15. 如果此列表不包含任何元素,我们创建 isEmpty() 以返回 true。
  16. 我们为 arrayList 创建 iterator() 方法,该方法以适当的顺序返回此列表中元素的迭代器。
  17. 我们创建 lastIndexOf() 以返回指定元素在此或 else 中最后一次出现的索引 -1。
  18. 我们为 ArrayList listIterator 创建 listIterator() 方法来返回一个列表迭代器在这个列表中的元素上。
  19. 我们写 listIterator(int index) 来返回一个列表迭代器在这个列表中的元素上。
  20. 我们编写了 offer() 方法来添加指定的元素作为这个列表的尾部(最后一个元素)。
  21. 我们写 offerFirst(E e) 来在这个列表的前面插入指定的元素。