0
#include<stdio.h>
#include<stdlib.h>
struct Node
{
    int col;
    int data;
    struct Node *next;
};
void insert(struct Node *p, int clm, int value)
{
    if(p==NULL)
    { 
        struct Node *t;
        t=(struct Node *)malloc(sizeof(struct Node));
        t->next=NULL;
        t->col=clm;
        t->data=value;
        p=t;
    }else
    {
        struct Node *temp,*t;
        temp=p;
        while(temp->next!=NULL)
        {
            temp=temp->next;
        }
        t=(struct Node *)malloc(sizeof(struct Node));
        t->data=value;
        t->col=clm;
        t->next=NULL;
        temp->next=t;
    }
}
void Display(struct Node *p[])
{
    for(int i=0;i<5;i++)
    {
        struct Node *t;
        t=p[i];
        for(int j=0;j<5;j++)
        {  
            if(t!= NULL)
            {
                if(t->col==j)
                {
                    printf("%d\t",t->data);
                    t=t->next;
                }
            }else
            {
                printf("%d\t",'0');
            }
        }
    printf("\n");
    }
}
void main()
{
    struct Node *a[5]={};
    insert(a[0],0,1);
    insert(a[0],1,1);
    insert(a[1],1,1);
    insert(a[2],2,1);
    insert(a[3],3,1);
    insert(a[4],4,1);
    Display(a);
}

This is code to store sparse matrix in the form of linked list. for this purpose i have created a function to insert elements in linked list. But the changes made inside function does not reflect in actual linked list here for struct Node *a[0] , i have created a link, so it shuld not be NULL now , but when i try to add another link it shows NULL

  • You should pass a pointer to what to modify if you want functions to modify caller's local things. – MikeCAT Apr 09 '22 at 13:01
  • as far as i learned in linked list while passing a pointer as a parameter my tutor was not using & 'ampersand' – Chartered23 Apr 09 '22 at 13:05
  • This is why you should first learn about function scope before proceeding to slightly more complex topics. Then you would know that e.g. `p=t;` has no effect outside the function. – Cheatah Apr 09 '22 at 13:15

0 Answers0