祈网网站建设,商城app怎么推广,深圳展示型网站建设,邳州市建设局官方网站山东大学#xff08;威海#xff09;2022级大一下C习题集#xff08;7#xff09; 函数题7-6-1 递增的整数序列链表的插入7-6-2 查找学生链表7-6-3 统计专业人数7-6-4 建立学生信息链表 编程题7-7-1 查找书籍7-7-2 找出总分最高的学生 函数题
7-6-1 递增的整数序列链表的插… 山东大学威海2022级大一下C习题集7 函数题7-6-1 递增的整数序列链表的插入7-6-2 查找学生链表7-6-3 统计专业人数7-6-4 建立学生信息链表 编程题7-7-1 查找书籍7-7-2 找出总分最高的学生 函数题
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.htypedef 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()
{//输入第一行给出正整数n10int 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.htypedef 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;
}