Advertisement
sanpai

Create a linked list

Aug 13th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<malloc.h>
  3.  
  4. struct node * addelement(struct node * ,int);
  5. void display(struct node *);
  6.  
  7. struct node
  8.  
  9. {
  10.  
  11. int data;
  12. struct node *ptr;
  13. };
  14.  
  15. int main()
  16. {
  17.  
  18. int a,b;
  19. struct node *head;
  20. printf(" \n Enter the elements to be added to the linked list ");
  21. scanf("%d %d",&a,&b);
  22. head=NULL;
  23. head=addelement(head,a);
  24. head=addelement(head,b);
  25. display(head);
  26. }
  27.  
  28. struct node * addelement(struct node *head,int info)
  29. {
  30.  
  31. struct node *newnode;
  32. newnode=(struct node *)malloc(sizeof(struct node));
  33. newnode->data=info;
  34. if(head==NULL)
  35. {
  36.  
  37. newnode->ptr=NULL;
  38. return newnode;
  39. }
  40. else
  41.  
  42. {
  43. newnode->ptr=head;
  44. return newnode;
  45. }
  46. }
  47.  
  48. void display(struct node *head)
  49. {
  50.  
  51. while(head!=NULL)
  52. {
  53. printf("%d\t",head->data);
  54. head=head->ptr;
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement