I am trying to save a graph in the adjacency list representation and hence, I am using pointers for that purpose. It is not fruitful to write the whole code snippet, hence I am writing the code snippet of that part, which I am failing to implement.
Just for clarification, I'm using the current
so as to be able to append a node at the end, and I don't need to traverse the whole list.
I'm not sure whether this way of assigning pointers works correct or not. Any subtle hint will be of great help. Many thanks !!
int n;
struct node{
int i;
struct node * next;
};
int main(){
printf("Enter nodes");
scanf("%d",&n);
struct node *adjlist=(struct node*)calloc(n,sizeof(struct node));
struct node *current=(struct node*)calloc(n,sizeof(struct node));
struct node *temp=(struct node*)calloc(1,sizeof(struct node));
temp->i=10;
temp->next=0;
/* PROBLEM IS HERE
What I want to do is :
(adjlist+n-2)=temp;
But it reflects the following error :
lvalue required as left operand of assignment
Hence, using the BELOW statement.*/
adjlist[n-2]=*temp;
current[n-2]=*temp;
temp=(struct node*)calloc(1,sizeof(struct node));
temp->i=20;
temp->next=0;
/* The same I want to implement here as well.*/
current[n-2].next=temp;
current[n-2]=*temp;
return 0;
}
Update :
I am trying to print the linked list corresponding to the address (adjlist+n-2)
i.e. the values 10 and 20 should get printed. But only the first value(10) is getting printed. The print function that I am using is :
void printlist(struct node * adjlist){
struct node *list=adjlist+n-2;
while((*list).next!=NULL){
printf ("\t%d",list->i);
list=list->next;}
printf ("\t%d",list->i);
}
Is it my print function that is creating blunder, or error lies somewhere else?