Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

find node from pos linkedlist java

// Java program to find n'th node in linked list
 
class Node {
    int data;
    Node next;
    Node(int d)
    {
        data = d;
        next = null;
    }
}
 
class LinkedList {
    Node head; // the head of list
 
    /* Takes index as argument and return data at index*/
    public int GetNth(int index)
    {
        Node current = head;
        int count = 0; /* index of Node we are
                          currently looking at */
        while (current != null)
        {
            if (count == index)
                return current.data;
            count++;
            current = current.next;
        }
 
        /* if we get to this line, the caller was asking
        for a non-existent element so we assert fail */
        assert (false);
        return 0;
    }
 
    /* Given a reference to the head of a list and an int,
       inserts a new Node on the front of the list. */
    public void push(int new_data)
    {
 
        /* 1. alloc the Node and put data*/
        Node new_Node = new Node(new_data);
 
        /* 2. Make next of new Node as head */
        new_Node.next = head;
 
        /* 3. Move the head to point to new Node */
        head = new_Node;
    }
 
    /* Driver code*/
    public static void main(String[] args)
    {
        /* Start with empty list */
        LinkedList llist = new LinkedList();
 
        /* Use push() to construct below list
           1->12->1->4->1  */
        llist.push(1);
        llist.push(4);
        llist.push(1);
        llist.push(12);
        llist.push(1);
 
        /* Check the count function */
        System.out.println("Element at index 3 is "
                           + llist.GetNth(3));
    }
}
Comment

find node from pos linkedlist c++

// C++ program to find n'th
// node in linked list
#include <assert.h>
#include <bits/stdc++.h>
using namespace std;
 
// Link list node
class Node {
public:
    int data;
    Node* next;
};
 
/* Given a reference (pointer to
pointer) to the head of a list
and an int, push a new node on
the front of the list. */
void push(Node** head_ref, int new_data)
{
 
    // allocate node
    Node* new_node = new Node();
 
    // put in the data
    new_node->data = new_data;
 
    // link the old list
    // off the new node
    new_node->next = (*head_ref);
 
    // move the head to point
    // to the new node
    (*head_ref) = new_node;
}
 
// Takes head pointer of
// the linked list and index
// as arguments and return
// data at index
int GetNth(Node* head, int index)
{
 
    Node* current = head;
 
    // the index of the
    // node we're currently
    // looking at
    int count = 0;
    while (current != NULL) {
        if (count == index)
            return (current->data);
        count++;
        current = current->next;
    }
 
    /* if we get to this line,
    the caller was asking
    for a non-existent element
    so we assert fail */
    assert(0);
}
 
// Driver Code
int main()
{
 
    // Start with the
    // empty list
    Node* head = NULL;
 
    // Use push() to construct
    // below list
    // 1->12->1->4->1
    push(&head, 1);
    push(&head, 4);
    push(&head, 1);
    push(&head, 12);
    push(&head, 1);
 
    // Check the count
    // function
    cout << "Element at index 3 is " << GetNth(head, 3);
    return 0;
}
 
// This code is contributed by rathbhupendra
Comment

PREVIOUS NEXT
Code Example
Java :: Java Creating HashMap from Other Maps 
Java :: java code to create square using 2D array 
Java :: java exe ausführen 
Java :: java lambda expression in priorityqueue 
Java :: how to call values from methods in flutter 
Java :: Zooming an Image using OpenCV in Java 
Java :: javax.servlet.ServletException: javax.servlet.jsp.JspException: Missing message for key 
Java :: goodbye java 
Java :: facebook share android Studio 
Java :: for-each Loop Sytnax JAVA 
Java :: spigot self cancelling task 
Java :: number of zeros in a binary array 
Java :: how to create 2d arrazlist in java 
Java :: java to run colored lines 
Java :: how to get single value from input string in java 
Java :: how to add 0 in left padding using regular expression java 
Java :: create a file in java in user home 
Java :: arrays in constructor java 
Java :: how to fix this problem in java - Exception in thread "main" java.lang.IllegalArgumentException: input == null! at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1358) 
Java :: java short data type 
Java :: collapsingtoolbarlayout collapse listener 
Java :: Accept Integer only in if else statement 
Java :: get time until start of next hour in java 
Java :: Fomating .json 
Java :: how to wait in the javaprogram 
Java :: last element array java 
Java :: codegrepper java instanceof 
Java :: java.lang.number is interface or abstract class 
Java :: java gui refresh frame 
Java :: autorest generate java client 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =