# include & lt; Stdlib. H>
# include & lt; String. H>
Struct film {
Char title [45].
Int rating;
Struct file * next;
};
Char * s_gets (char * st, int n);
Int main (void)
{
Struct film * head=NULL;
Prev struct film * and * current;
Char input [45].
Puts (" enter first name ");
While (s_gets (45) input, & amp; & Input [0]!='\ 0') {
The current=(struct film *) malloc (sizeof (struct film));
Prev=(struct) film * malloc (sizeof (struct film));
If (head==NULL)
The head=current;
The else
Prev - & gt; Next=current;
Current - & gt; Next=NULL;
Strcpy_s (current - & gt; The title, 45, input);
Puts (" enter first rating ");
Scanf_s (" % d ", & amp; Current - & gt; Rating);
While (getchar ()! )
='\ n'continue;
Puts (" enter next ");
Prev=current;
}
If (head==NULL)
Puts (" there is no moive ");
The else
Puts (" there is a movie list ");
The current=head;
While (current!=NULL) {
Printf (" % s, % d ", current - & gt; The title, the current - & gt; Rating);
The current=current - & gt; next;
}
The current=head;
While (current!=NULL) {
Free (current);
The head=current - & gt; next;
}
return 0;
}
Char * s_gets (char * st, int n) {
Char * ret_val;
Char * find;
int i=0;
Ret_val=the fgets (st, n, stdin);//below is done already input shown in the content of the
If (ret_val) {//the fgets didn't read the file at the end or read
The find=STRCHR (st, '\ n');
If (find)
{
* find='\ 0';
}
The else
While (getchar ()! )
='\ n'continue;
}
Return ret_val;
}
CodePudding user response:
Memory leaks:while (current!=NULL) {
Free (current);
The current=current - & gt; next;
}
CodePudding user response:
And the pre pointer also need not allocate space, should be commented out//prev=(struct film *) malloc (sizeof (struct film));
CodePudding user response:
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; String. H>
Struct film {
Char title [45].
Int rating;
Struct file * next;
};
Char * s_gets (char * st, int n);
Int main (void)
{
Struct film * head=NULL;
Prev struct film * and * current;
Char input [45].
Puts (" enter first name ");
While (s_gets (45) input, & amp; & Input [0]!='\ 0') {
The current=(struct film *) malloc (sizeof (struct film));
//prev=(struct) film * malloc (sizeof (struct film));
If (head==NULL)
The head=current;
The else
Prev - & gt; Next=current;
Current - & gt; Next=NULL;
Strcpy (current - & gt; The title, input);
Puts (" enter first rating ");
The scanf (" % d ", & amp; Current - & gt; Rating);
While (getchar ()! )
='\ n'continue;
Puts (" enter next ");
Prev=current;
}
If (head==NULL)
Puts (" there is no moive ");
The else
Puts (" there is a movie list ");
The current=head;
While (current!=NULL) {
Printf (" % s, % d \ n ", current - & gt; The title, the current - & gt; Rating);
The current=current - & gt; next;
}
The current=head;
While (current!=NULL) {
Free (current);
The current=current - & gt; next;
}
return 0;
}
Char * s_gets (char * st, int n) {
Char * ret_val;
Char * find;
int i=0;
Ret_val=the fgets (st, n, stdin);//below is done already input shown in the content of the
If (ret_val) {//the fgets didn't read the file at the end or read
The find=STRCHR (st, '\ n');
If (find)
{
* find='\ 0';
}
The else
While (getchar ()! )
='\ n'continue;
}
Return ret_val;
}