Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #include <stdio.h>
  2. //structure with two fields
  3. struct node
  4. {
  5. int data;
  6. struct node *next;
  7.  
  8. };
  9. // data type definition
  10. typedef struct node node;
  11. //pointer to node which initially set to NULL
  12. node *head=NULL;
  13.  
  14. //fucntion create and display
  15. void create(int num);
  16. void display();
  17.  
  18.  
  19. main()
  20. {
  21. int n,i,num;
  22.  
  23. printf("enter the no of nodes : ");
  24. scanf ("%d",&n);
  25. for(i=0;i<n;++i)
  26. {
  27. printf("enter the data : ");
  28. scanf("%d",&num);
  29. create(num);
  30.  
  31. }
  32. display();
  33. }
  34.  
  35. void create(int num)
  36. {
  37. printf("n");
  38. if(head==NULL)
  39. {
  40. node *temp=(node*)malloc(sizeof(node));
  41. temp->data=num;
  42. temp->next=head;
  43. head=temp;
  44.  
  45.  
  46. }
  47.  
  48. else
  49. {
  50. node *temp1=head;
  51. while(temp1!=NULL)
  52. {
  53. temp1=temp1->next;
  54.  
  55. }
  56.  
  57. node *ptr=(node*)malloc(sizeof(node));
  58.  
  59. ptr->data=num;
  60. ptr->next=temp1->next;
  61. temp1->next=ptr;
  62.  
  63.  
  64. }
  65.  
  66. }
  67.  
  68. void display()
  69. {
  70. node *temp;
  71. temp=head;
  72.  
  73. printf("list is : ");
  74. while(temp!=NULL)
  75. {
  76. printf("%d->",temp->data);
  77. temp=temp->next;
  78.  
  79. }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement