Advertisement
saira12tabassum19

Untitled

Oct 28th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<conio.h>
  4. int node_num=1;int sum=0;
  5. typedef struct Node node;
  6. struct Node{
  7. int x;
  8. node *next;
  9.  
  10.  
  11. }*head=NULL;
  12. void create()
  13. {
  14.  
  15. int n;
  16. printf("Enter number of nodes:");
  17. scanf("%d",&n);
  18. node *temp,*current;
  19. temp=(node*)malloc(sizeof(node));
  20. printf("Enter data: ");
  21. scanf("%d",&temp->x);
  22. head=temp;
  23. temp->next=NULL;
  24. current=temp;
  25. for(int i=2;i<=n;i++)
  26. {
  27. temp=(node*)malloc(sizeof(node));
  28. scanf("%d",&temp->x);
  29. temp->next=NULL;
  30. current->next=temp;
  31. current=temp;
  32. node_num++;
  33. }
  34.  
  35. }
  36.  
  37.  
  38.  
  39.  
  40. void display()
  41. {
  42.  
  43. node *c=head;
  44. for(int i=1;i<=node_num;i++)
  45. {
  46. sum=sum+c->x;
  47. c=c->next;
  48. }
  49.  
  50.  
  51. }
  52.  
  53. void l_insert()
  54. {
  55. node *temp,*store=head;
  56. temp=(node*)malloc(sizeof(node));
  57. printf("Insert new data:");
  58. scanf("%d",&temp->x);
  59. temp->next=NULL;
  60.  
  61. for(int i=2;i<=node_num;i++)
  62. {
  63. store=store->next;
  64. }
  65. store->next=temp;
  66. node_num++;
  67.  
  68.  
  69.  
  70.  
  71. }
  72.  
  73. int main()
  74. {
  75. create();
  76. l_insert();
  77. display();
  78. printf("Sum of total nodes: %d\n",sum);
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement