输入学号查找学生pta
时间: 2024-02-28 17:49:22 浏览: 158
根据学生学号查找学生信息的索引器
4星 · 用户满意度95%
以下是一个根据学号查找学生的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
typedef struct student {
int no;
char name[MAX_NAME_LEN];
struct student *next;
} Student, *List;
List CreateList() {
List head = NULL, tail = NULL;
int no;
char name[MAX_NAME_LEN];
while (1) {
scanf("%d", &no);
if (no == -1) {
break;
}
scanf("%s", name);
Student *node = (Student *)malloc(sizeof(Student));
node->no = no;
strcpy(node->name, name);
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}
return head;
}
Student *Find(List head, int no) {
Student *p = head;
while (p != NULL) {
if (p->no == no) {
return p;
}
p = p->next;
}
return NULL;
}
int main() {
List list = CreateList();
int no;
scanf("%d", &no);
Student *p = Find(list, no);
if (p != NULL) {
printf("%d %s\n", p->no, p->name);
} else {
printf("Not found\n");
}
return 0;
}
```
阅读全文