Quote Originally Posted by Hodor View Post
I have no idea what you're trying to do with the while loop... current is never going to become NULL so it's an infinite loop. Get rid of it

But if you know you only ever want 2 nodes you'd use an array. Otherwise you'd write something along the lines of what john.c wrote
following code works for me

Code:
#include<stdio.h>#include<stdlib.h>
  
struct node    
{
   int x;
   struct node *next;   
};
 
int main ()   
{
   struct node *current = NULL; 
   struct node *last = NULL; 
   struct node *head;
     
   current = malloc(sizeof(*current)); 
   last = malloc(sizeof(*last)); 
   
   current->x = 10;
   current->next = last;
        
   last->x = 30;
   last->next = NULL;
  
   head = current;
   
  while (head != NULL)
  {
      printf("%d\n", head->x);
      head = head-> next;
  }
  free(current);
  free(last);
  free(head);
  return 0;
}
I think there is one problem in the code I never check weather memory is allocated or not allocated that should I check before