struct Node *readFile_creat(struct Node *head, int Class) { FILE *fp = fopen("class1_name_list.txt", "r"); switch (Class) { case 1: { fp = fopen("class1_name_list.txt", "r"); break; } case 2: { fp = fopen("class2_name_list.txt", "r"); break; } case 3: { fp = fopen("class3_name_list.txt", "r"); break; } case 4: { fp = fopen("class4_name_list.txt", "r"); break; } } if (!fp) { printf("打开异常"); exit(1); } char name_1[10]; char number_1[10]; struct Node *newNode; head->next = NULL; while (fscanf(fp, "%s %s", name_1, number_1) != EOF) { //end of file,即判断是否输入完毕 newNode = (struct Node *)malloc(sizeof(struct Node)); strcpy(newNode->name, name_1); strcpy(newNode->number, number_1); newNode->next = head->next; head->next = newNode; } printf("%d班录入名单完成.\n", Class); return head; }
时间: 2024-03-11 19:44:10 浏览: 24
这段代码是用来读取四个班级的学生名单文件,并创建一个链表来存储学生信息。函数的参数是一个指向链表头节点的指针和一个表示班级的整数。根据班级的不同,函数会打开不同的文件,读取文件中的学生姓名和学号,并将它们存储在一个新的节点中。然后将新节点插入到链表的头部。最后返回链表的头节点指针。这段代码的功能是为学校管理系统中班级管理模块提供支持。
相关问题
struct node *creat_node
你好!很高兴回答你的问题。关于创建一个名为 "node" 的结构体指针,你可以使用以下代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *create_node() {
struct node *new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL) {
printf("内存分配失败\n");
return NULL;
}
new_node->next = NULL;
return new_node;
}
int main() {
struct node *n = create_node();
if (n == NULL) {
return 1;
}
// 在这里可以对新节点进行操作
free(n);
return 0;
}
```
在这里,我们定义了一个结构体 `node`,其中包含一个整数类型的数据 `data` 和一个指向另一个 `node` 结构体的指针 `next`。然后,我们定义了一个函数 `create_node()` 来创建一个新的 `node` 节点,并返回指向该节点的指针。在 `main()` 函数中,我们调用 `create_node()` 函数来创建一个新节点,并在使用完成后释放内存。
希望这能帮到你!如果还有其他问题,请随时提问。
用c实现要求实现以下结构体或者函数(每个函数功能请严格按照要 求完成,不要修改,检查时会用统一main函数调用执行): struct Node; int list_tail_insert(Node *list_head, int var) // 单个数据插入,尾插法 Node *list_head_insert(Node *list_head, int var) //单个数据插入,头插法 Node *list_specific_insert(Node *list_head, int location, int var) //指定位置插入,可以插入头,尾, 或者头尾之间任意位置 void print_list(Node *list_head) //输出链表,循环 方式,空格隔开 void reverse_print_list(Node *list_head) //逆序输 出,递归方式,空格隔开 void change_specific_var(Node *list_head, int old_var, int new_var) //修改链表中的指定元素值 Node *del_specific_var(Node *list_head, int del_var) //删除链表中的指定元素值 Node *sort(Node *list_head) //从小到大排序 示例main函数中调用顺序 Node *list_head_insert Node *list_head_insert Node *list_head_insert int list_tail_insert int list_tail_insert Node *list_specific_insert void print_list Node *sort void print_listnvoid reverse_print_list void change_specific_var 2 3 void print_list Node *del_specific_var 3 void print_list
非常抱歉,我可以回答你的问题。以下是用 C 实现要求的结构体和函数:
struct Node {
int data;
struct Node *next;
};
int list_tail_insert(Node *list_head, int var) {
Node *new_node = (Node*)malloc(sizeof(Node));
new_node->data = var;
new_node->next = NULL;
if (list_head == NULL) {
list_head = new_node;
} else {
Node *p = list_head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_node;
}
return 0;
}
Node *list_head_insert(Node *list_head, int var) {
Node *new_node = (Node*)malloc(sizeof(Node));
new_node->data = var;
new_node->next = list_head;
return new_node;
}
Node *list_specific_insert(Node *list_head, int location, int var) {
Node *new_node = (Node*)malloc(sizeof(Node));
new_node->data = var;
if (location == 1) {
new_node->next = list_head;
return new_node;
}
Node *p = list_head;
int i = 1;
while (p != NULL && i < location - 1) {
p = p->next;
i++;
}
if (p == NULL) {
return list_head;
}
new_node->next = p->next;
p->next = new_node;
return list_head;
}
void print_list(Node *list_head) {
Node *p = list_head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
}
void reverse_print_list(Node *list_head) {
if (list_head == NULL) {
return;
}
reverse_print_list(list_head->next);
printf("%d ", list_head->data);
}
void change_specific_var(Node *list_head, int old_var, int new_var) {
Node *p = list_head;
while (p != NULL) {
if (p->data == old_var) {
p->data = new_var;
break;
}
p = p->next;
}
}
Node *del_specific_var(Node *list_head, int del_var) {
Node *p = list_head;
Node *prev = NULL;
while (p != NULL) {
if (p->data == del_var) {
if (prev == NULL) {
list_head = p->next;
} else {
prev->next = p->next;
}
free(p);
break;
}
prev = p;
p = p->next;
}
return list_head;
}
Node *sort(Node *list_head) {
if (list_head == NULL || list_head->next == NULL) {
return list_head;
}
Node *p = list_head;
Node *q = list_head->next;
Node *r = NULL;
while (q != NULL) {
if (q->data < p->data) {
p->next = q->next;
q->next = p;
if (p == list_head) {
list_head = q;
} else {
r->next = q;
}
r = q;
q = p->next;
} else {
r = p;
p = q;
q = q->next;
}
}
return list_head;
}
int main() {
Node *list_head = NULL;
list_head = list_head_insert(list_head, 1);
list_head = list_head_insert(list_head, 2);
list_head = list_head_insert(list_head, 3);
list_tail_insert(list_head, 4);
list_tail_insert(list_head, 5);
list_specific_insert(list_head, 3, 6);
print_list(list_head);
printf("\n");
list_head = sort(list_head);
print_list(list_head);
printf("\n");
reverse_print_list(list_head);
printf("\n");
change_specific_var(list_head, 2, 3);
print_list(list_head);
printf("\n");
list_head = del_specific_var(list_head, 3);
print_list(list_head);
return 0;
}