hamaXD

CPT 7.1

Nov 8th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct song{
  4.   char title[128];
  5.   double duration;
  6. };
  7. typedef struct song Song;
  8.  
  9. struct listnode{
  10.   Song s;
  11.   struct listnode *next;
  12. };
  13. typedef struct listnode LN;
  14.  
  15. void insert(LN **hptr, Song *sp);
  16. LN *find_tail(LN *head);
  17. double sum_duration(LN *head);
  18.  
  19. int main(){
  20.   Song buf,*sp;
  21.   int i;
  22.   LN *head;
  23.   head=NULL;
  24.   sp=(Song*)malloc(sizeof(Song));
  25.   while(i<5){
  26.     scanf("%s",buf.title);
  27.     scanf("%lf",&buf.duration);
  28.     *sp=buf;
  29.     insert(&head,sp);
  30.     i++;
  31.   }
  32.  
  33.   printf("=%.2f",sum_duration(head));
  34.   return 0;
  35. }
  36.  
  37. void insert(LN **hptr, Song *sp){
  38.     LN *new_node = (LN*)malloc(sizeof(LN));
  39.     LN *tail;
  40.     *new_node->s.title=sp->title;
  41.     new_node->s.duration=sp->duration;
  42.     new_node->next=NULL;
  43.     tail=find_tail(*hptr);
  44.     if(tail==NULL)
  45.         *hptr=new_node;
  46.     else
  47.         tail->next=new_node;
  48. }
  49.  
  50. LN *find_tail(LN *head){
  51.     LN *tail;
  52.     if(head==NULL)
  53.         return NULL;
  54.     tail=head;
  55.     while(tail->next!=NULL){
  56.         tail=tail->next;
  57.     }
  58.     return tail;
  59. }
  60.  
  61. double sum_duration(LN *head){
  62.     float num=0;
  63.         while(head!=NULL){
  64.             num=num+head->s.duration;
  65.             head=head->next;
  66.         }
  67.         return num;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment