w3resource

C++ Linked List Exercises: Get Nth node in a Singly Linked List

C++ Linked List: Exercise-8 with Solution

Write a C++ program to get Nth node in a given Singly Linked List.

Test Data:
Original list:
7 5 3 1
Position: 1
Value: 7
Position: 2
Value: 5
Position: 3
Value: 3
Position: 4
Value: 1

Sample Solution:

C++ Code:

#include <iostream>

using namespace std;

struct  Node
{   
    int num;
    Node *next;
 }; //node constructed

  int size = 0;
  void insert(Node** head, int num){
    Node* new_Node = new Node();
    new_Node->num = num;
    new_Node->next = *head;
    *head = new_Node;    
     size++;
}

int get_Nth_Node(Node *head, int value)
{
Node *current_Pointer = head;
int length = 0;
while (current_Pointer != NULL) {
        if (length == value)
            return (current_Pointer->num);
        length++;
        current_Pointer = current_Pointer->next;
}
    return -1;
}
 
  //Display all nodes
  void display_all_nodes(Node* node)
    { 
      while(node!=NULL){
        cout << node->num << " "; 
        node = node->next; 
    } 
}

int main()
{
    Node* head = NULL;
    insert(&head,1);
    insert(&head,3);
    insert(&head,5);
    insert(&head,7);
    cout << "Original list:\n";
    display_all_nodes(head); 
    int pos = 1;
    cout << "\n\nPosition: " << pos;
    int result = get_Nth_Node(head, pos-1);
    cout << "\nValue: " << result;
    pos = 2;
    cout << "\n\nPosition: " << pos;
    result = get_Nth_Node(head, pos-1);
    cout << "\nValue: " << result;
    pos = 3;
    cout << "\n\nPosition: " << pos;
    result = get_Nth_Node(head, pos-1);
    cout << "\nValue: " << result;
    pos = 4;
    cout << "\n\nPosition: " << pos;
    result = get_Nth_Node(head, pos-1);
    cout << "\nValue: " << result;
    cout<<endl;
    return 0;
}

Sample Output:

Original list:
7 5 3 1

Position: 1
Value: 7

Position: 2
Value: 5

Position: 3
Value: 3

Position: 4
Value: 1

Flowchart:

Flowchart: Get Nth node in a Singly Linked List.
Flowchart: Get Nth node in a Singly Linked List.

CPP Code Editor:

Contribute your code and comments through Disqus.

Previous C++ Exercise: Insert new node at the middle of a Singly Linked List.
Next C++ Exercise: Insert a node at any position of a Singly Linked List.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.

C++ Programming: Tips of the Day

Program being compiled differently in 3 major C++ compilers. Which one is right?

GCC is correct, at least according to C++11 lookup rules. 3.4.3.1 [class.qual]/2 specifies that, if the nested name specifier is the same as the class name, it refers to the constructor not the injected class name. It gives examples:

B::A ba;           // object of type A
A::A a;            // error, A::A is not a type name
struct A::A a2;    // object of type A

It looks like MSVC misinterprets it as function-style cast expression creating a temporary C with y as a constructor parameter; and Clang misinterprets it as a declaration of a variable called y of type C.

Ref: https://bit.ly/3ww1Bna

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook