Question description A long time ago, there was a desolate village in India. The ancient buildings, streets, and businesses were deserted. The windows were open, and the stairwell was in disarray. You can be sure that it will be a fantastic area for mice to romp about in! People in the community have now chosen to provide high-quality education to young people in order to further the village's growth. As a result, they established a programming language coaching centre. People from the coaching centre are presently performing a test. Create a programme for the GetNth() function, which accepts a linked list and an integer index and returns the data value contained in the node at that index position. Example Input: 1->10->30->14, index = 2 Output: 30 The node at index 2 is 30
C Program


#include<stdio.h>

#include<stdlib.h>

struct node

{

    int data;

    struct node *next;

};

void push(struct node **head,int x)

{

    struct node *temp=(struct node*)malloc(sizeof(struct node));

    temp->data=x;

    temp->next=*head;

    *head=temp;

}

void print(struct node *head)

{

    printf("Linked list:");

    while(head)

    {

        printf("-->%d",head->data);

        head=head->next;

    }

    printf("\n");

}

int GetNth(struct node* head,int index)

{

    int count=1;

    while(head)

    {

        if(count==index)

            return head->data;

        count++;

        head=head->next;

    }

    return -1;

}

int main()

{

    int n,i,x,index;

    struct node *head=NULL;

    scanf("%d",&n);

    for(i=0;i<n;i++)

    {

        scanf("%d",&x);

        push(&head,x);

    }

    scanf("%d",&index);

    print(head);

    printf("Node at index=%d:%d",index,GetNth(head,index));

    return 0;

}