Advertisement
Osher15151

מבחן לדוגמא - מבנה נתונים שאלה 1

Jan 27th, 2020
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. Node *createnode(int x)
  2. {
  3. Node *new=(Node *)malloc(sizeof(Node));
  4. new->info=x;
  5. new->next=null;
  6. return new;
  7. }
  8.  
  9. int calcavg(list *head)
  10. {
  11. int sum=0;
  12. int cnt=0;
  13. Node *pos = head;
  14. if(pos==NULL)
  15. return -1
  16.  
  17. do{
  18. sum+=pos->info;
  19. cnt++;
  20. pos=pos->next;
  21. return sum/cnt;
  22.  
  23. }while(pos !=head);
  24.  
  25. }//end of calcavg function
  26.  
  27. List *updateMid(list *head)
  28. {
  29. Node *pos=head,*temp;
  30. int avg;
  31. if(head==NULL) return NULL;
  32. if(head->next == head) return head;
  33.  
  34. avg=calcavg(head);
  35. if(head->info==avg) return head;
  36.  
  37. while(pos->next !=head){
  38. if(pos->next == avg)
  39. {
  40. temp = pos->next;
  41. pos->next = pos->next->next;
  42. temp->next = head;
  43. head=temp;
  44. return head;
  45. }
  46.  
  47. pos = pos->next;
  48.  
  49. }//while
  50.  
  51. temp = createnode(avg);
  52. temp -> next = head;
  53. head = temp;
  54. return head;
  55.  
  56. } // Update Mid Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement