fork download
  1. //A C PROGRAM TO CREATE S LINKED LIST
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. struct node{
  5. int data;
  6. struct node *next;
  7. };
  8. int main(){
  9. struct node *node,*head=NULL,*temp=NULL;
  10. int i,size;
  11. printf("enter no of nodes:");
  12. scanf("%d",&size);
  13. for(i=0;i<size;i++){
  14. node=(struct node*)malloc(sizeof(struct node));
  15. node->next=NULL;
  16. if(node==NULL){
  17. printf("the allocation failed");
  18. return 0;
  19. }
  20. else{
  21. printf("enter%dnode data:",i+1);
  22. scanf("%d",&(node->data));
  23. if(head==NULL){
  24. head=node;
  25. temp=node;
  26. }
  27. else{
  28. temp->next=node;
  29. temp=temp->next;
  30. }
  31. }
  32. }
  33. //printing elements
  34. temp=head;
  35. while(temp!=NULL){
  36. printf("->%d",temp->data);
  37. temp=temp->next;
  38. }
  39.  
  40. }
Success #stdin #stdout 0.01s 5316KB
stdin
4
10
20
30
40
stdout
enter no of nodes:enter1node data:enter2node data:enter3node data:enter4node data:->10->20->30->40