Student* find(int i_no); 是什么意思
时间: 2023-09-03 19:04:59 浏览: 75
### 回答1:
这是一个函数声明,表示定义了一个名为 `find` 的函数,函数返回类型为 `Student*`,表示该函数返回一个指向 `Student` 类型对象的指针。函数的参数是一个 `int` 类型的变量 `i_no`,用于指定要查找的学生对象的编号。该函数可能会在某个数据结构中查找学生编号为 `i_no` 的对象,并返回指向该对象的指针。
### 回答2:
"Student* find(int i_no);" 是一个函数声明,它的返回类型是 "Student*",意味着该函数将返回一个指向 Student 类型对象的指针。
这个函数的名称是 "find",它接受一个名为 "i_no" 的整数参数。"i_no" 可能表示学生的学号或其他标识符。函数的目的是根据传入的学号查找相应的学生,并返回指向该学生对象的指针。
使用该函数时,你可以传入一个学号作为参数,函数会返回一个指向对应学生对象的指针。可以通过此指针进一步访问学生对象的属性和方法,或进行其他操作。
需要注意的是,由于函数返回的是指针,可能需要在使用完毕后手动释放内存,以避免内存泄漏问题。
相关问题
用c语言针对同班同学信息设计一个通讯录,学生信息有姓名,学号,电话号码等。以学生姓名为关键字设计哈希表,并完成相应的建表和查表程序,并分析平均查找长度(姓名以汉语拼音形式,待填入哈希表的人名不低于30个,自行设计哈希函数,用线性探测再散列法或链地址法处理冲突;在查找的过程中给出比较的次数。完成按姓名查询的操作。)
好的,这是一个非常实用的程序。以下是一个C语言的哈希表通讯录的示例代码,你可以参考它来完成你的程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 50
#define MAX_PHONE_LEN 20
#define HASH_TABLE_SIZE 50
typedef struct Student {
char name[MAX_NAME_LEN];
char id[MAX_PHONE_LEN];
char phone[MAX_PHONE_LEN];
struct Student *next;
} Student;
int hash(char *name) {
int sum = 0;
for (int i = 0; i < strlen(name); i++) {
sum += name[i];
}
return sum % HASH_TABLE_SIZE;
}
Student *create_student(char *name, char *id, char *phone) {
Student *s = (Student*)malloc(sizeof(Student));
strcpy(s->name, name);
strcpy(s->id, id);
strcpy(s->phone, phone);
s->next = NULL;
return s;
}
void insert_student(Student **hash_table, char *name, char *id, char *phone) {
int index = hash(name);
Student *head = hash_table[index];
Student *s = create_student(name, id, phone);
if (head == NULL) {
hash_table[index] = s;
} else {
while (head->next != NULL) {
head = head->next;
}
head->next = s;
}
}
Student *find_student(Student **hash_table, char *name, int *compare_count) {
int index = hash(name);
Student *head = hash_table[index];
while (head != NULL) {
(*compare_count)++;
if (strcmp(head->name, name) == 0) {
return head;
}
head = head->next;
}
return NULL;
}
int main() {
Student **hash_table = (Student**)malloc(sizeof(Student*) * HASH_TABLE_SIZE);
memset(hash_table, 0, sizeof(Student*) * HASH_TABLE_SIZE);
insert_student(hash_table, "Zhang San", "001", "123456");
insert_student(hash_table, "Li Si", "002", "234567");
insert_student(hash_table, "Wang Wu", "003", "345678");
insert_student(hash_table, "Zhao Liu", "004", "456789");
char input_name[MAX_NAME_LEN];
printf("Please input a name to search: ");
scanf("%s", input_name);
int compare_count = 0;
Student *s = find_student(hash_table, input_name, &compare_count);
if (s != NULL) {
printf("ID: %s\n", s->id);
printf("Phone: %s\n", s->phone);
printf("Compare count: %d\n", compare_count);
} else {
printf("No such student.\n");
}
return 0;
}
```
在这个示例代码中,我们使用了哈希表来存储学生信息,并且针对学生姓名设计了哈希函数。在插入学生信息时,我们会根据哈希函数计算出学生姓名对应的哈希表索引,然后将学生信息插入到对应的链表中。在查找学生信息时,我们同样会根据哈希函数计算出对应的哈希表索引,并且遍历对应的链表来查找学生信息。在查找的过程中,我们会记录比较次数,以便分析平均查找长度。
当然,这个示例代码中还有很多可以改进的地方,比如可以使用链地址法来处理冲突,可以动态调整哈希表大小等等。但是这个示例代码已经足够帮助你了解如何使用哈希表来实现通讯录功能。如果你有什么问题,可以随时向我提问。
Write a program to simulate a process of translation from a logical address to physical address. Assumptions 1. Assume the file la.txt includes the sequence of generated addresses from CPU. 2. Use a part of memory as backing store that store data for a process. 3. The backing store size is 128 bytes 4. The size of process p is 128 bytes. 5. The contents of p is included in a file pdata.bin which is a binary file. 6. Use a part of memory as RAM. The size of physical memory is 256 bytes, from 0 to 255. All the physical memory is available, allocating starting from beginning in sequence. That is, allocate frame 0 first, then frame 1, then frame 2…. 7. The size of a frame is 32 bytes, i.e., 5 bits for the offset in a frame, total number of frames is 8. 8. At beginning, no page table is available for process p. Requirements Write a program to 1. Setup a simulating backing store in memory. Read the data from pdata.bin to this backing store. 2. Initialize a page table for process p, set the frame number to be -1 for each page, indicating that the page is not loaded into memory yet. 3. Read logical addresses one by one from la.txt. 4. For each logical address, a) if its page has been loaded into physical memory, simply find the frame number in the page table, then generate physical address, find and print out the physical address and data inside this address. b) if the page is used for the first time, i.e., in page table, its frame number is -1,then the page that contains this address should be loaded into a free frame in physical memory (RAM). Then update the page table by adding the frame number to the right index in the page table. Then repeat 4a). Refer to Figure 1 for the relationships and how physical memory, backing store, and CPU are simulated. Figure 1 How physical memory, backing store and CPU are simulated in this program assignment Hints: a) use a memory block pointed by a pointer or use an array as a simulation of backing store b) use functions fread or mmap for the binary file read. Search through the Internet for the usage of these functions. c) Use an array to simulate the memory. d) Use bit operators &, |, <<, and >> to get the bits in a logic address or form a physical address e) Use char for the type of data in the process, use unsigned char (8 bits) for the type of address. Coding & Submission 1. Using pure C to finish this program. 2. Put all the codes in one .c file named PA3_#####.c, replace “#####” as the last 5 digits of your student ID. 3. Put pdata.txt and la.txt in the same folder as PA3_#####.c, which the need .txt file can be open directly by filename instead of absolute path. 4. Submit only the .c file mentioned above.使用C语言完成
```
#include <stdio.h>
#include <stdlib.h>
#define BACKING_STORE_SIZE 128
#define PAGE_SIZE 32
#define NUM_PAGES 4
#define NUM_FRAMES 8
#define FRAME_SIZE 32
#define PHYSICAL_MEMORY_SIZE NUM_FRAMES * FRAME_SIZE
int page_table[NUM_PAGES];
unsigned char backing_store[BACKING_STORE_SIZE];
unsigned char physical_memory[PHYSICAL_MEMORY_SIZE];
void load_page(int page_num, int frame_num) {
int byte_offset = page_num * PAGE_SIZE;
int frame_offset = frame_num * FRAME_SIZE;
for (int i = 0; i < PAGE_SIZE; i++) {
physical_memory[frame_offset + i] = backing_store[byte_offset + i];
}
}
int main() {
FILE *la_file = fopen("la.txt", "r");
FILE *pdata_file = fopen("pdata.bin", "rb");
if (la_file == NULL || pdata_file == NULL) {
printf("Error opening file\n");
return 1;
}
// populate backing store with process data
fread(backing_store, sizeof(unsigned char), BACKING_STORE_SIZE, pdata_file);
// initialize page table
for (int i = 0; i < NUM_PAGES; i++) {
page_table[i] = -1;
}
// read logical addresses from file
int logical_address;
while (fscanf(la_file, "%d", &logical_address) != EOF) {
// calculate page number and offset
int page_num = logical_address / PAGE_SIZE;
int page_offset = logical_address % PAGE_SIZE;
// check if page is in memory
if (page_table[page_num] != -1) {
int frame_num = page_table[page_num];
int physical_address = (frame_num * FRAME_SIZE) + page_offset;
printf("Logical address: %d, Physical address: %d, Data: %c\n",
logical_address, physical_address, physical_memory[physical_address]);
} else {
// find a free frame in physical memory
int free_frame = -1;
for (int i = 0; i < NUM_FRAMES; i++) {
if (page_table[i] == -1) {
free_frame = i;
break;
}
}
// if no free frame is found, use a random one (for simplicity)
if (free_frame == -1) {
free_frame = rand() % NUM_FRAMES;
page_table[free_frame] = -1;
}
// load page into free frame
load_page(page_num, free_frame);
page_table[page_num] = free_frame;
// print physical address
int physical_address = (free_frame * FRAME_SIZE) + page_offset;
printf("Logical address: %d, Physical address: %d, Data: %c\n",
logical_address, physical_address, physical_memory[physical_address]);
}
}
fclose(la_file);
fclose(pdata_file);
return 0;
}
```