Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct song{
- char title[128];
- double duration;
- };
- typedef struct song Song;
- struct listnode{
- Song s;
- struct listnode *next;
- };
- typedef struct listnode LN;
- void insert(LN **hptr, Song *sp);
- LN *find_tail(LN *head);
- double sum_duration(LN *head);
- int main(){
- Song buf,*sp;
- int i;
- LN *head;
- head=NULL;
- sp=(Song*)malloc(sizeof(Song));
- while(i<5){
- scanf("%s",buf.title);
- scanf("%lf",&buf.duration);
- *sp=buf;
- insert(&head,sp);
- i++;
- }
- printf("=%.2f",sum_duration(head));
- return 0;
- }
- void insert(LN **hptr, Song *sp){
- LN *new_node = (LN*)malloc(sizeof(LN));
- LN *tail;
- *new_node->s.title=sp->title;
- new_node->s.duration=sp->duration;
- new_node->next=NULL;
- tail=find_tail(*hptr);
- if(tail==NULL)
- *hptr=new_node;
- else
- tail->next=new_node;
- }
- LN *find_tail(LN *head){
- LN *tail;
- if(head==NULL)
- return NULL;
- tail=head;
- while(tail->next!=NULL){
- tail=tail->next;
- }
- return tail;
- }
- double sum_duration(LN *head){
- float num=0;
- while(head!=NULL){
- num=num+head->s.duration;
- head=head->next;
- }
- return num;
- }
Advertisement
Add Comment
Please, Sign In to add comment