Home » Program To Search An Element In A Singly Linked List

Program To Search An Element In A Singly Linked List

by Online Tutorials Library

Program to search an element in a singly linked list

Explanation

In this program, we need to search a node in the given singly linked list.

Program to search an element in a singly linked list

To solve this problem, we will traverse through the list using a node current. Current points to head and start comparing searched node data with current node data. If they are equal, set the flag to true and print the message along with the position of the searched node.

For, eg. In the above list, a search node says 4 which can be found at the position 4.

Algorithm

  1. Create a class Node which has two attributes: data and next. Next is a pointer to the next node in the list.
  2. Create another class SearchLinkedList which has two attributes: head and tail.
  3. addNode() will add a new node to the list:
    1. Create a new node.
    2. It first checks, whether the head is equal to null which means the list is empty.
    3. If the list is empty, both head and tail will point to a newly added node.
    4. If the list is not empty, the new node will be added to end of the list such that tail’s next will point to a newly added node. This new node will become the new tail of the list.
  4. searchNode() will search for a node in the list:
    1. Variable i will keep track of the position of the searched node.
    2. The variable flag will store boolean value false.
    3. Node current will point to head node.
    4. Iterate through the loop by incrementing current to current.next and i to i + 1.
    5. Compare each node’s data with the searched node. If a match is found, set flag to true.
    6. If the flag is true, display the position of the searched node.
    7. Else, display the message “Element is not present in the list”.
  5. display() will display the nodes present in the list:
    1. Define a node current which will initially point to head of the list.
    2. Traverse through the list till current points to null.
    3. Display each node by making current to point to node next to it in each iteration.

Solution

Python

Output:

Element is present in the list at the position : 2  Element is not present in the list  

C

Output:

Element is present in the list at the position : 2  Element is not present in the list  

JAVA

Output:

Element is present in the list at the position : 2  Element is not present in the list  

C#

Output:

Element is present in the list at the position : 2  Element is not present in the list  

PHP

Output:

Element is present in the list at the position : 2  Element is not present in the list  

Next Topic#

You may also like