首页 > Java > java教程 > 正文

Java 中的链表

PHPz
发布: 2024-08-30 15:48:16
原创
750 人浏览过

Java中的LinkedList是与数组不同的线性数据结构。在 Java 程序中使用链表有一定的优点和缺点。链表中的每个元素都存储在称为节点的单元中。每个节点都有一个特定的地址。 LinkedList 的主要缺点是每个点的节点不容易访问。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

每个节点都有指针来寻址,要访问具体的节点,必须从头开始,然后到达要访问的节点的具体指针。与其他 Java 接口一样,LinkedList 类也包含许多构造函数和方法。在本文中,我们将看到 LinkedList 中使用的两个构造函数。

他们是:

  • LinkedList(): 用于分配一个空的 LinkedList()。
  • LinkedList(Collection C):这用于创建一个包含指定集合的​​所有元素的有序列表,由集合的迭代器返回。

Java中LinkedList的方法

Java LinkedList 类中有许多方法或函数。我们将在本文中看到 Java LinkedList 类中的一些函数。

他们是:

  • add(int a, I Element):此方法用于在此列表中的特定位置插入特定元素。
  • add( E e): 此方法将指定元素固定到列表末尾。
  • add(int index, Collection C): 此方法从起始位置开始插入列表中所有指定的元素。
  • offerFirst​(): 此方法将指定元素插入此列表的前面。
  • addLast():此方法用于在列表末尾插入元素。
  • voidclear():该方法用于删除Linkedlist中的所有元素。
  • poll​(): 它删除列表的第一个元素。
  • lastIndexOf​(): 用于返回指定元素在此列表中最后一次出现的索引。
  • getLast(): 该函数用于返回 LinkedList 中的最后一个元素。
  • offer​(): 此方法插入指定元素作为列表的尾部元素。
  • offerLast​(): 此方法在此列表末尾插入指定元素。
  • peek​(): 它检索列表的第一个元素。
  • peekFirst​(): 此方法用于检索列表的最后一个元素,如果列表为空,则返回 null。
  • addFirst(): 该方法用于将元素插入到列表的开头。
  • peekLast​(): 此方法用于检索列表的最后一个元素,如果列表为空,则返回 null。
  • pollFirst​(): 此方法用于检索和删除此列表的第一个元素,如果此列表为空,则返回 null。
  • contains():如果 LinkedList 包含节点处的特定元素,则此函数返回 true。
  • pollLast​(): 此方法删除此列表的最后一个元素,如果此列表为空,则返回 null。
  • removeFirst​(): 此方法返回此列表中的第一个元素。
  • element(): 此方法检索但不删除列表的头部。
  • getFirst():该方法用于返回LinkedList的第一个元素。
  • remove​(): 此方法删除 LinkedList 的第一个元素。
  • remove​(int index): 此方法删除此列表中指定位置的元素。
  • removeLast​(): 此方法返回此列表中的最后一个元素。
  • set​(int index, E element): 此方法将此列表中指定位置的元素替换为指定元素。
  • size​(): 此方法返回此列表中的元素数量。

Java 中 LinkedList 的示例

下面给出了提到的示例:

示例#1

在这个编码示例中,我们将看到 LinkedList 方法,在链表中插入某些元素,然后删除它们,最后显示链表。

代码:

import java.util.*;
public class Example3
{
public static void main(String args[])
{
LinkedList<String> object = new LinkedList<String>();
// Adding elements to the linked list
object.add("A");
object.add("B");
object.addLast("C");
object.addFirst("D");
object.add(2, "E");
object.add("F");
object.add("G");
System.out.println("Linked list : " + object);
object.remove("C");
object.remove(3);
object.removeFirst();
object.removeLast();
System.out.println("Linked list after deletion: " + object);
}
}
登录后复制

输出:

Java 中的链表

In the sample output, we see that there are certain elements in the linkedlist, and finally, certain elements are deleted, and then the linkedlist after all the deletion of the elements is shown.

Example #2

In this program, we are going to see four names being printed using sequential order in LinkedList. We use a String LinkedList and use it to print names that can be of any number. We use the While loop here for printing the names which are present in the program.

Code:

import java.util.*;
public class LinkedList1
{
public static void main(String args[])
{
LinkedList<String> al=new LinkedList<String>();
al.add("Ishankamal Mitra");
al.add("Sourya Mukherjee");
al.add("Satyaki Das");
al.add("Debodooty Sarkar");
Iterator<String> itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
登录后复制

Output:

Java 中的链表

In this program, we check how the coding helps us to print four names in sequential order as mentioned in the LinkedList. In the next program, we are going to see how the sequence is changed; that is, the names are printed in reverse order of the input.

Example #3

In this code, the program inputs the name and then prints the names in the reverse order of their sequence.

Code:

import java.util.*;
public class LinkedList4
{
public static void main(String args[])
{
LinkedList<String> ll=new LinkedList<String>();
ll.add("Ishankamal Mitra");
ll.add("Sourya Mukherjee");
ll.add("Satyaki Das");
//Going through the list of elements in Reverse order
Iterator i=ll.descendingIterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
登录后复制

Output:

Java 中的链表

In this program, we use the DescendingIterator(), and we use it to print the names in the reverse order of the input. We can see it very clearly through the program.

Conclusion

In this article, we saw the different constructors and methods which are present in the LinkedList class. Plus, we saw a Java program to illustrate the insertion and deletion of elements in a LinkedList. We also saw the advantages and disadvantages of using LinkedList over arrays. They contain nodes that are not easily accessible and have to be accessed through the LinkedList head. We also notice three examples of coding where names are printed in reverse order, sequential order, and removing elements from a LinkedList. These programs help us to understand the methodology of the LinkedList class.

以上是Java 中的链表的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!