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

Set 26:

A one-way linked list with head nodes has been established in the given program, and the nodes in the linked list are linked in order according to the data field. The function fun is to delete the nodes with the same data field value in the linked list and keep only one. 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 *h) 
{ SLIST *p, *q; 
p=h->next; 
if (p!=NULL) 
{ q=p->next; 
while(q!=NULL) 
{ if (p->data==q->data) 
{ p->next=q->next; 
free(___1___); 
q=p->___2___; 
} 
else 
{ p=q; 
q=q->___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]={1,2,2,3,4,4,4,5}; 
head=creatlist(a); 
printf("\nThe list before deleting :\n"); outlist(head); 
fun(head); 
printf("\nThe list after deleting :\n"); outlist(head); 
} 

Problem solving ideas:
This question is to investigate the examinee's operation on the linked list, mainly to solve the problem of deleting nodes with the same data field value in the 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 function fun uses two temporary structure pointer variables p and Q to operate the linked list. First, P points to the next pointer at the beginning of the linked list, and Q points to the next pointer of P. then use the while loop statement to judge whether the pointer q is NULL. If the Q pointer points to NULL, the function ends and returns. If it is not NULL, it is necessary to judge whether the data values in P and Q are the same. If the values are the same, it is necessary to
Delete the node, and then continue to judge that the value of the next node is the same. If it is still the same, continue to delete the node until it is different. If the values of the two nodes are different, then p points to q, and q points to the next pointer of q, and then continue the above process.
The method of deleting a node is: first point the next pointer of p to the next pointer of q, then release the memory pointed by the q pointer, and finally
q pointer and then point to p's next pointer to delete a node in the linked list.
Place 1: to release the memory space indicated by the q pointer, fill in q.
The second place: the q pointer points to the next pointer of p. to complete the link again, you should fill in next.
The third point: if the values of the two nodes are different, then q points to the next pointer of Q, and next should be filled in.

Given program modi1 The function fun in C is to sort the n elements in the array from small to large by selection method.
Please correct the error in the program so that it can get 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> 
#define N 20 
void fun(int a[], int n) 
{ int i, j, t, p; 
for (j = 0 ;j < n-1 ;j++) { 
p = j 
for (i = j;i < n; i++) 
if(a[i] < a[p]) 
p = j; 
t = a[p] ; a[p] = a[j] ; a[j] = t; 
} 
} 
main() 
{ 
int a[N]={9,6,8,3,-1},i, m = 5; 
printf("Data before sorting:") ; 
for(i = 0;i < m;i++) printf("%d ",a[i]); printf("\n"); 
fun(a,m); 
printf("Sorted data:") ; 
for(i = 0;i < m;i++) printf("%d ",a[i]); printf("\n"); 
} 

Problem solving ideas:
First place: the semicolon is missing after the statement.
The second place: the position where the minimum value is saved, so it should be changed to: p = i;.

Please write a function fun. Its function is to find all integers between 1 and m (including m) that can be divided by 7 or 11, put them in array a, and return the number of these numbers through n. For example, if the value passed to m is 50, the program
Output: 7 11 14 21 22 28 33 35 42 44 49
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> 
#define M 100 
void fun ( int m, int *a , int *n ) 
{ 
} 
main( ) 
{ int aa[M], n, k; 
fun ( 50, aa, &n ); 
for ( k = 0; k < n; k++ ) 
if((k+1)%20==0) printf("\n"); 
else printf( "%4d", aa[k] ); 
printf("\n") ; 
NONO( ); 
} 

Problem solving ideas:
This question is to investigate the way that candidates can divide a certain number by other numbers, and store the qualified numbers in array a. The program we give is to solve this problem by using for loop statement and modulus.
Reference answer:

#include <stdio.h> 
#define M 100 
void fun ( int m, int *a , int *n ) 
{ 
int i ; 
*n=0 ; 
for(i=7 ; i<=m; i++) 
if((i % 7 == 0) || (i % 11 == 0)) a[(*n)++]=i ; 
} 

Added by Redapple on Fri, 04 Mar 2022 05:47:32 +0200