内容正文:
编写说明:湖南省普通高校对口招生专业理论考试《计算机应用类专业综合考纲百套卷》,依据《湖南省普通高校对口招生计算机应用类专业综合知识基本要求及考试大纲》编写。每门课程均分三层训练:基础层(具象化支架)拆解考点为微目标,紧扣考纲 “掌握”“理解” 要求编写考点训练卷;巩固层(关联性支架)强化知识交叉与场景关联,按考纲专题编专题训练卷;应用层(引导性支架)聚焦真题突破,结合知识模块与教材编写课程综合卷。
本试卷是第87卷,按《计算机编程语言——C语言》范围和要求编写。具体内容为:①用函数指针变量调用函数;②动态内存分配;③用指针处理链表;④用指针处理链表应用实例。
湖南省《计算机应用类专业综合考纲百套卷》 第87卷
《计算机编程语言——C语言》
指向函数的指针、动态内存分配与指向它的指针变量
考点训练卷
考试时间60分钟 满分100分
班级________姓名________学号________成绩________
一、单项选择题(本大题共15小题,每题2分,共30分)
1.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ int data;
struct Node *next;};
int main()
{ struct Node *p = (struct Node *)malloc(sizeof(struct Node));
p->data = 20;
p->next = NULL;
printf("%d", p->data);
free(p);return 0; }
A. 0 B. 20 C. 随机值 D. 编译报错
2.下列程序代码运行后的输出结果是( )
int add(int a, int b)
{ return a + b;}
int sub(int a, int b)
{ return a - b;}
int main()
{ int (*fp)(int, int);
fp = add;
printf("%d ", fp(15, 6));
fp = sub;
printf("%d", fp(15, 6));
return 0; }
A. 21 9 B. 9 21 C. 21 -9 D. 编译错误
3.下列程序代码运行后的输出结果是( )
struct Node
{ int num;
struct Node *next;};
int main()
{ struct Node n1, n2;
n1.num = 10;
n2.num = 30;
n1.next = &n2;
n2.next = NULL;
printf("%d", n1.next->num);
return 0; }
A. 10 B. 30 C. 0 D. 空值
4.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ int val;struct Node *next;};
void traverse(struct Node *head)
{ struct Node *p = head;
while(p != NULL)
{ printf("%d ", p->val);
p = p->next; }
}
int main()
{ struct Node a, b, c;
a.val = 1; b.val = 2; c.val = 3;
a.next = &b; b.next = &c; c.next = NULL;
traverse(&a);
return 0; }
A. 1 2 3 B. 3 2 1 C. 1 3 D. 无输出
5.下列程序代码运行后的输出结果是( )
int calc(int x)
{ return x * x;}
int main()
{ int (*f)(int) = calc;
printf("%d", f(8));
return 0; }
A.16 B.24 C.8 D.64
6.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ int data; struct Node *next;};
struct Node *createNode(int d)
{ struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = d;
newNode->next = NULL;
return newNode;}
int main()
{ struct Node *head = createNode(50);
printf("%d", head->data);
free(head);
return 0; }
A. 50 B. 0 C. 随机值 D. 编译报错
7.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ int id; struct Node *next;};
int main()
{ struct Node *h = NULL;
struct Node *p = (struct Node *)malloc(sizeof(struct Node));
p->id = 100;
p->next = h;
h = p;
printf("%d", h->id);
free(p); return 0; }
A.100 B.0 C.随机值 D.语法错误
8.下列程序代码运行后的输出结果是( )
struct Node
{ int num; struct Node *next;};
int main()
{ struct Node n1, n2, n3;
n1.num = 11; n2.num = 22; n3.num = 33;
n1.next = &n2;
n2.next = &n3;
printf("%d", n1.next->next->num);
return 0; }
A. 11 B. 22 C. 33 D. 0
9.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ int score; struct Node *next;};
void insertAfter(struct Node *pos, int val)
{ struct Node *newN = (struct Node *)malloc(sizeof(struct Node));
newN->score = val;
newN->next = pos->next;
pos->next = newN;}
int main()
{ struct Node a,b;
a.score = 80; b.score = 90;
a.next = &b; b.next = NULL;
insertAfter(&a, 85);
printf("%d", a.next->score);
return 0; }
A. 80 B. 85 C. 90 D. 0
10.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ char ch; struct Node *next;};
int main()
{ struct Node *head = NULL;
struct Node *p1 = (struct Node*)malloc(sizeof(struct Node));
struct Node *p2 = (struct Node*)malloc(sizeof(struct Node));
p1->ch = 'A'; p2->ch = 'B';
head = p1;
p1->next = p2;
p2->next = NULL;
printf("%c", head->next->ch);
free(p1); free(p2);return 0; }
A. A B. B C. 空字符 D. 乱码
11.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ int data;struct Node *next;};
struct Node *delFirst(struct Node *head)
{ struct Node *p = head;
head = head->next;
free(p);
return head;}
int main()
{ struct Node *h, *n1, *n2;
n1 = (struct Node*)malloc(sizeof(struct Node));
n2 = (struct Node*)malloc(sizeof(struct Node));
n1->data = 6; n2->data = 8;
n1->next = n2; n2->next = NULL;
h = n1;
h = delFirst(h);
printf("%d", h->data);
free(h);return 0;}
A. 6 B. 8 C. 0 D. 随机值
12.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
int main()
{ int *p = (int *)malloc(4 * sizeof(int));
p[0] = 10; p[1] = 20;
printf("%d", *(p + 1));
free(p);
return 0; }
A. 10 B. 20 C. 4 D. 编译错误
13.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{int age;struct Node *next;};
int main()
{ struct Node a = {18, NULL};
struct Node b = {20, &a};
printf("%d", b.next->age);
return 0; }
A.18 B.20 C.38 D.0
14.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ int num;struct Node *next;};
int main()
{ struct Node *head = NULL;
struct Node *p, *q;
p = (struct Node*)malloc(sizeof(struct Node));
q = (struct Node*)malloc(sizeof(struct Node));
p->num = 100; q->num = 200;
p->next = q; q->next = NULL;
head = p;
printf("%d ", head->num);
printf("%d", head->next->num);
free(p); free(q);
return 0; }
A. 100 200 B. 200 100 C. 100 0 D. 200 0
15.下列程序代码运行后的输出结果是( )
int fun1(int x) { return x+1; }
int fun2(int x) { return x-1; }
int main()
{ int (*f)(int);
int a,b;
f = fun1;
a = f(10);
f = fun2;
b = f(a);
printf("%d", b);
return 0;}
A. 10 B. 11 C. 9 D. 12
二、阅读程序,写出运行结果(本大题共10小题。每空2分,合计20分)
1.下列程序的运行结果是__________________。
#include <stdio.h>
#include<malloc.h>
struct Node
{int data; struct Node *next;};
int getLength(struct Node *head)
{ int cnt = 0;
struct Node *p = head;
while(p != NULL)
{ cnt++;
p = p->next;
}
return cnt;
}
int main()
{ struct Node a,b,c,d;
a.next=&b; b.next=&c; c.next=&d; d.next=NULL;
printf("%d", getLength(&a));
return 0; }
2.下列程序的运行结果是__________________。
#include <stdio.h>
#include<malloc.h>
struct Node
{int val;struct Node *next;};
void append(struct Node **head, int v)
{ struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node *p;
newNode->val = v;
newNode->next = NULL;
if(*head == NULL)
{ *head = newNode;
return;}
p = *head;
while(p->next != NULL)
p = p->next;
p->next = newNode;
}
int main()
{ struct Node *h = NULL;
append(&h, 4);
append(&h, 7);
printf("%d %d", h->val, h->next->val);
free(h->next); free(h);
return 0; }
3.下列程序的运行结果是__________________。
#include <stdio.h>
#include<malloc.h>
struct Node {
int data;
struct Node *next;};
struct Node *reverse(struct Node *head) {
struct Node *pre = NULL, *cur = head, *nxt;
while (cur != NULL) {
nxt = cur->next;
cur->next = pre;
pre = cur;
cur = nxt;
}
return pre;
}
int main() {
struct Node c = {8, NULL};
struct Node b = {5, &c};
struct Node a = {2, &b};
struct Node *newHead = reverse(&a);
printf("%d
", newHead->data);
return 0; }
4.下列程序的运行结果是__________________。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{ int val; struct Node *next;}Node;
Node* createList(int arr[], int n)
{ Node *head, *tail;
int i;
if(n <= 0) return NULL;
head = tail = (Node*)malloc(sizeof(Node));
head->val = arr[0];
head->next = NULL;
for(i = 1; i < n; i++)
{ Node *p = (Node*)malloc(sizeof(Node));
p->val = arr[i];
p->next = NULL;
tail->next = p;
tail = p;
}
return head;
}
void delByIndex(Node **head, int idx)
{ Node *p = *head, *pre = NULL;
int cnt = 0;
if(*head == NULL || idx < 0) return;
while(p != NULL && cnt < idx)
{ pre = p;
p = p->next;
cnt++; }
if(p == NULL) return;
if(pre == NULL)
*head = p->next;
else
pre->next = p->next;
free(p);
}
void printList(Node *head)
{
Node *p = head;
while(p != NULL)
{ printf("%d ", p->val);
p = p->next;
}
}
int main()
{ int data[] = {12, 25, 37, 41, 58};
int len = sizeof(data)/sizeof(data[0]);
Node *h = createList(data, len);
delByIndex(&h, 2);
printList(h);
return 0; }
5.下列程序的运行结果是__________________。
#include <stdio.h>
#include <stdlib.h>
typedef struct DNode
{ int num;
struct DNode *prev;
struct DNode *next;
}DNode;
DNode* buildDList(int a, int b, int c)
{ DNode *n1 = (DNode*)malloc(sizeof(DNode));
DNode *n2 = (DNode*)malloc(sizeof(DNode));
DNode *n3 = (DNode*)malloc(sizeof(DNode));
n1->num = a; n2->num = b; n3->num = c;
n1->prev = NULL; n1->next = n2;
n2->prev = n1; n2->next = n3;
n3->prev = n2; n3->next = NULL;
return n3;}
int main()
{ DNode *tail = buildDList(6, 19, 32);
DNode *p = tail;
while(p != NULL)
{ printf("%d ", p->num);
p = p->prev; }
return 0; }
6.下列程序的运行结果是__________________。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{ int data;
struct Node *next;}Node;
void traverse(Node *head, int (*calc)(int))
{ Node *p = head;
while(p != NULL)
{ printf("%d ", calc(p->data));
p = p->next;
}
}
int doubleNum(int x)
{ return x * 2;}
int main()
{ Node n1, n2, n3;
n1.data = 7; n2.data = 11; n3.data = 15;
n1.next = &n2; n2.next = &n3; n3.next = NULL;
traverse(&n1, doubleNum);
return 0; }
7.下列程序的运行结果是__________________。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{ int val; struct Node *next;}Node;
int getLen(Node *head)
{ int cnt = 0;
Node *p = head;
while(p)
{ cnt++;p = p->next;}
return cnt;}
void insertAfterLastTwo(Node *head, int newVal)
{ int len = getLen(head),i;
Node *p = head;
Node *newNode = (Node*)malloc(sizeof(Node));
for(i = 0; i < len - 2; i++)
p = p->next;
newNode->val = newVal;
newNode->next = p->next;
p->next = newNode; }
void show(Node *head)
{ Node *p = head;
while(p)
{ printf("%d ", p->val);
p = p->next;}
}
int main()
{ Node a, b, c, d;
a.val = 3; b.val = 9; c.val = 16; d.val = 22;
a.next = &b; b.next = &c; c.next = &d; d.next = NULL;
insertAfterLastTwo(&a, 45);
show(&a);
return 0; }
8.下列程序的运行结果是__________________。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{ int num; struct Node *next;}Node;
void splitList(Node *head, Node **oddHead, Node **evenHead)
{ Node *oTail = NULL, *eTail = NULL;
Node *p = head;
*oddHead = NULL;
*evenHead = NULL;
while(p)
{ if(p->num % 2 == 1)
{ if(*oddHead == NULL) *oddHead = oTail = p;
else
{ oTail->next = p;
oTail = p; }
}
else
{ if(*evenHead == NULL) *evenHead = eTail = p;
else
{ eTail->next = p;
eTail = p; }
}
p = p->next;
}
if(oTail) oTail->next = NULL;
if(eTail) eTail->next = NULL;
}
void print(Node *h)
{ Node *p = h;
while(p)
{ printf("%d ", p->num);
p = p->next;
}
printf("
");
}
int main()
{ Node *h1, *h2;
Node n1={5},n2={8},n3={13},n4={20},n5={27};
n1.next=&n2; n2.next=&n3;
n3.next=&n4; n4.next=&n5; n5.next=NULL;
splitList(&n1, &h1, &h2);
print(h1);
print(h2);
return 0; }
9.下列程序的运行结果是__________________。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{int data; struct Node *next;}Node;
Node* initList(int arr[], int n)
{ Node *h = (Node*)malloc(sizeof(Node)),*t;
int i;
if(n == 0) return NULL;
h->data = arr[0];
h->next = NULL;
t = h;
for(i = 1; i < n; i++)
{ Node *p = (Node*)malloc(sizeof(Node));
p->data = arr[i];
p->next = NULL;
t->next = p;
t = p;
}
return h;
}
int findSecondMax(Node *head)
{ int max1, max2;
Node *p = head;
max1 = max2 = p->data;
p = p->next;
while(p)
{ if(p->data > max1)
{ max2 = max1;
max1 = p->data; }
else if(p->data < max1 && p->data > max2)
{ max2 = p->data; }
p = p->next;
}
return max2;
}
int main()
{ int buf[] = {44, 18, 62, 35, 62, 29};
Node *head = initList(buf, 6);
printf("%d", findSecondMax(head));
return 0; }
10.下列程序的运行结果是__________________。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{ int val; struct Node *next;}Node;
Node* swapPair(Node *head)
{ Node dummy,*pre;
dummy.next = head;
pre = &dummy;
while(pre->next != NULL && pre->next->next != NULL)
{ Node *cur = pre->next;
Node *nxt = cur->next;
cur->next = nxt->next;
nxt->next = cur;
pre->next = nxt;
pre = cur;
}
return dummy.next;
}
void print(Node *h)
{ Node *p = h;
while(p)
{ printf("%d ", p->val);
p = p->next;
}
}
int main()
{ Node n1={2},n2={7},n3={11},n4={16},n5={23};
Node *newHead = swapPair(&n1);
n1.next=&n2; n2.next=&n3;
n3.next=&n4; n4.next=&n5; n5.next=NULL;
print(newHead);
return 0; }
三、程序填空。按照题目要求,将正确内容填入答题卡相应位置,使程序完整(每空2分,共计50分)
1.(2024年)下列程序的功能是:通过调用函数fun(),将单向链表逆置。若原链表从头到尾的结点数据域依次为1、2、3、4、5;逆置后,从头到尾的结点数据域依次为5、4、3、2、1。
#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef struct node {
int data;
_________①_________
} NODE;
NODE *fun(NODE *h){
NODE *p,*q,*r;
p = h;
if(p == NULL){
return NULL;
}
q = p->next;
_________②_________
while(q){
r = q->next;
_________③_________
p = q;
_________④_________
}
return p;
}
NODE *creatlist(int a[])
{
NODE *h,*p,*q; int i;
h=NULL;
for(i=0;i<N;i++)
{
_________⑤_________
q->data=a[i];
q->next = NULL;
if (h == NULL) h = p = q;
else {p->next = q; p = q;}
}
return h;
}
void outlist(NODE *h)
{
NODE *p;
p=h;
if(p==NULL) printf("The list is NULL!
");
else
{
printf("
Head ");
do
{
printf("->%d",p->data);p=p->next;
}while(p!=NULL);
printf("->End
");
}
}
int main()
{
NODE *head;
int a[N]={1,2,3,4,5};
head=creatlist(a);
printf("
The original list:
");
outlist(head);
head=fun(head);
printf("
The list after inverting :
");
outlist(head);
return 0;
}
2.(2023年)下列程序功能是:输入学生信息与三科成绩,并存放在链表中;输出总成绩最高的学生信息及成绩。例如,若学生信息为:“2023001 张一明 83 92 78;2023002 李小明 85 94 78;2023003 王大民 88 88 78”,则执行程序后,输出总分最高的学生李小明的相关信息。
#include <stdio.h>
#include <stdlib.h>
#define maxNum 3
typedef struct data{
char ID[10]; /*存放学号*/
char name[10]; /*存放姓名*/
int score[3]; /*存放三科成绩*/
struct data *next;
} ______①______
MYDATA *creatData() /*创建存放信息的链表*/
{
MYDATA *p,*q,*h;
int i;
int INPUT=0;
h=q=(MYDATA *)malloc(sizeof(MYDATA));
while(INPUT++<maxNum)
{
p=(MYDATA *)malloc(sizeof(MYDATA));
printf("input No(7 numbers):");
scanf("%s",p->ID);
printf("input name(8 characters):");
scanf("%s",p->name);
printf("input 3 scores:
");
for(i=0;i<3;i++)
scanf("%d",(p->score)+i);
p->next=p;
q->next=p;
______②______
}
q->next=NULL;
return h;
}
void outData(MYDATA *h)/*输出链表存放的信息*/
{
MYDATA *p;
int i;
p=h->next;
while(p!=NULL)
{
printf("No--%s
",p->ID);
printf("Name--%s
",p->name);
for(i=0;i<3;i++)
printf("%d ",(p->score)[i]);
p=p->next;
printf("
");
}
}
void findMax(MYDATA *h)/*输出总成绩最高的学生信息*/
{
MYDATA *p;
int i,pos=0,j=0;
int maxSum=0;
p=h->next;
while(p!=NULL)
{
int sum=0;
for(i=0;i<3;i++)
sum+=(p->score)[i];
if(sum>maxSum)
{
maxSum=sum;
______③______
}
j++;
p=p->next;
}
______④______
printf("the best score is:
");
while(pos--)
p=p->next;
printf("No--%s
",p->ID);
printf("Name--%s
",p->name);
for(i=0;i<3;i++)
printf("%d ",(p->score)[i]);
printf("maxSum=%d
",maxSum);
}
int main()
{
MYDATA *h;
h=creatData();
outData(h);
______⑤______
return 0;
}
3.(2022年)下列程序的主函数中,通过函数调用把N个人的姓名与温度数据放入一个带头节点的链表中,h指向链表的头节点。函数Check()的功能是:找出体温大于37.5的结点(除头结点外),并显示相关信息。请补充程序,使其完整。
#include"stdio.h"
#define N 5
typedef struct ss
{char name[8];/*姓名*/
float t; /*温度*/
___________①___________;
}NODE;
void Check (NODE *h) /*把温度大于37.5的结点信息显示出来*/
{int sum=0;
NODE *p;
__________②_________;
while(p!=NULL)
{
if(_______③_______)
printf("%s,%3.1f
",p->name,p->t);
p=p->next;
}
}
NODE *creatlist(int n)/*创建链表*/
{
NODE *h,*p,*s;
int i;float temp;
h=p=(NODE*)malloc(sizeof(NODE));
for(i=1;i<n;i++)
{
s=(NODE*)malloc(sizeof(NODE));
printf("input name=");
scanf("%s",s->name);
printf("input t=");
scanf("%f",&temp);
s->t=temp;
s->next=p;
__________④_________
p=p->next;
}
__________⑤_________;
return h;
}
void OutList(NODE *h)/*输出链表*/
{
NODE *p;
p=h->next;
printf("
The LIST :
HEAD");
while(p){
printf("->%s,%3.1f",p->name,p->t);
p=p->next;
}
printf("
");
}
int main(void)
{
NODE *head; int sum;
system("CLS");
head=creatlist(N);
OutList(head);
Check(head);
return 0;
}
4.(2026年)下列程序功能是:从一组由逗号隔开的姓名中,按姓氏统计人数并列出名单,规定姓名的第1个汉字为姓氏(在GBK字符编码中,1个汉字占2字节)。输出的第一行为总人数,其后的每行为姓氏、人数及名单。例如:
输入:张三,李四,王五,李小明,王明,刘振邦,刘二,李赵聘婷,李世时,刘杨同心
输出:共有10人
张姓有1人:张三
李姓有4人:李四、李小明、李赵聘婷、李世时
王姓有2人:王五、王明
刘姓有3人:刘振邦、刘二、刘杨同心
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FN_NUM_MAX 500
#define NAME_NUM_MAX 1000
#define NAME_LEN_MAX 101
struct NameNode {
char *pName;
struct NameNode *next;
};
typedef struct {
char firstName[3]; // 姓氏所需存储空间为3字节
int nameNum;
struct NameNode *pHead;
} FirstNameInfo;
// 对输入的姓名字符串进行分割
char **nameSplit(char *nameStr, int *pNameNum) {
int i, k, nameNum, len;
char **namePtrArr = (char **)malloc(NAME_NUM_MAX * sizeof(char *));
char tmpName[NAME_LEN_MAX];
char *pName;
len = strlen(nameStr);
k = 0;
nameNum = 0;
for (i = 0; nameStr[i] != '\0'; i++) {
if (nameStr[i] != ',') {
tmpName[k] = nameStr[i];
k++;
if (nameStr[i + 1] == ',' || nameStr[i + 1] == '\0') {
tmpName[k] = '\0';
__________________①________________
pName = (char *)malloc(NAME_LEN_MAX);
strcpy(pName, tmpName);
namePtrArr[nameNum] = pName;
nameNum++;
k = 0;
}
}
}
*pNameNum = nameNum;
return namePtrArr;
}
void copyFirstName(char *pFirstName, char *pName) {
pFirstName[0] = pName[0];
pFirstName[1] = pName[1];
____________②____________
pFirstName[2] = '\0';
}
int cmpFirstName(char *pFirstName, char *pName) {
if (pFirstName[0] == pName[0] && pFirstName[1] == pName[1])
return 1;
return 0;
}
void dealOneName(char *aName, FirstNameInfo *fniArr, int *pNum) {
int i, k;
struct NameNode *pNew, *pNow, *pPre;
k = *pNum;
for (i = 0; i < k; i++) {
if (cmpFirstName(fniArr[i].firstName, aName) == 1)
break;
}
if (k == 0 || i >= k) {
copyFirstName(fniArr[i].firstName, aName);
fniArr[i].nameNum = 1;
pNew = (struct NameNode *)malloc(sizeof(struct NameNode));
pNew->pName = aName;
pNew->next = NULL;
_____________③______________
fniArr[i].pHead = pNew;
k++;
} else {
fniArr[i].nameNum++;
pNew = (struct NameNode *)malloc(sizeof(struct NameNode));
pNew->pName = aName;
pNew->next = NULL;
pPre = pNow = fniArr[i].pHead;
while (pNow != NULL) {
pPre = pNow;
pNow = pNow->next;
}
pPre->next = pNew;
}
*pNum = k;
}
void showFirstNames(FirstNameInfo aXingNode) {
struct NameNode *p;
printf("%s 姓有 %d 人:", aXingNode.firstName, aXingNode.nameNum);
p = aXingNode.pHead;
while (p != NULL) {
printf("%s", p->pName);
____________④____________
if (p->next != NULL)
printf("、");
p = p->next;
}
printf("
");
}
void destroy(FirstNameInfo *fniArr, int nameNum) {
int i;
struct NameNode *pPre, *pNow;
for (i = 0; i < nameNum; i++) {
pNow = fniArr[i].pHead;
while (pNow != NULL) {
pPre = pNow;
___________⑤__________
pNow = pNow->next;
free(pPre->pName);
free(pPre);
}
}
}
int main() {
int i, n = 0, fnName = 0;
char nameStr[(NAME_LEN_MAX + 1) * NAME_NUM_MAX];
char **namesArr;
FirstNameInfo fniArr[FN_NUM_MAX];
gets(nameStr);
namesArr = nameSplit(nameStr, &n);
for (i = 0; i < n; i++) {
dealOneName(namesArr[i], fniArr, &fnName);
}
printf("共有 %d 人
", n);
for (i = 0; i < fnName; i++) {
showFirstNames(fniArr[i]);
}
destroy(fniArr, fnName);
free(namesArr);
return 0;
}
5.给定一条无序单向链表,依次完成 4 项连续操作:
①环形检测:判断链表是否存在环,若有环则输出提示并终止;无环继续执行;
②奇偶节点拆分:按节点位置下标拆分链表:下标偶数节点一条链,下标奇数节点一条链;
③有序归并:将拆分后的两条子链表,合并为一条升序有序链表;
④原地重排:将最终有序链表,按「最大值→最小值→次大→次小」交替重排。
输出:
原始链表:18 5 29 7 42 11 33
环形检测:无环
下标偶数节点链表:18 29 42 33
下标奇数节点链表:5 7 11
归并后有序链表:5 7 11 18 29 42 33
大小交替重排最终链表:5 33 7 42 11 29 18
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{ int data;
struct Node *next;
} Node;
Node* CreateList(int arr[], int n)// 尾插法创建单链表
{ Node *head, *tail, *newNode;
int i;
if (n <= 0)
{ return NULL; }
head = NULL;
tail = NULL;
for (i = 0; i < n; i++)
{ newNode = (Node*)malloc(sizeof(Node));
newNode->data = arr[i];
newNode->next = NULL;
if (head == NULL)
{ head = tail = newNode; }
else
{ ____________①____________
tail = newNode;
}
}
return head;
}
void PrintList(Node *head)
{
Node *p;
p = head;
while (p != NULL)
{ printf("%d ", p->data);
p = p->next;
}
printf("
");
}
int HasCircle(Node *head)//检测链表是否有环
{
Node *slow, *fast;
slow = head;
fast = head;
while (fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = __________②___________
if (slow == fast)
{
return 1;
}
}
return 0;
}
//按【节点下标奇偶】拆分链表
void SplitByIndex(Node *head, Node **evenHead, Node **oddHead)
{
Node *eTail, *oTail, *p;
int idx;
*evenHead = NULL;
*oddHead = NULL;
eTail = NULL;
oTail = NULL;
p = head;
idx = 0;
while (p != NULL)
{ if (idx % 2 == 0)
{ if (*evenHead == NULL)
{ *evenHead = eTail = p; }
else
{ _________③_________
eTail = p; }
}
else
{ if (*oddHead == NULL)
{ *oddHead = oTail = p; }
else
{ oTail->next = p;
oTail = p; }
}
idx++;
__________④_________
}
if (eTail)
{ eTail->next = NULL; }
if (oTail)
{ oTail->next = NULL; }
}
Node* MergeTwoSorted(Node *l1, Node *l2)// 归并两个升序链表
{ Node dummy;
Node *cur;
dummy.next = NULL;
cur = &dummy;
while (l1 != NULL && l2 != NULL)
{ if (l1->data < l2->data)
{
cur->next = l1;
l1 = l1->next;
}
else
{
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
cur->next = l1 ? l1 : l2;
return dummy.next;
}
Node* ReverseList(Node *head)// 链表原地逆序
{
Node *pre, *cur, *nxt;
pre = NULL;
cur = head;
while (cur != NULL)
{ nxt = cur->next;
cur->next = pre;
pre = cur;
cur = nxt;
}
return pre;
}
Node* GetMid(Node *head)
{
Node *slow, *fast;
slow = head;
fast = head;
while (______________⑤______________)
{ slow = slow->next;
fast = fast->next->next;
}
return slow;
}
void ReArrange(Node *head)
{ Node *mid, *l2, *p1, *p2, *n1, *n2;
if (head == NULL || head->next == NULL)
{ return; }
mid = GetMid(head);
l2 = mid->next;
mid->next = NULL;
l2 = ReverseList(l2);
p1 = head;
p2 = l2;
while (p2 != NULL)
{ n1 = p1->next;
n2 = p2->next;
p1->next = p2;
p2->next = n1;
p1 = n1;
p2 = n2;
}
}
void FreeList(Node *head)
{ Node *p, *q;
p = head;
while (p != NULL)
{ q = p->next;
free(p);
p = q;
}
}
int main()
{ int data[] = {18, 5, 29, 7, 42, 11, 33};
int len;
Node *head, *even, *odd, *mergeHead;
len = sizeof(data) / sizeof(data[0]);
head = CreateList(data, len);
printf("原始链表:");
PrintList(head);
if (HasCircle(head))
{ printf("检测到链表存在环,程序终止!
");
FreeList(head);
return 0; }
printf("环形检测:无环
");
SplitByIndex(head, &even, &odd);
printf("下标偶数节点链表:");
PrintList(even);
printf("下标奇数节点链表:");
PrintList(odd);
mergeHead = MergeTwoSorted(even, odd);
printf("归并后有序链表:");
PrintList(mergeHead);
ReArrange(mergeHead);
printf("大小交替重排最终链表:");
PrintList(mergeHead);
FreeList(mergeHead);
return 0; }
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
学科网(北京)股份有限公司
学科网(北京)股份有限公司
$
编写说明:湖南省普通高校对口招生专业理论考试《计算机应用类专业综合考纲百套卷》,依据《湖南省普通高校对口招生计算机应用类专业综合知识基本要求及考试大纲》编写。每门课程均分三层训练:基础层(具象化支架)拆解考点为微目标,紧扣考纲 “掌握”“理解” 要求编写考点训练卷;巩固层(关联性支架)强化知识交叉与场景关联,按考纲专题编专题训练卷;应用层(引导性支架)聚焦真题突破,结合知识模块与教材编写课程综合卷。
本试卷是第87卷,按《计算机编程语言——C语言》范围和要求编写。具体内容为:①用函数指针变量调用函数;②动态内存分配;③用指针处理链表;④用指针处理链表应用实例。
湖南省《计算机应用类专业综合考纲百套卷》 第87卷
《计算机编程语言——C语言》
指向函数的指针、动态内存分配与指向它的指针变量
考点训练卷
考试时间60分钟 满分100分
班级________姓名________学号________成绩________
一、单项选择题(本大题共15小题,每题2分,共30分)
1.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ int data;
struct Node *next;};
int main()
{ struct Node *p = (struct Node *)malloc(sizeof(struct Node));
p->data = 20;
p->next = NULL;
printf("%d", p->data);
free(p);return 0; }
A. 0 B. 20 C. 随机值 D. 编译报错
【答案】B
【解析】动态开辟节点,赋值后正常打印数据,最后释放内存,操作规范,输出20。
2.下列程序代码运行后的输出结果是( )
int add(int a, int b)
{ return a + b;}
int sub(int a, int b)
{ return a - b;}
int main()
{ int (*fp)(int, int);
fp = add;
printf("%d ", fp(15, 6));
fp = sub;
printf("%d", fp(15, 6));
return 0; }
A. 21 9 B. 9 21 C. 21 -9 D. 编译错误
【答案】A
【解析】fp是函数指针变量,先后指向加法、减法函数,15+6=21,15-6=9。
3.下列程序代码运行后的输出结果是( )
struct Node
{ int num;
struct Node *next;};
int main()
{ struct Node n1, n2;
n1.num = 10;
n2.num = 30;
n1.next = &n2;
n2.next = NULL;
printf("%d", n1.next->num);
return 0; }
A. 10 B. 30 C. 0 D. 空值
【答案】B
【解析】两个普通结构体节点构成链表,n1.next指向节点n2,访问其成员num得到30。
4.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ int val;struct Node *next;};
void traverse(struct Node *head)
{ struct Node *p = head;
while(p != NULL)
{ printf("%d ", p->val);
p = p->next; }
}
int main()
{ struct Node a, b, c;
a.val = 1; b.val = 2; c.val = 3;
a.next = &b; b.next = &c; c.next = NULL;
traverse(&a);
return 0; }
A. 1 2 3 B. 3 2 1 C. 1 3 D. 无输出
【答案】A
【解析】构建单向链表 a→b→c,遍历函数从头节点开始依次打印所有节点数据。
5.下列程序代码运行后的输出结果是( )
int calc(int x)
{ return x * x;}
int main()
{ int (*f)(int) = calc;
printf("%d", f(8));
return 0; }
A.16 B.24 C.8 D.64
【答案】D
【解析】f为指向单参数整型函数的指针,指向calc,计算8*8=64。
6.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ int data; struct Node *next;};
struct Node *createNode(int d)
{ struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = d;
newNode->next = NULL;
return newNode;}
int main()
{ struct Node *head = createNode(50);
printf("%d", head->data);
free(head);
return 0; }
A. 50 B. 0 C. 随机值 D. 编译报错
【答案】A
【解析】程序通过 createNode 函数动态分配内存并成功创建了一个数据域 data 为 50 的节点,head 指针指向该节点。随后在 main 函数中直接通过 head->data 访问并打印该值,因此输出结果为 50;最后调用 free 释放内存。
7.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ int id; struct Node *next;};
int main()
{ struct Node *h = NULL;
struct Node *p = (struct Node *)malloc(sizeof(struct Node));
p->id = 100;
p->next = h;
h = p;
printf("%d", h->id);
free(p); return 0; }
A.100 B.0 C.随机值 D.语法错误
【答案】A
【解析】向空链表头部插入新节点,头指针h指向新节点,打印节点数据100。
8.下列程序代码运行后的输出结果是( )
struct Node
{ int num; struct Node *next;};
int main()
{ struct Node n1, n2, n3;
n1.num = 11; n2.num = 22; n3.num = 33;
n1.next = &n2;
n2.next = &n3;
printf("%d", n1.next->next->num);
return 0; }
A. 11 B. 22 C. 33 D. 0
【答案】C
【解析】n1.next指向 n2,n1.next->next指向 n3,取出 n3 的num值 33。
9.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ int score; struct Node *next;};
void insertAfter(struct Node *pos, int val)
{ struct Node *newN = (struct Node *)malloc(sizeof(struct Node));
newN->score = val;
newN->next = pos->next;
pos->next = newN;}
int main()
{ struct Node a,b;
a.score = 80; b.score = 90;
a.next = &b; b.next = NULL;
insertAfter(&a, 85);
printf("%d", a.next->score);
return 0; }
A. 80 B. 85 C. 90 D. 0
【答案】B
【解析】在节点a之后插入新节点,a的后继节点变为新节点,打印其分数85。
10.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ char ch; struct Node *next;};
int main()
{ struct Node *head = NULL;
struct Node *p1 = (struct Node*)malloc(sizeof(struct Node));
struct Node *p2 = (struct Node*)malloc(sizeof(struct Node));
p1->ch = 'A'; p2->ch = 'B';
head = p1;
p1->next = p2;
p2->next = NULL;
printf("%c", head->next->ch);
free(p1); free(p2);return 0; }
A. A B. B C. 空字符 D. 乱码
【答案】B
【解析】链表head→p1→p2,head->next指向 p2,取出字符B。
11.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ int data;struct Node *next;};
struct Node *delFirst(struct Node *head)
{ struct Node *p = head;
head = head->next;
free(p);
return head;}
int main()
{ struct Node *h, *n1, *n2;
n1 = (struct Node*)malloc(sizeof(struct Node));
n2 = (struct Node*)malloc(sizeof(struct Node));
n1->data = 6; n2->data = 8;
n1->next = n2; n2->next = NULL;
h = n1;
h = delFirst(h);
printf("%d", h->data);
free(h);return 0;}
A. 6 B. 8 C. 0 D. 随机值
【答案】B
【解析】删除链表首节点,原第二个节点变为新头节点,数据为 8。
12.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
int main()
{ int *p = (int *)malloc(4 * sizeof(int));
p[0] = 10; p[1] = 20;
printf("%d", *(p + 1));
free(p);
return 0; }
A. 10 B. 20 C. 4 D. 编译错误
【答案】B
【解析】动态开辟 4 个 int 空间,p+1指向第二个元素,取值 20。
13.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{int age;struct Node *next;};
int main()
{ struct Node a = {18, NULL};
struct Node b = {20, &a};
printf("%d", b.next->age);
return 0; }
A.18 B.20 C.38 D.0
【答案】A
【解析】节点 b 的 next 指向节点 a,访问age成员得到 18。
14.下列程序代码运行后的输出结果是( )
#include <stdio.h>
#include<malloc.h>
struct Node
{ int num;struct Node *next;};
int main()
{ struct Node *head = NULL;
struct Node *p, *q;
p = (struct Node*)malloc(sizeof(struct Node));
q = (struct Node*)malloc(sizeof(struct Node));
p->num = 100; q->num = 200;
p->next = q; q->next = NULL;
head = p;
printf("%d ", head->num);
printf("%d", head->next->num);
free(p); free(q);
return 0; }
A. 100 200 B. 200 100 C. 100 0 D. 200 0
【答案】A
【解析】链表依次为 p、q,分别打印两个节点的数据。
15.下列程序代码运行后的输出结果是( )
int fun1(int x) { return x+1; }
int fun2(int x) { return x-1; }
int main()
{ int (*f)(int);
int a,b;
f = fun1;
a = f(10);
f = fun2;
b = f(a);
printf("%d", b);
return 0;}
A. 10 B. 11 C. 9 D. 12
【答案】A
【解析】第一步10+1=11,第二步11-1=10,最终输出 10。
二、阅读程序,写出运行结果(本大题共10小题。每空2分,合计20分)
1.下列程序的运行结果是__________________。
#include <stdio.h>
#include<malloc.h>
struct Node
{int data; struct Node *next;};
int getLength(struct Node *head)
{ int cnt = 0;
struct Node *p = head;
while(p != NULL)
{ cnt++;
p = p->next;
}
return cnt;
}
int main()
{ struct Node a,b,c,d;
a.next=&b; b.next=&c; c.next=&d; d.next=NULL;
printf("%d", getLength(&a));
return 0; }
【答案】4
2.下列程序的运行结果是__________________。
#include <stdio.h>
#include<malloc.h>
struct Node
{int val;struct Node *next;};
void append(struct Node **head, int v)
{ struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node *p;
newNode->val = v;
newNode->next = NULL;
if(*head == NULL)
{ *head = newNode;
return;}
p = *head;
while(p->next != NULL)
p = p->next;
p->next = newNode;
}
int main()
{ struct Node *h = NULL;
append(&h, 4);
append(&h, 7);
printf("%d %d", h->val, h->next->val);
free(h->next); free(h);
return 0; }
【答案】4 7
3.下列程序的运行结果是__________________。
#include <stdio.h>
#include<malloc.h>
struct Node {
int data;
struct Node *next;};
struct Node *reverse(struct Node *head) {
struct Node *pre = NULL, *cur = head, *nxt;
while (cur != NULL) {
nxt = cur->next;
cur->next = pre;
pre = cur;
cur = nxt;
}
return pre;
}
int main() {
struct Node c = {8, NULL};
struct Node b = {5, &c};
struct Node a = {2, &b};
struct Node *newHead = reverse(&a);
printf("%d
", newHead->data);
return 0; }
【答案】8
4.下列程序的运行结果是__________________。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{ int val; struct Node *next;}Node;
Node* createList(int arr[], int n)
{ Node *head, *tail;
int i;
if(n <= 0) return NULL;
head = tail = (Node*)malloc(sizeof(Node));
head->val = arr[0];
head->next = NULL;
for(i = 1; i < n; i++)
{ Node *p = (Node*)malloc(sizeof(Node));
p->val = arr[i];
p->next = NULL;
tail->next = p;
tail = p;
}
return head;
}
void delByIndex(Node **head, int idx)
{ Node *p = *head, *pre = NULL;
int cnt = 0;
if(*head == NULL || idx < 0) return;
while(p != NULL && cnt < idx)
{ pre = p;
p = p->next;
cnt++; }
if(p == NULL) return;
if(pre == NULL)
*head = p->next;
else
pre->next = p->next;
free(p);
}
void printList(Node *head)
{
Node *p = head;
while(p != NULL)
{ printf("%d ", p->val);
p = p->next;
}
}
int main()
{ int data[] = {12, 25, 37, 41, 58};
int len = sizeof(data)/sizeof(data[0]);
Node *h = createList(data, len);
delByIndex(&h, 2);
printList(h);
return 0; }
【答案】12 25 41 58
5.下列程序的运行结果是__________________。
#include <stdio.h>
#include <stdlib.h>
typedef struct DNode
{ int num;
struct DNode *prev;
struct DNode *next;
}DNode;
DNode* buildDList(int a, int b, int c)
{ DNode *n1 = (DNode*)malloc(sizeof(DNode));
DNode *n2 = (DNode*)malloc(sizeof(DNode));
DNode *n3 = (DNode*)malloc(sizeof(DNode));
n1->num = a; n2->num = b; n3->num = c;
n1->prev = NULL; n1->next = n2;
n2->prev = n1; n2->next = n3;
n3->prev = n2; n3->next = NULL;
return n3;}
int main()
{ DNode *tail = buildDList(6, 19, 32);
DNode *p = tail;
while(p != NULL)
{ printf("%d ", p->num);
p = p->prev; }
return 0; }
【答案】32 19 6
6.下列程序的运行结果是__________________。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{ int data;
struct Node *next;}Node;
void traverse(Node *head, int (*calc)(int))
{ Node *p = head;
while(p != NULL)
{ printf("%d ", calc(p->data));
p = p->next;
}
}
int doubleNum(int x)
{ return x * 2;}
int main()
{ Node n1, n2, n3;
n1.data = 7; n2.data = 11; n3.data = 15;
n1.next = &n2; n2.next = &n3; n3.next = NULL;
traverse(&n1, doubleNum);
return 0; }
【答案】14 22 30
7.下列程序的运行结果是__________________。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{ int val; struct Node *next;}Node;
int getLen(Node *head)
{ int cnt = 0;
Node *p = head;
while(p)
{ cnt++;p = p->next;}
return cnt;}
void insertAfterLastTwo(Node *head, int newVal)
{ int len = getLen(head),i;
Node *p = head;
Node *newNode = (Node*)malloc(sizeof(Node));
for(i = 0; i < len - 2; i++)
p = p->next;
newNode->val = newVal;
newNode->next = p->next;
p->next = newNode; }
void show(Node *head)
{ Node *p = head;
while(p)
{ printf("%d ", p->val);
p = p->next;}
}
int main()
{ Node a, b, c, d;
a.val = 3; b.val = 9; c.val = 16; d.val = 22;
a.next = &b; b.next = &c; c.next = &d; d.next = NULL;
insertAfterLastTwo(&a, 45);
show(&a);
return 0; }
【答案】3 9 16 45 22
8.下列程序的运行结果是__________________。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{ int num; struct Node *next;}Node;
void splitList(Node *head, Node **oddHead, Node **evenHead)
{ Node *oTail = NULL, *eTail = NULL;
Node *p = head;
*oddHead = NULL;
*evenHead = NULL;
while(p)
{ if(p->num % 2 == 1)
{ if(*oddHead == NULL) *oddHead = oTail = p;
else
{ oTail->next = p;
oTail = p; }
}
else
{ if(*evenHead == NULL) *evenHead = eTail = p;
else
{ eTail->next = p;
eTail = p; }
}
p = p->next;
}
if(oTail) oTail->next = NULL;
if(eTail) eTail->next = NULL;
}
void print(Node *h)
{ Node *p = h;
while(p)
{ printf("%d ", p->num);
p = p->next;
}
printf("
");
}
int main()
{ Node *h1, *h2;
Node n1={5},n2={8},n3={13},n4={20},n5={27};
n1.next=&n2; n2.next=&n3;
n3.next=&n4; n4.next=&n5; n5.next=NULL;
splitList(&n1, &h1, &h2);
print(h1);
print(h2);
return 0; }
【答案】
5 13 27
8 20
9.下列程序的运行结果是__________________。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{int data; struct Node *next;}Node;
Node* initList(int arr[], int n)
{ Node *h = (Node*)malloc(sizeof(Node)),*t;
int i;
if(n == 0) return NULL;
h->data = arr[0];
h->next = NULL;
t = h;
for(i = 1; i < n; i++)
{ Node *p = (Node*)malloc(sizeof(Node));
p->data = arr[i];
p->next = NULL;
t->next = p;
t = p;
}
return h;
}
int findSecondMax(Node *head)
{ int max1, max2;
Node *p = head;
max1 = max2 = p->data;
p = p->next;
while(p)
{ if(p->data > max1)
{ max2 = max1;
max1 = p->data; }
else if(p->data < max1 && p->data > max2)
{ max2 = p->data; }
p = p->next;
}
return max2;
}
int main()
{ int buf[] = {44, 18, 62, 35, 62, 29};
Node *head = initList(buf, 6);
printf("%d", findSecondMax(head));
return 0; }
【答案】44
10.下列程序的运行结果是__________________。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{ int val; struct Node *next;}Node;
Node* swapPair(Node *head)
{ Node dummy,*pre;
dummy.next = head;
pre = &dummy;
while(pre->next != NULL && pre->next->next != NULL)
{ Node *cur = pre->next;
Node *nxt = cur->next;
cur->next = nxt->next;
nxt->next = cur;
pre->next = nxt;
pre = cur;
}
return dummy.next;
}
void print(Node *h)
{ Node *p = h;
while(p)
{ printf("%d ", p->val);
p = p->next;
}
}
int main()
{ Node n1={2},n2={7},n3={11},n4={16},n5={23};
Node *newHead = swapPair(&n1);
n1.next=&n2; n2.next=&n3;
n3.next=&n4; n4.next=&n5; n5.next=NULL;
print(newHead);
return 0; }
【答案】2 7 11 16 23
三、程序填空。按照题目要求,将正确内容填入答题卡相应位置,使程序完整(每空2分,共计50分)
1.(2024年)下列程序的功能是:通过调用函数fun(),将单向链表逆置。若原链表从头到尾的结点数据域依次为1、2、3、4、5;逆置后,从头到尾的结点数据域依次为5、4、3、2、1。
#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef struct node {
int data;
_________①_________
} NODE;
NODE *fun(NODE *h){
NODE *p,*q,*r;
p = h;
if(p == NULL){
return NULL;
}
q = p->next;
_________②_________
while(q){
r = q->next;
_________③_________
p = q;
_________④_________
}
return p;
}
NODE *creatlist(int a[])
{
NODE *h,*p,*q; int i;
h=NULL;
for(i=0;i<N;i++)
{
_________⑤_________
q->data=a[i];
q->next = NULL;
if (h == NULL) h = p = q;
else {p->next = q; p = q;}
}
return h;
}
void outlist(NODE *h)
{
NODE *p;
p=h;
if(p==NULL) printf("The list is NULL!
");
else
{
printf("
Head ");
do
{
printf("->%d",p->data);p=p->next;
}while(p!=NULL);
printf("->End
");
}
}
int main()
{
NODE *head;
int a[N]={1,2,3,4,5};
head=creatlist(a);
printf("
The original list:
");
outlist(head);
head=fun(head);
printf("
The list after inverting :
");
outlist(head);
return 0;
}
【答案】① struct node *next;
② p->next=NULL;
③ q->next=p;
④ q=r;
⑤ q=(NODE*)malloc(sizeof(NODE));
2.(2023年)下列程序功能是:输入学生信息与三科成绩,并存放在链表中;输出总成绩最高的学生信息及成绩。例如,若学生信息为:“2023001 张一明 83 92 78;2023002 李小明 85 94 78;2023003 王大民 88 88 78”,则执行程序后,输出总分最高的学生李小明的相关信息。
#include <stdio.h>
#include <stdlib.h>
#define maxNum 3
typedef struct data{
char ID[10]; /*存放学号*/
char name[10]; /*存放姓名*/
int score[3]; /*存放三科成绩*/
struct data *next;
} ______①______
MYDATA *creatData() /*创建存放信息的链表*/
{
MYDATA *p,*q,*h;
int i;
int INPUT=0;
h=q=(MYDATA *)malloc(sizeof(MYDATA));
while(INPUT++<maxNum)
{
p=(MYDATA *)malloc(sizeof(MYDATA));
printf("input No(7 numbers):");
scanf("%s",p->ID);
printf("input name(8 characters):");
scanf("%s",p->name);
printf("input 3 scores:
");
for(i=0;i<3;i++)
scanf("%d",(p->score)+i);
p->next=p;
q->next=p;
______②______
}
q->next=NULL;
return h;
}
void outData(MYDATA *h)/*输出链表存放的信息*/
{
MYDATA *p;
int i;
p=h->next;
while(p!=NULL)
{
printf("No--%s
",p->ID);
printf("Name--%s
",p->name);
for(i=0;i<3;i++)
printf("%d ",(p->score)[i]);
p=p->next;
printf("
");
}
}
void findMax(MYDATA *h)/*输出总成绩最高的学生信息*/
{
MYDATA *p;
int i,pos=0,j=0;
int maxSum=0;
p=h->next;
while(p!=NULL)
{
int sum=0;
for(i=0;i<3;i++)
sum+=(p->score)[i];
if(sum>maxSum)
{
maxSum=sum;
______③______
}
j++;
p=p->next;
}
______④______
printf("the best score is:
");
while(pos--)
p=p->next;
printf("No--%s
",p->ID);
printf("Name--%s
",p->name);
for(i=0;i<3;i++)
printf("%d ",(p->score)[i]);
printf("maxSum=%d
",maxSum);
}
int main()
{
MYDATA *h;
h=creatData();
outData(h);
______⑤______
return 0;
}
【答案】①MYDATA; ②q=p; ③pos=j;
④p=h->next; ⑤findMax(h);
3.(2022年)下列程序的主函数中,通过函数调用把N个人的姓名与温度数据放入一个带头节点的链表中,h指向链表的头节点。函数Check()的功能是:找出体温大于37.5的结点(除头结点外),并显示相关信息。请补充程序,使其完整。
#include"stdio.h"
#define N 5
typedef struct ss
{char name[8];/*姓名*/
float t; /*温度*/
___________①___________;
}NODE;
void Check (NODE *h) /*把温度大于37.5的结点信息显示出来*/
{int sum=0;
NODE *p;
__________②_________;
while(p!=NULL)
{
if(_______③_______)
printf("%s,%3.1f
",p->name,p->t);
p=p->next;
}
}
NODE *creatlist(int n)/*创建链表*/
{
NODE *h,*p,*s;
int i;float temp;
h=p=(NODE*)malloc(sizeof(NODE));
for(i=1;i<n;i++)
{
s=(NODE*)malloc(sizeof(NODE));
printf("input name=");
scanf("%s",s->name);
printf("input t=");
scanf("%f",&temp);
s->t=temp;
s->next=p;
__________④_________
p=p->next;
}
__________⑤_________;
return h;
}
void OutList(NODE *h)/*输出链表*/
{
NODE *p;
p=h->next;
printf("
The LIST :
HEAD");
while(p){
printf("->%s,%3.1f",p->name,p->t);
p=p->next;
}
printf("
");
}
int main(void)
{
NODE *head; int sum;
system("CLS");
head=creatlist(N);
OutList(head);
Check(head);
return 0;
}
【答案】①struct ss *next ②p=h->next ③p->t>37.5
④p->next=s; ⑤p->next=NULL
4.(2026年)下列程序功能是:从一组由逗号隔开的姓名中,按姓氏统计人数并列出名单,规定姓名的第1个汉字为姓氏(在GBK字符编码中,1个汉字占2字节)。输出的第一行为总人数,其后的每行为姓氏、人数及名单。例如:
输入:张三,李四,王五,李小明,王明,刘振邦,刘二,李赵聘婷,李世时,刘杨同心
输出:共有10人
张姓有1人:张三
李姓有4人:李四、李小明、李赵聘婷、李世时
王姓有2人:王五、王明
刘姓有3人:刘振邦、刘二、刘杨同心
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FN_NUM_MAX 500
#define NAME_NUM_MAX 1000
#define NAME_LEN_MAX 101
struct NameNode {
char *pName;
struct NameNode *next;
};
typedef struct {
char firstName[3]; // 姓氏所需存储空间为3字节
int nameNum;
struct NameNode *pHead;
} FirstNameInfo;
// 对输入的姓名字符串进行分割
char **nameSplit(char *nameStr, int *pNameNum) {
int i, k, nameNum, len;
char **namePtrArr = (char **)malloc(NAME_NUM_MAX * sizeof(char *));
char tmpName[NAME_LEN_MAX];
char *pName;
len = strlen(nameStr);
k = 0;
nameNum = 0;
for (i = 0; nameStr[i] != '\0'; i++) {
if (nameStr[i] != ',') {
tmpName[k] = nameStr[i];
k++;
if (nameStr[i + 1] == ',' || nameStr[i + 1] == '\0') {
tmpName[k] = '\0';
__________________①________________
pName = (char *)malloc(NAME_LEN_MAX);
strcpy(pName, tmpName);
namePtrArr[nameNum] = pName;
nameNum++;
k = 0;
}
}
}
*pNameNum = nameNum;
return namePtrArr;
}
void copyFirstName(char *pFirstName, char *pName) {
pFirstName[0] = pName[0];
pFirstName[1] = pName[1];
____________②____________
pFirstName[2] = '\0';
}
int cmpFirstName(char *pFirstName, char *pName) {
if (pFirstName[0] == pName[0] && pFirstName[1] == pName[1])
return 1;
return 0;
}
void dealOneName(char *aName, FirstNameInfo *fniArr, int *pNum) {
int i, k;
struct NameNode *pNew, *pNow, *pPre;
k = *pNum;
for (i = 0; i < k; i++) {
if (cmpFirstName(fniArr[i].firstName, aName) == 1)
break;
}
if (k == 0 || i >= k) {
copyFirstName(fniArr[i].firstName, aName);
fniArr[i].nameNum = 1;
pNew = (struct NameNode *)malloc(sizeof(struct NameNode));
pNew->pName = aName;
pNew->next = NULL;
_____________③______________
fniArr[i].pHead = pNew;
k++;
} else {
fniArr[i].nameNum++;
pNew = (struct NameNode *)malloc(sizeof(struct NameNode));
pNew->pName = aName;
pNew->next = NULL;
pPre = pNow = fniArr[i].pHead;
while (pNow != NULL) {
pPre = pNow;
pNow = pNow->next;
}
pPre->next = pNew;
}
*pNum = k;
}
void showFirstNames(FirstNameInfo aXingNode) {
struct NameNode *p;
printf("%s 姓有 %d 人:", aXingNode.firstName, aXingNode.nameNum);
p = aXingNode.pHead;
while (p != NULL) {
printf("%s", p->pName);
____________④____________
if (p->next != NULL)
printf("、");
p = p->next;
}
printf("
");
}
void destroy(FirstNameInfo *fniArr, int nameNum) {
int i;
struct NameNode *pPre, *pNow;
for (i = 0; i < nameNum; i++) {
pNow = fniArr[i].pHead;
while (pNow != NULL) {
pPre = pNow;
___________⑤__________
pNow = pNow->next;
free(pPre->pName);
free(pPre);
}
}
}
int main() {
int i, n = 0, fnName = 0;
char nameStr[(NAME_LEN_MAX + 1) * NAME_NUM_MAX];
char **namesArr;
FirstNameInfo fniArr[FN_NUM_MAX];
gets(nameStr);
namesArr = nameSplit(nameStr, &n);
for (i = 0; i < n; i++) {
dealOneName(namesArr[i], fniArr, &fnName);
}
printf("共有 %d 人
", n);
for (i = 0; i < fnName; i++) {
showFirstNames(fniArr[i]);
}
destroy(fniArr, fnName);
free(namesArr);
return 0;
}
【答案】①pName = (char *)malloc(NAME_LEN_MAX);
②pFirstName[2] = '\0';
③fniArr[i].pHead = pNew;
④p->next != NULL;
⑤pNow = pNow->next;
5.给定一条无序单向链表,依次完成 4 项连续操作:
①环形检测:判断链表是否存在环,若有环则输出提示并终止;无环继续执行;
②奇偶节点拆分:按节点位置下标拆分链表:下标偶数节点一条链,下标奇数节点一条链;
③有序归并:将拆分后的两条子链表,合并为一条升序有序链表;
④原地重排:将最终有序链表,按「最大值→最小值→次大→次小」交替重排。
输出:
原始链表:18 5 29 7 42 11 33
环形检测:无环
下标偶数节点链表:18 29 42 33
下标奇数节点链表:5 7 11
归并后有序链表:5 7 11 18 29 42 33
大小交替重排最终链表:5 33 7 42 11 29 18
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{ int data;
struct Node *next;
} Node;
Node* CreateList(int arr[], int n)// 尾插法创建单链表
{ Node *head, *tail, *newNode;
int i;
if (n <= 0)
{ return NULL; }
head = NULL;
tail = NULL;
for (i = 0; i < n; i++)
{ newNode = (Node*)malloc(sizeof(Node));
newNode->data = arr[i];
newNode->next = NULL;
if (head == NULL)
{ head = tail = newNode; }
else
{ ____________①____________
tail = newNode;
}
}
return head;
}
void PrintList(Node *head)
{
Node *p;
p = head;
while (p != NULL)
{ printf("%d ", p->data);
p = p->next;
}
printf("
");
}
int HasCircle(Node *head)//检测链表是否有环
{
Node *slow, *fast;
slow = head;
fast = head;
while (fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = __________②___________
if (slow == fast)
{
return 1;
}
}
return 0;
}
//按【节点下标奇偶】拆分链表
void SplitByIndex(Node *head, Node **evenHead, Node **oddHead)
{
Node *eTail, *oTail, *p;
int idx;
*evenHead = NULL;
*oddHead = NULL;
eTail = NULL;
oTail = NULL;
p = head;
idx = 0;
while (p != NULL)
{ if (idx % 2 == 0)
{ if (*evenHead == NULL)
{ *evenHead = eTail = p; }
else
{ _________③_________
eTail = p; }
}
else
{ if (*oddHead == NULL)
{ *oddHead = oTail = p; }
else
{ oTail->next = p;
oTail = p; }
}
idx++;
__________④_________
}
if (eTail)
{ eTail->next = NULL; }
if (oTail)
{ oTail->next = NULL; }
}
Node* MergeTwoSorted(Node *l1, Node *l2)// 归并两个升序链表
{ Node dummy;
Node *cur;
dummy.next = NULL;
cur = &dummy;
while (l1 != NULL && l2 != NULL)
{ if (l1->data < l2->data)
{
cur->next = l1;
l1 = l1->next;
}
else
{
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
cur->next = l1 ? l1 : l2;
return dummy.next;
}
Node* ReverseList(Node *head)// 链表原地逆序
{
Node *pre, *cur, *nxt;
pre = NULL;
cur = head;
while (cur != NULL)
{ nxt = cur->next;
cur->next = pre;
pre = cur;
cur = nxt;
}
return pre;
}
Node* GetMid(Node *head)
{
Node *slow, *fast;
slow = head;
fast = head;
while (______________⑤______________)
{ slow = slow->next;
fast = fast->next->next;
}
return slow;
}
void ReArrange(Node *head)
{ Node *mid, *l2, *p1, *p2, *n1, *n2;
if (head == NULL || head->next == NULL)
{ return; }
mid = GetMid(head);
l2 = mid->next;
mid->next = NULL;
l2 = ReverseList(l2);
p1 = head;
p2 = l2;
while (p2 != NULL)
{ n1 = p1->next;
n2 = p2->next;
p1->next = p2;
p2->next = n1;
p1 = n1;
p2 = n2;
}
}
void FreeList(Node *head)
{ Node *p, *q;
p = head;
while (p != NULL)
{ q = p->next;
free(p);
p = q;
}
}
int main()
{ int data[] = {18, 5, 29, 7, 42, 11, 33};
int len;
Node *head, *even, *odd, *mergeHead;
len = sizeof(data) / sizeof(data[0]);
head = CreateList(data, len);
printf("原始链表:");
PrintList(head);
if (HasCircle(head))
{ printf("检测到链表存在环,程序终止!
");
FreeList(head);
return 0; }
printf("环形检测:无环
");
SplitByIndex(head, &even, &odd);
printf("下标偶数节点链表:");
PrintList(even);
printf("下标奇数节点链表:");
PrintList(odd);
mergeHead = MergeTwoSorted(even, odd);
printf("归并后有序链表:");
PrintList(mergeHead);
ReArrange(mergeHead);
printf("大小交替重排最终链表:");
PrintList(mergeHead);
FreeList(mergeHead);
return 0; }
【答案】①tail->next = newNode; ②fast->next->next;
③eTail->next = p; ④p = p->next;
⑤fast->next != NULL && fast->next->next != NULL
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
学科网(北京)股份有限公司
学科网(北京)股份有限公司
$