Untitled

mail@pastecode.io avatar
unknown
plain_text
22 days ago
1.3 kB
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
//creating a structure to define the datatype of the list 
struct node  
{
        int data;
        struct node* next ;
}*new_node , *temp, *head;
int main ()
{
    int i,j,n,h;
    head=(struct node*)malloc(sizeof(struct node));
    if (head==NULL)
    {
        printf("no memory ");
        exit(0);
    }
    printf("enter the data for the head node:");
    scanf("%d",&h);
    head -> data = h;
    head -> next = NULL;
    temp=head;
    printf("enter the number of elements of the link  list:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        new_node=(struct node*)malloc(sizeof(struct node));
        if (new_node==NULL)
        {
            printf("no memory");
            break;
        }
        printf("enter the data of the %d node",i+1);
        scanf("%d",&j);
        new_node -> data = j;
        new_node -> next = NULL;
        temp -> next = new_node;
        temp = new_node;
        new_node=NULL;
    }
    temp=head;   
    while (temp!=NULL)
    {
        printf("data: %d \n",temp -> data);
        printf("address: %p \n",temp -> next);
        temp=temp->next;
        printf("address: %p \n",temp -> next);
        
    } return 0;
}
Leave a Comment