Search an element in a Doubly Linked List
Last Updated :
28 Mar, 2022
Given a Doubly linked list(DLL) containing N nodes and an integer X, the task is to find the position of the integer X in the doubly linked list. If no such position found then print -1.
Examples:
Input: 15 <=> 16 <=> 8 <=> 7 <=> 13, X = 8
Output: 3
Explanation: X (= 8) is present at the 3rd node of the doubly linked list.
Therefore, the required output is 3
Input: 5 <=> 3 <=> 4 <=> 2 <=> 9, X = 0
Output: -1
Explanation: X (= 0) is not present in the doubly linked list.
Therefore, the required output is -1
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say pos, to store the position of the node containing data value X in the doubly linked list.
- Initialize a pointer, say temp, to store the head node of the doubly linked list.
- Iterate over the linked list and for every node, check if data value of that node is equal to X or not. If found to be true, then print pos.
- Otherwise, print -1.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
};
void push(Node** head_ref, int new_data)
{
Node* new_node
= (Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL) {
(*head_ref)->prev = new_node;
}
(*head_ref) = new_node;
}
int search(Node** head_ref, int x)
{
Node* temp = *head_ref;
int pos = 0;
while (temp->data != x
&& temp->next != NULL) {
pos++;
temp = temp->next;
}
if (temp->data != x)
return -1;
return (pos + 1);
}
int main()
{
Node* head = NULL;
int X = 8;
push(&head, 14);
push(&head, 9);
push(&head, 8);
push(&head, 15);
push(&head, 18);
cout << search(&head, X);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
Node prev;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = head_ref;
if (head_ref != null )
{
head_ref.prev = new_node;
}
head_ref = new_node;
return head_ref;
}
static int search(Node head_ref, int x)
{
Node temp = head_ref;
int pos = 0 ;
while (temp.data != x
&& temp.next != null )
{
pos++;
temp = temp.next;
}
if (temp.data != x)
return - 1 ;
return (pos + 1 );
}
public static void main(String[] args)
{
Node head = null ;
int X = 8 ;
head = push(head, 14 );
head = push(head, 9 );
head = push(head, 8 );
head = push(head, 15 );
head = push(head, 18 );
System.out.print(search(head, X));
}
}
|
C#
using System;
class GFG{
public class Node
{
public int data;
public Node next;
public Node prev;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = head_ref;
if (head_ref != null )
{
head_ref.prev = new_node;
}
head_ref = new_node;
return head_ref;
}
static int search(Node head_ref, int x)
{
Node temp = head_ref;
int pos = 0;
while (temp.data != x &&
temp.next != null )
{
pos++;
temp = temp.next;
}
if (temp.data != x)
return -1;
return (pos + 1);
}
public static void Main(String[] args)
{
Node head = null ;
int X = 8;
head = push(head, 14);
head = push(head, 9);
head = push(head, 8);
head = push(head, 15);
head = push(head, 18);
Console.Write(search(head, X));
}
}
|
Javascript
<script>
class Node {
constructor()
{
this .data = 0;
this .next = null ;
this .prev = null ;
}
};
function push(head_ref, new_data)
{
var new_node
= new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = (head_ref);
if ((head_ref) != null ) {
(head_ref).prev = new_node;
}
(head_ref) = new_node;
return head_ref;
}
function search( head_ref, x)
{
var temp = head_ref;
var pos = 0;
while (temp.data != x
&& temp.next != null ) {
pos++;
temp = temp.next;
}
if (temp.data != x)
return -1;
return (pos + 1);
}
var head = null ;
var X = 8;
head = push(head, 14);
head = push(head, 9);
head = push(head, 8);
head = push(head, 15);
head = push(head, 18);
document.write( search(head, X));
</script>
|
Python3
class Node:
def __init__( self ):
self .data = 0 ;
self . next = None ;
self .prev = None ;
def push(head_ref, new_data):
new_Node = Node();
new_Node.data = new_data;
new_Node.prev = None ;
new_Node. next = head_ref;
if (head_ref ! = None ):
head_ref.prev = new_Node;
head_ref = new_Node;
return head_ref;
def search(head_ref, x):
temp = head_ref;
pos = 0 ;
while (temp.data ! = x and temp. next ! = None ):
pos + = 1 ;
temp = temp. next ;
if (temp.data ! = x):
return - 1 ;
return (pos + 1 );
if __name__ = = '__main__' :
head = None ;
X = 8 ;
head = push(head, 14 );
head = push(head, 9 );
head = push(head, 8 );
head = push(head, 15 );
head = push(head, 18 );
print (search(head, X));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...