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;
- Song *input_song();
- void insert(LN **hptr, Song *sp);
- LN *find_tail(LN *head);
- double sum_duration(LN *head);
- int main(){
- printf("main\n");
- LN *head;
- head=NULL;
- Song *sp;
- int i;
- for(i = 0; i < 5; i++){
- sp = input_song(); //malloc, scanf Song in this function
- insert(&head,sp);
- }
- printf("=%.2f",sum_duration(head));
- return 0;
- }
- Song *input_song(){
- printf("input\n");
- Song temp,*sp;
- sp=(Song*)malloc(sizeof(Song));
- printf("input title\n");
- scanf("%s",temp.title);
- printf("title ;%s input duration\n",temp.title);
- scanf("%lf",&temp.duration);
- printf("duration : %lf send insert\n",temp.duration);
- *sp = temp;
- return sp;
- }
- void insert(LN **hptr, Song *sp){
- printf("insert\n");
- LN *new_node = (LN*)malloc(sizeof(LN));
- LN *tail;
- new_node->s=sp;
- new_node->next=NULL;
- tail=find_tail(*hptr);
- if(tail==NULL)
- *hptr=new_node;
- else
- tail->next=new_node;
- }
- LN *find_tail(LN *head){
- printf("find_tail\n");
- LN *tail;
- if(head==NULL)
- return NULL;
- tail=head;
- while(tail->next!=NULL){
- tail=tail->next;
- }
- return tail;
- }
- double sum_duration(LN *head){
- printf("sum_duration\n");
- float num=0;
- while(head!=NULL){
- num=num+head->s->duration;
- head=head->next;
- }
- return num;
- }
Add Comment
Please, Sign In to add comment