C program finding pointer to a node at a particular position

Complete C program finding pointer to a node at a particular position

#include<stdio.h>
#include<stdlib.h>

struct node {   ///struct of a new node
    int data;   ///data
    struct node *next;  ///address
} *head;

void createlist(int n);  ///function to create list
void traverselist();     ///function to display list

int main()
{
    int n;
    printf("Enter the total number of nodes: ");
    scanf("%d",&n);
    createlist(n);
     int k=3;
    traverselist(k);

    return 0;
}
///create a list of n nodes
void createlist( int n)
{
    struct node *newnode, *temp;
    int data , i;
    head=(struct node *)malloc(sizeof(struct node));
    ///allocate memory for head node
    if(head==NULL)
        printf("Unable to allocatae memory!!!\n");
    else{
        ///Input data of node from the user
        printf("Enter the data of node 1: ");
        scanf("%d",&data);

        head->data=data;    ///link the data fieled with data
        head->next=NULL;    ///linked the address fieled to NULL

        temp=head;
        ///create n nodes and add to the linked list
        for(i=2; i<=n; i++)
        {
            newnode=(struct node *)malloc(sizeof(struct node));
            ///allocate memory for newnode
            if(newnode==NULL)
            {
                printf("Unable to allocate memory!!\n");
                break;
            }
            else
            {
                printf("Enter the data of node %d: ",i);
                scanf("%d",&data);

                newnode->data=data; ///link the data fieled of newnode with data
                newnode->next=NULL; ///link the address field of newnode with NULL

                temp->next=newnode;
                temp=temp->next;

            }
        }
    }
}
///Display list
void traverselist(int k)
{
     struct node *temp;

    if(head==NULL)
        printf("List is empty!!!\n");
    else
    {
        temp=head;
        int position=1;
        int i;
        for(i=1; i<k && temp!=NULL; i++)
        {
            temp=temp->next; ///move to the next node
        }
        if(temp==NULL)
            printf("Value of position %d is not found in the list.\n", k);
        else
            printf("Value of position %d is %d.\n",k,temp->data);
    }
}

Leave a Comment