2 2.(简答题 请编程实现 1、定义一个数nfind(low,highmid)。实现在一个有序的列表里面查找用户输入的数x,如果x存在那么数的回H值为x的素引 如果不存在,则的数的返回造为不存在。 2,主程序中生成列表st,有20个元素,且元素均为1-1000范围内的随机整数, 3、x从键盘输入,并进行测试
时间: 2024-03-12 16:45:03 浏览: 105
以下是 Python3 代码实现:
```python
import random
# 生成有序列表
st = sorted([random.randint(1, 1000) for i in range(20)])
# 定义查找函数
def nfind(low, high, mid):
if low > high:
return -1
elif st[mid] == x:
return mid
elif st[mid] < x:
return nfind(mid+1, high, (mid+1+high)//2)
else:
return nfind(low, mid-1, (low+mid-1)//2)
# 主程序
x = int(input("请输入要查找的数:"))
index = nfind(0, len(st)-1, len(st)//2)
if index == -1:
print("不存在")
else:
print("存在,索引为:", index)
```
在此代码中,nfind函数实现了二分查找算法,用于在有序列表中查找指定的数x。主程序中先生成了一个有序列表st,然后从键盘读入x,并调用nfind函数进行查找,最后输出查找结果。
相关问题
用C 语言构造学生信息链表,学生信息应包括姓名和年龄(链表初始化),根据自己的需要建立链表添加学生信息,学生信息不得少于3条(建立链表:头插法或者尾插法皆可) 3.打印出所有学生的信息(遍历),并统计学生人数(求链表长度) 4.在第二个位置插入一个学生信息,姓名为Tom,年龄为10(链表插入),并打印出插入后的学生信息表(遍历) 5.查找年龄为10的学生,打印其姓名(按值查找) 6.删除第二个学生的信息(删除),打印删除后的学生信息表(遍历)
以下是用C语言实现的学生信息链表,包括链表初始化、添加学生信息、遍历链表、插入学生信息、查找学生信息和删除学生信息。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
typedef struct student {
char name[20];
int age;
struct student *next;
} Student;
// 初始化链表
Student *init_list() {
Student *head = (Student*)malloc(sizeof(Student));
head->next = NULL;
return head;
}
// 添加学生信息
void add_student(Student *head, char *name, int age) {
Student *p = (Student*)malloc(sizeof(Student));
strcpy(p->name, name);
p->age = age;
p->next = head->next;
head->next = p;
}
// 遍历链表并统计学生人数
int traverse_list(Student *head) {
int count = 0;
Student *p = head->next;
while (p != NULL) {
printf("name: %s, age: %d\n", p->name, p->age);
p = p->next;
count++;
}
return count;
}
// 在第二个位置插入学生信息
void insert_student(Student *head, char *name, int age) {
Student *p = (Student*)malloc(sizeof(Student));
strcpy(p->name, name);
p->age = age;
p->next = head->next->next;
head->next->next = p;
}
// 查找年龄为指定值的学生
char* find_student(Student *head, int age) {
Student *p = head->next->next;
while (p != NULL) {
if (p->age == age) {
return p->name;
}
p = p->next;
}
return NULL;
}
// 删除第二个学生的信息
void delete_student(Student *head) {
Student *p = head->next->next;
head->next->next = p->next;
free(p);
}
int main() {
Student *head = init_list();
add_student(head, "Alice", 18);
add_student(head, "Bob", 20);
add_student(head, "Charlie", 22);
printf("All students:\n");
int count = traverse_list(head);
printf("Total count: %d\n", count);
printf("\nInsert a student:\n");
insert_student(head, "Tom", 10);
traverse_list(head);
printf("\nFind a student:\n");
char *name = find_student(head, 10);
if (name != NULL) {
printf("The name of the student is %s.\n", name);
} else {
printf("No student with this age.\n");
}
printf("\nDelete a student:\n");
delete_student(head);
traverse_list(head);
return 0;
}
```
写一个单向链表存放员工name,age,gender,要求增删改查源码
以下是单向链表存放员工name,age,gender的增删改查源码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义员工结构体
typedef struct Employee {
char name[20];
int age;
char gender[10];
struct Employee *next;
} Employee;
// 创建员工链表
Employee *createEmployeeList() {
Employee *head = (Employee *)malloc(sizeof(Employee));
head->next = NULL;
return head;
}
// 添加员工
void addEmployee(Employee *head, char *name, int age, char *gender) {
Employee *newEmployee = (Employee *)malloc(sizeof(Employee));
strcpy(newEmployee->name, name);
newEmployee->age = age;
strcpy(newEmployee->gender, gender);
newEmployee->next = NULL;
Employee *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = newEmployee;
}
// 删除员工
void deleteEmployee(Employee *head, char *name) {
Employee *p = head->next;
Employee *pre = head;
while (p != NULL) {
if (strcmp(p->name, name) == ) {
pre->next = p->next;
free(p);
return;
}
pre = p;
p = p->next;
}
}
// 修改员工信息
void modifyEmployee(Employee *head, char *name, int age, char *gender) {
Employee *p = head->next;
while (p != NULL) {
if (strcmp(p->name, name) == ) {
p->age = age;
strcpy(p->gender, gender);
return;
}
p = p->next;
}
}
// 查找员工
void findEmployee(Employee *head, char *name) {
Employee *p = head->next;
while (p != NULL) {
if (strcmp(p->name, name) == ) {
printf("Name: %s, Age: %d, Gender: %s\n", p->name, p->age, p->gender);
return;
}
p = p->next;
}
printf("Employee not found.\n");
}
// 打印员工链表
void printEmployeeList(Employee *head) {
Employee *p = head->next;
while (p != NULL) {
printf("Name: %s, Age: %d, Gender: %s\n", p->name, p->age, p->gender);
p = p->next;
}
}
int main() {
Employee *head = createEmployeeList();
addEmployee(head, "Tom", 25, "Male");
addEmployee(head, "Lucy", 30, "Female");
addEmployee(head, "John", 28, "Male");
printf("Employee List:\n");
printEmployeeList(head);
printf("\nDelete employee Lucy:\n");
deleteEmployee(head, "Lucy");
printEmployeeList(head);
printf("\nModify employee John's information:\n");
modifyEmployee(head, "John", 29, "Male");
printEmployeeList(head);
printf("\nFind employee Tom:\n");
findEmployee(head, "Tom");
return ;
}
阅读全文