Home >>Cpp Programs >C++ Program to find Nth node in Linked List

C++ Program to find Nth node in Linked List

C++ Program to find Nth node in Linked List

In this example, we'll see a C++ program through which we will find a node in a Linked list.

Algorithm:
  • Step 1: First, declare the recursive function with parameters (Node * head, int data).
  • Step 2: Then, Put the Node *temp = head, int index = 0;
  • Step 3: Repeat Step 4 and Step 5 while (temp!= NULL)
  • Step 4: if(temp -> data == data) then return index.
  • Step 5: else index++ and temp = temp->next; then return -1.
Program:

#include <bits/stdc++.h>
using namespace std;
struct Node{// linked list Node
int data;
Node * next;
};
Node *newNode(int k){ //defining new node
Node *temp = (Node*)malloc(sizeof(Node)); 
temp->data = k; 
temp->next = NULL; 
return temp; 
}
//Used to add new node at the end of the list
Node *addNode(Node* head, int k){
if(head == NULL){
head = newNode(k);
}
else{
Node * temp = head;
Node * node = newNode(k);
while(temp->next!= NULL){
temp = temp->next;
}
temp-> next = node;
}
return head;
}
// Used to create new linked list and return head
Node *createNewLL(){
int cont = 1;
int data;
Node* head = NULL;
while(cont){
cout<<"Enter the data of the Node"<<endl;
cin>>data;
head = addNode(head,data);
cout<<"Do you want to continue?(0/1)"<<endl;
cin>>cont;
}
return head;
}
//Function for finding the node
int findNodeInLL(Node* head, int data){
//Used to keep track of the Node Index
int index = 0;              
Node * temp = head;
//LinkedList traversal for finding the node
while(temp!=NULL){
if(temp->data == data){         
//If element found return index
return index;               
}
temp = temp->next;
index++;
}   
//If element not found
return -1;                  
}
//Driver Main
int main(){
Node * head = createNewLL();
int data;
cout<<"Enter the data of the linked list to be found."<<endl;
cin>>data;
int index = findNodeInLL(head,data);
cout<<"It is present at "<<index<< endl;
return 0;
}

Output:
Enter the data of the Node
1
Do you want to continue?(0/1)
1
Enter the data of the Node
2
Do you want to continue?(0/1)
1
Enter the data of the Node
3
Do you want to continue?(0/1)
1
Enter the data of the Node
4
Do you want to continue?(0/1)
1
Enter the data of the Node
5
Do you want to continue?(0/1)
0
Enter the data of the linked list to be found.
3
It is present at 2

Cpp Programs Fibonacci Series in C++ Armstrong Number in C++ Factorial program in C++ Check Palindrome in C++ Prime Number Program In C++ Reverse Number Program in C++ Sum Of Digits Program in C++ C++ Find C++ protected keyword CPP Program for different ways to print array elements CPP Program to determine the colour of chess square CPP Program to Reverse Number CPP Program to Calculate Power of a Number CPP Program to print all Even and Odd numbers from 1 to N Program to find whether a no is power of two in CPP C++ program to find largest list of prime numbers Auto keyword in Cpp C++ program to print the left Rotation of the array Convert a given binary Tree to Doubly Linked List Delete keys in a Linked list using C++ program How do you delete a linked list in C++ Implement stack with linked list in c++ C++ Program to find first occurrence of a Number Using Recursion in an array C++ program to find Last occurrence of a Number using Recursion in an array C++ Program to find Union of two singly Linked Lists Remove Duplicates from linked list in c++ C++ Program to find Nth node in Linked List Merge sort singly linked list c++ C++ Program to Convert Roman Number to Integer Number C++ Program to find LCM of two numbers C++ Password Generator C++ Program to multiply two numbers without using multiplication operator C++ Program to print all possible subset of a set Sum of all the elements in an array divisible by a given number Print First uppercase letter in a string c++ C++ Program to Find the Number Occurring Odd Number of Times C++ Program for Print address of Variable Using Pointer C++ Program to Find ASCII Value of a Character C++ Classes and Objects Program Create a class method in C++ C++ Create Empty Class Define a class method outside the class definition in C++ How to create multiple objects of a class in C++
No Sidebar ads