PTA:C课程设计(7)
山东大学(威海)2022级大一下C习题集(7)
函数题
7-6-1 递增的整数序列链表的插入
接口:
List Insert( List L, ElementType X );
要求实现一个函数,在递增的整数序列链表(带头结点)中插入一个新整数,并保持该序列的有序性。
其中List
结构定义如下:
typedef struct Node *PtrToNode;
struct Node {ElementType Data; /* 存储结点数据 */PtrToNode Next; /* 指向下一个结点的指针 */
};
typedef PtrToNode List; /* 定义单链表类型 */
L
是给定的带头结点的单链表,其结点存储的数据是递增有序的;函数Insert
要将X插入L
,并保持该序列的有序性,返回插入后的链表头指针。
List Insert( List L, ElementType X )
{List cur = L;List tmp = (List)malloc(sizeof(struct Node));tmp->Data = X;while(cur->Next&& cur->Next->Data < X){cur = cur->Next;}tmp->Next = cur->Next;cur->Next = tmp;return L;
}
7-6-2 查找学生链表
学生信息链表结点定义如下:
typedef struct List{int sno; char sname[10];List *next;
};
需要创建的函数包括:
创建学生信息链表函数:CreateList;
查找学生信息链表函数:Find。
接口:
List * CreateList(); //键盘输入若干学生学号和姓名,学号与姓名以空格符间隔,当输入的学号为-1时,输入结束,创建学生信息链表函数,返回学生链表的头指针。
List * Find(List *head, int no) //在学生信息链表(头指针为head)中查询学号为no的学生,返回该学生结点的指针。
实现:
//键盘输入若干学生学号和姓名,学号与姓名以空格符间隔,当输入的学号为-1时,输入结束,
//创建学生信息链表函数,返回学生链表的头指针。
List* BuyList(int sno)
{List* newnode = (List*)malloc(sizeof(struct List));newnode->sno = sno;newnode->next = NULL;return newnode;
}List * CreateList()
{List *head, *tail;int id;char sname[10] = {0};tail = head = (List*)malloc(sizeof(List));head->next = NULL;scanf("%d", &id);while(id != -1){List* t = BuyList(id);scanf("%s",t->sname);scanf("%d",&id);tail->next = t;tail = t;}scanf("%s",sname);return head;
}//在学生信息链表(头指针为head)中查询学号为no的学生,返回该学生结点的指针。
List * Find(List *head, int no)
{List* cur = head->next;while(cur){if(cur->sno == no)return cur;cur = cur->next;}return cur;
}
7-6-3 统计专业人数
实现一个函数,统计学生学号链表中专业为计算机的学生人数。链表结点定义如下:
struct ListNode {char code[8];struct ListNode *next;
};
这里学生的学号共7位数字,其中第2、3位是专业编号。计算机专业的编号为02
接口:
int countcs( struct ListNode *head );
其中head
是用户传入的学生学号链表的头指针;函数countcs
统计并返回head
链表中专业为计算机的学生人数。
实现:
int countcs( struct ListNode *head )
{if(head == NULL)return 0;struct ListNode *cur = head;int cnt = 0;while(cur){if(cur->code[1] == '0' && cur->code[2] == '2')cnt++;cur = cur->next;}return cnt;
}
7-6-4 建立学生信息链表
实现一个将输入的学生成绩组织成单向链表的简单函数。
接口:
void input();
该函数利用scanf
从输入中获取学生的信息,并将其组织成单向链表。链表节点结构定义如下:
struct stud_node {int num; /*学号*/char name[20]; /*姓名*/int score; /*成绩*/struct stud_node *next; /*指向下个结点的指针*/
};
单向链表的头尾指针保存在全局变量head
和tail
中。
输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。
实现:
struct stud_node* Buynode()
{struct stud_node*node = (struct stud_node*)malloc(sizeof(struct stud_node));node->next = NULL;return node;
}void input()
{struct stud_node* cur = Buynode();scanf("%d", &cur->num);while (cur->num != 0){if (head == NULL){scanf("%s", cur->name);scanf("%d", &cur->score);head = tail = cur;}else{scanf("%s", cur->name);scanf("%d", &cur->score);tail->next = cur;tail = tail->next;}cur = Buynode();scanf("%d", &cur->num);}
}
编程题
7-7-1 查找书籍
#include <stdio.h>
#include <stdlib.h>typedef struct book {double price;char name[40];struct book* next;
}book;book* Buynode()
{book* node = (book*)malloc(sizeof(book));node->next = NULL;return node;
}int main()
{//输入第一行给出正整数n(<10)int n = 0;scanf("%d", &n);getchar();book* head, * cur;head = cur = Buynode();gets(cur->name);scanf("%lf", &cur->price);while (--n)//n本书{getchar();book* node = Buynode();gets(node->name);scanf("%lf", &node->price);cur->next = node;cur = cur->next;}cur = head;double max,min;max = min = cur->price;
//选出价格最高、最低while (cur){if (cur->price > max)max = cur->price;if (cur->price < min)min = cur->price;cur = cur->next;}cur = head;while (cur->price != max){cur = cur->next;}printf("%.2lf, %s\\n", cur->price, cur->name);cur = head;while (cur->price != min){cur = cur->next;}printf("%.2lf, %s\\n", cur->price, cur->name);return 0;
}
7-7-2 找出总分最高的学生
#include <stdio.h>
#include <stdlib.h>typedef struct stu {char num[9];char name[15];int s1;int s2;int s3;struct stu* next;
}stu;stu* Buynode()
{stu* node = (stu*)malloc(sizeof(stu));node->next = NULL;return node;
}int main()
{int n = 0;scanf("%d", &n);getchar();stu* cur,* head;cur = head = Buynode();scanf("%s",head->num);scanf("%s",head->name);scanf("%d%d%d", &head->s1, &head->s2, &head->s3);while (--n){getchar();stu* node = Buynode();scanf("%s",node->num);scanf("%s",node->name);scanf("%d%d%d", &node->s1, &node->s2, &node->s3);cur->next = node;cur = cur->next;}cur = head;int max = cur->s1 + cur->s2 + cur->s3;while (cur){if ((cur->s1 + cur->s2 + cur->s3) > max)max = cur->s1 + cur->s2 + cur->s3;cur = cur->next;}cur = head;while ((cur->s1 + cur->s2 + cur->s3) != max){cur = cur->next;}printf("%s %s %d", cur->name, cur->num, cur->s1 + cur->s2 + cur->s3);return 0;
}