Advertisement
sanpai

Employee Info through Linked List and finding average sal

Aug 27th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<string.h>
  4. struct emp
  5. {
  6.  
  7. char name[10];
  8. char designation[10];
  9. float salary;
  10. struct emp *ptr;
  11. };
  12.  
  13. struct emp * addelement(struct emp *,char name[],char des[],float );
  14. void display(struct emp *);
  15. void average(struct emp *);
  16.  
  17. int main()
  18. {
  19.  
  20. int num,i;
  21. struct emp *head;
  22. float sal;
  23. head=NULL;
  24. char name[10],des[10];
  25. printf("\n Enter the number of employees to be added to the database :");
  26. scanf("%d",&num);
  27. for(i=1;i<=num;i++)
  28. {
  29. printf("Enter the name of the employee :");
  30. scanf("%s",name);
  31. printf("\n Enter the designation of the employee :");
  32. scanf("%s",des);
  33. printf("\n Enter the salary of the employee : ");
  34. scanf("%f",&sal);
  35. head=addelement(head,name,des,sal);
  36. }
  37.  
  38. printf("\n The employee information is as follows ");
  39. display(head);
  40.  
  41. average(head);
  42. }
  43.  
  44. struct emp * addelement(struct emp *head,char name[10],char des[10],float sal)
  45.  
  46. {
  47. struct emp *newnode;
  48.  
  49. newnode=(struct emp *)malloc(sizeof(struct emp));
  50.  
  51. strcpy(newnode->name,name);
  52. strcpy(newnode->designation,des);
  53. newnode->salary=sal;
  54. if(head==NULL)
  55.  
  56. {
  57.  
  58. newnode->ptr=NULL;
  59.  
  60. return newnode;
  61.  
  62. }
  63. else
  64.  
  65. {
  66.  
  67. newnode->ptr=head;
  68.  
  69. return newnode;
  70. }
  71.  
  72. }
  73.  
  74. void display(struct emp *p3)
  75.  
  76. {
  77.  
  78. if(p3==NULL)
  79.  
  80. {
  81.  
  82.  
  83.  
  84. printf("\n The list is empty \n");
  85.  
  86. }
  87.  
  88.  
  89.  
  90. while(p3!=NULL)
  91.  
  92. {
  93.  
  94. printf("\n Employee Name: %s,Employee Designation: %s,salary: %f",p3->name,p3->designation,p3->salary);
  95.  
  96. p3=p3->ptr;
  97.  
  98. }
  99.  
  100. }
  101.  
  102. void average(struct emp *p4)
  103.  
  104. {
  105. float average,sal=0;
  106. int n=0;
  107. if(p4==NULL)
  108. {
  109.  
  110. printf("\n The List is empty");
  111.  
  112. }
  113.  
  114. while(p4!=NULL)
  115. {
  116. if((strcmp(p4->designation,"manager")==0)||(strcmp(p4->designation,"MANAGER")==0))
  117. {
  118. sal=sal+(p4->salary);
  119. n++;
  120. }
  121. p4=p4->ptr;
  122. }
  123. average=(sal)/n;
  124. printf("\n The Average salary of manager in the organisation : %f",average);
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement