100 sets of grade II C operation questions in the national computer grade examination question bank (set 93)

Set 93:

A one-way linked list with head node has been established in the given program. The fun function will be called many times in the main function. Each time the fun function is called, the data in the tail node of the linked list will be output, and the node will be released to shorten the linked list.
Please fill in the correct content in the underline of the program and delete the underline to make the program get the correct result.
Note: the source program is stored in blank1 under the examinee folder In C.
Do not add or delete lines, nor change the structure of the program!
Given source program:

#include <stdio.h> 
#include <stdlib.h> 
#define N 8 
typedef struct list 
{ int data; 
struct list *next; 
} SLIST; 
void fun( SLIST *p) 
{ SLIST *t, *s; 
t=p->next; s=p; 
while(t->next != NULL) 
{ s=t; 
t=t->___1___; 
} 
printf(" %d ",___2___); 
s->next=NULL; 
free(___3___); 
} 
SLIST *creatlist(int *a) 
{ SLIST *h,*p,*q; int i; 
h=p=(SLIST *)malloc(sizeof(SLIST)); 
for(i=0; i<N; i++) 
{ q=(SLIST *)malloc(sizeof(SLIST)); 
q->data=a[i]; p->next=q; p=q; 
} 
p->next=0; 
return h; 
} 
void outlist(SLIST *h) 
{ SLIST *p; 
p=h->next; 
if (p==NULL) printf("\nThe list is NULL!\n"); 
else 
{ printf("\nHead"); 
do { printf("->%d",p->data); p=p->next; } while(p!=NULL); 
printf("->End\n"); 
} 
} 
main() 
{ SLIST *head; 
int a[N]={11,12,15,18,19,22,25,29}; 
head=creatlist(a); 
printf("\nOutput from head:\n"); outlist(head); 
printf("\nOutput from tail: \n"); 
while (head->next != NULL){ 
fun(head); 
printf("\n\n"); 
printf("\nOutput from head again :\n"); outlist(head); 
} 
} 

Problem solving ideas:
This problem is to output the data at the end of the linked list by calling the function once for the established linked list. There are three places in the program that should be filled with appropriate contents so that the program can run with correct results.
The first part: because this problem requires the output of the data at the end of the linked list, the function uses the while loop statement to find the pointer at the end of the linked list and store it in the temporary variable s. then it is necessary to judge whether the linked list has ended each cycle. If so, exit the cycle and output. Because it is operated through the T pointer variable, so, It is necessary to take the next pointer of T and reassign it to t, so next should be filled in here.
Second: output the data of the last node, so t - > data or (* t) should be filled in data.
The third place: after outputting the data of the last node and deleting this node, the program requires to release memory, so t should be filled in.

Given program modi1 The function fun in C is to output the characters in the string in reverse order without changing the contents of the string.
For example, if the string is abcd, you should output: dcba.
Please correct the error in the program so that it can calculate the correct result.
Note: do not change the main function, do not add or delete lines, and do not change the structure of the program!
Given source program:

#include <stdio.h> 
fun (char a) 
{ if ( *a ) 
{ fun(a+1) ; 
printf("%c" *a) ; 
} 
} 
main( ) 
{ char s[10]="abcd"; 
printf("Pre process string=%s\n Processed string=", s); 
fun(s); printf("\n") ; 
} 

Problem solving ideas:
First: parameter a should be defined as a string pointer.
Second: missing comma in statement.

Please write a function fun. Its function is to compare the length of two strings (do not call the function of calculating the string length provided by C language), and the function returns a longer string. If two strings have the same length, the first string is returned.
For example, if you enter beijing shanghai (the Enter key), the function will return shanghai.
Note: some source programs have the file prog1 In C.
Do not change anything in the main function and other functions, and only fill in the curly brackets of function fun
Several statements you wrote.
Given source program:

#include <stdio.h> 
char *fun ( char *s, char *t) 
{ 
} 
main( ) 
{ char a[20],b[20]; 
printf("Input 1th string:") ; 
gets( a); 
printf("Input 2th string:") ; 
gets( b); 
printf("%s\n",fun (a, b )); 
NONO (); 
} 

Problem solving ideas:
This question is to compare the length of two strings and return the string as required.
The program we give is to use the for loop to judge which of the two strings is longer or equal. The termination value of the loop is whether the string terminator is in the two strings. If so, exit the loop body. Next, judge whether the two strings have terminators at the same time, and return the first string s. if not, judge which string has terminators first, and return the specified string as required.
Reference answer:

char *fun ( char *s, char *t) 
{ 
int i; 
char *p=s, *q=t; 
for(i=0;*p && *q; i++) { 
p++; q++; 
} 
if(*p == 0 && *q == 0) return s ; 
if(*p) return s ; 
else return t ; 
} 

Added by silverspy18 on Fri, 18 Feb 2022 19:39:00 +0200