1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to pass head address of linked list to a function in C programming

Discussion in 'C' started by Panini, Nov 1, 2016.

  1. Panini

    Panini New Member

    Joined:
    Nov 1, 2016
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Hi,
    Could someone explain how to write a function to access and display head node in a one node linked list only? I am learning linked list of my own and I am stuck there. Can someone explain it urgently? Thank you very much in advance.

    I have written following code In Turbo C. Please let me know what is wrong in the code:
    -----------------------------------------------------------------------------------------------------------------------------------
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
    void display(struct node *)
    void main()
    {
                      struct node
                      {
                                        int x;
                                        struct node *next;
                       };
                      struct node *head,*temp;
                      clrscr();
    
                     head=NULL;
       
                     temp=(struct node*)malloc(sizeof(struct node*));
                      temp->x=500;
                     temp->next=NULL;
    
                     head=temp;
    
                     display(head);   \\this is reall not working. Please can some on explain urgently\\             
    
                     getch();           
    }
    void display(struct node *p)
    {
                    struct node *ptr;
                     ptr=p;
    
                     printf("%d",ptr->x);  \\whether to declare a local variable x here to print int value in the node?\\
    }
    -----------------------------------------------------------------------------------------------------------------------------------
     

Share This Page