Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

linked list distance between two nodes

typedef struct node {
    int data;
    struct node *next;
} nodal;

nodal *distance(nodal *start) {
    int n1, n2, d1 = 0, d2 = 0, length = 0;
    if (start == NULL) {
        printf("List is empty
");
    } else {
        printf("
Enter the fist node element : ");
        if (scanf("%d", &n1) != 1) {
            printf("invalid input
");
            return start;
        }
        printf("
Enter the second node element : ");
        if (scanf("%d", &n2) != 1) {
            printf("invalid input
");
            return start;
        }
        nodal *ptr = start;
        for (;;) {
            length++;
            if (ptr->data == n1 && !d1) {
                d1 = length;
            } else if (ptr->data == n2) {
                if (d1) {
                    d2 = length;
                    break;
                }
                if (!d2)
                    d2 = length;
            }
            ptr = ptr->next;
            if (ptr == start)
                break;
        }
    }
    if (d1 && d2) {
        int dist = d2 - d1;
        if (dist < 0)
            dist += length;
        printf("The distance between node(%d) and node(%d) is %d
", d1, d2, dist);
    } else {
        if (d2)
            printf("node(%d) not found
", d1);
        if (d1)
            printf("node(%d) not found
", d2);
    }
    return start;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: react Mixed symbols 
Javascript :: react Fractional rating 
Javascript :: substraction js 
Javascript :: react-select is unable to remove selected option after adding value props 
Javascript :: Fix Blurry Canvas on Mobile Phones 
Javascript :: change the input feild name when the div contaoining that field is cloned using jquery 
Javascript :: how to swap two variable values in js 
Javascript :: how to translate the title in js file in magento 2 
Javascript :: js find :invalid inside div 
Javascript :: axios 401 unauthorized refresh token multipal request 
Javascript :: parent.containts js 
Javascript :: convert js to ts 
Javascript :: rails + vue js projcet demo 
Javascript :: browser app get screen siwe 
Javascript :: get form control value in angular 8 
Javascript :: angular data table push not working 
Javascript :: url(image loacation) give a class 
Javascript :: graal.js javascript array in java 
Javascript :: javascript array filter exercises 
Javascript :: https://www.npmjs.com/package/ngx-lightbox 
Javascript :: render eror cant find variable: react 
Javascript :: for await range javascript 
Javascript :: ** in javascript 
Javascript :: concatenate to require string in solidity ethereum 
Javascript :: how to signup users without login in firebase js 
Javascript :: generate product key in js 
Javascript :: How to create a debounce higher order function 
Javascript :: Vue Apexchart LineChart 
Javascript :: flutter enum to json 
Javascript :: circular objects javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =