在链表中搜索元素的Java程序
先决条件: Java中的LinkedList
LinkedList是一种线性数据结构,其中元素不存储在连续的内存位置。每个元素都是一个单独的对象,称为具有数据部分和地址部分的节点。元素使用指针或引用链接。在删除和插入的情况下,链表优于数组,因为它们需要 O(1) 时间进行相应的操作。
链表的优点:
- 插入和删除需要 O(1) 时间。
- 本质上是动态的。
- 内存不是连续的。
句法:
LinkedList variableName = new LinkedList<>();
Example:
LinkedList ll = new LinkedList<>();
任务:
在链表中搜索元素。
方法:
当链表直接提供给我们时,我们可以使用for循环遍历链表,找到元素。万一,如果我们不允许使用预建库,我们需要创建我们自己的链表并搜索元素。
例子:
Input: ll1 = [10, 20, 30, -12, 0, 23, -2, 12]
element = 23
Output: 5
Input: ll2 = [1, 2, 3, 4, 5]
element = 3
Output: 2
以下是我们可以在链表中搜索元素的两种方法。
方法 1:当我们被允许使用内置库时
- 首先,初始化一个链表。
- for 循环用于遍历链表中存在的元素。
下面是上述方法的实现:
Java
// Java Program to find an element in a Linked List
// Importing the Linked List class
import java.util.LinkedList;
class SearchInALinkedList {
public static void main(String[] args)
{
// Initializing the Linked List
LinkedList ll = new LinkedList<>();
// Adding elements to the Linked List
ll.add(1);
ll.add(2);
ll.add(3);
ll.add(4);
ll.add(5);
ll.add(6);
ll.add(7);
// Element to be searched
int element = 4;
// Initializing the answer to the index -1
int ans = -1;
// Traversing through the Linked List
for (int i = 0; i < ll.size(); i++) {
// Eztracting each element in
// the Linked List
int llElement = ll.get(i);
// Checking if the extracted element is equal to
// the element to be searched
if (llElement == element) {
// Assigning the index of the
// element to answer
ans = i;
break;
}
}
// Checking if the element is present in the Linked
// List
if (ans == -1) {
System.out.println("Element not found");
}
else {
System.out.println(
"Element found in Linked List at " + ans);
}
}
}
Java
// A Generic Node class is used to create a Linked List
class Node {
// Data Stored in each Node of the Linked List
E data;
// Pointer to the next node in the Linked List
Node next;
// Node class constructor used to initializes the data
// in each Node
Node(E data) { this.data = data; }
}
class LinkedList {
// Points to the head of the Linked
// List i.e the first element
Node head = null;
int size = 0;
// Addition of elements to the tail of the Linked List
public void add(E element)
{
// Checks whether the head is created else creates a
// new one
if (head == null) {
head = new Node<>(element);
size++;
return;
}
// The Node which needs to be added at
// the tail of the Linked List
Node add = new Node<>(element);
// Storing the instance of the
// head pointer
Node temp = head;
// The while loop takes us to the tail of the Linked
// List
while (temp.next != null) {
temp = temp.next;
}
// New Node is added at the tail of
// the Linked List
temp.next = add;
// Size of the Linked List is incremented as
// the elements are added
size++;
}
// Searches the Linked List for the given element and
// returns it's particular index if found else returns
// -1
public int search(E element)
{
if (head == null) {
return -1;
}
int index = 0;
Node temp = head;
// While loop is used to search the entire Linked
// List starting from the tail
while (temp != null) {
// Returns the index of that particular element,
// if found.
if (temp.data == element) {
return index;
}
// Gradually increases index while
// traversing through the Linked List
index++;
temp = temp.next;
}
// Returns -1 if the element is not found
return -1;
}
}
public class GFG {
public static void main(String[] args) throws Exception
{
// Initializing the Linked List
LinkedList ll = new LinkedList<>();
// Adding elements to the Linked List
ll.add(1);
ll.add(10);
ll.add(12);
ll.add(-1);
ll.add(0);
ll.add(-19);
ll.add(34);
// Element to be searched
int element = -1;
// Searching the Linked
// List for the element
int ans = ll.search(-1);
if (ans == -1) {
System.out.println(
"Element not found in the Linked List");
}
else
System.out.println(
"Element found in the Linked List at "
+ ans);
}
}
输出
Element found in Linked List at 3
时间复杂度: O(n),其中n是链表中存在的元素数。
方法 2:当我们不允许使用内置库时
- 首先,创建一个通用节点类。
- 创建一个 LinkedList 类并将头节点初始化为 null。
- 创建所需的添加和搜索功能。
- 在 main 方法中初始化 LinkedList。
- 使用搜索方法查找元素。
下面是上述方法的实现:
Java
// A Generic Node class is used to create a Linked List
class Node {
// Data Stored in each Node of the Linked List
E data;
// Pointer to the next node in the Linked List
Node next;
// Node class constructor used to initializes the data
// in each Node
Node(E data) { this.data = data; }
}
class LinkedList {
// Points to the head of the Linked
// List i.e the first element
Node head = null;
int size = 0;
// Addition of elements to the tail of the Linked List
public void add(E element)
{
// Checks whether the head is created else creates a
// new one
if (head == null) {
head = new Node<>(element);
size++;
return;
}
// The Node which needs to be added at
// the tail of the Linked List
Node add = new Node<>(element);
// Storing the instance of the
// head pointer
Node temp = head;
// The while loop takes us to the tail of the Linked
// List
while (temp.next != null) {
temp = temp.next;
}
// New Node is added at the tail of
// the Linked List
temp.next = add;
// Size of the Linked List is incremented as
// the elements are added
size++;
}
// Searches the Linked List for the given element and
// returns it's particular index if found else returns
// -1
public int search(E element)
{
if (head == null) {
return -1;
}
int index = 0;
Node temp = head;
// While loop is used to search the entire Linked
// List starting from the tail
while (temp != null) {
// Returns the index of that particular element,
// if found.
if (temp.data == element) {
return index;
}
// Gradually increases index while
// traversing through the Linked List
index++;
temp = temp.next;
}
// Returns -1 if the element is not found
return -1;
}
}
public class GFG {
public static void main(String[] args) throws Exception
{
// Initializing the Linked List
LinkedList ll = new LinkedList<>();
// Adding elements to the Linked List
ll.add(1);
ll.add(10);
ll.add(12);
ll.add(-1);
ll.add(0);
ll.add(-19);
ll.add(34);
// Element to be searched
int element = -1;
// Searching the Linked
// List for the element
int ans = ll.search(-1);
if (ans == -1) {
System.out.println(
"Element not found in the Linked List");
}
else
System.out.println(
"Element found in the Linked List at "
+ ans);
}
}
输出
Element found in the Linked List at 3
时间复杂度: O(n),其中n是链表中存在的元素数。