定义一个如下的结构体,动态申请内存,用表头添加或表尾添加学生,创建一个链表,要求完成链表显示、查找、删除功能。 struct Student { char name[20]; int num; int score; struct Student *next; };
时间: 2023-03-19 11:20:52 浏览: 81
此题可以使用链表实现,首先需要定义一个头结点,然后根据需求,用表头添加或表尾添加学生,创建一个链表,要求完成链表显示、查找、删除功能。可以设置三个函数,分别实现链表显示、查找、删除功能。链表显示函数可以遍历链表,显示学生的姓名、学号和成绩;查找函数可以查找指定学生的学号;删除函数可以删除指定学生的学号。
相关问题
创建一个关于链表结构体的c语言
在C语言中,链表是一种数据结构,它由节点组成,每个节点包含数据和指向下一个节点的指针。创建一个链表结构体的基本步骤如下:
1. 定义节点结构体(Node):
```c
typedef struct Node {
int data; // 数据域,存储节点值
struct Node* next; // 指针域,指向下一个节点
} Node;
```
这里,`data` 存储节点的数据,`next` 是一个指向 `Node` 类型的指针,用于链接各个节点。
2. 创建链表头结点(通常初始化为 `NULL`):
```c
Node* head = NULL;
```
3. 插入、删除和查找操作:
- 插入元素: 在链表末尾插入节点,或在指定位置插入
- 删除元素: 根据值或位置找到节点并删除
- 查找元素: 遍历链表直到找到目标值或到达末尾
4. 链表操作函数示例:
```c
// 插入元素
void insertNode(Node** head, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = *head;
*head = newNode;
}
// 删除元素
void deleteNode(Node** head, int value) {
Node* temp = *head;
Node* prev = NULL;
if (temp != NULL && temp->data == value) {
*head = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != value) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
// 查找元素
Node* searchNode(Node* head, int value) {
Node* current = head;
while (current != NULL) {
if (current->data == value) return current;
current = current->next;
}
return NULL;
}
```
用一个结构体储存学生信息,在用链表访问学生结构体
可以定义一个包含学生信息的结构体,例如:
```
struct Student {
int id;
char name[20];
int age;
float score;
};
```
然后可以定义一个链表节点结构体,包含指向下一个节点的指针和一个学生信息结构体的指针,例如:
```
struct ListNode {
struct Student* data;
struct ListNode* next;
};
```
接下来可以定义一个链表结构体,包含指向链表头节点和尾节点的指针,例如:
```
struct LinkedList {
struct ListNode* head;
struct ListNode* tail;
};
```
最后可以实现链表的基本操作,例如创建链表、添加节点、删除节点、遍历节点等。具体实现可以参考以下代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int id;
char name[20];
int age;
float score;
};
struct ListNode {
struct Student* data;
struct ListNode* next;
};
struct LinkedList {
struct ListNode* head;
struct ListNode* tail;
};
struct Student* createStudent(int id, char* name, int age, float score) {
struct Student* student = (struct Student*)malloc(sizeof(struct Student));
student->id = id;
strcpy(student->name, name);
student->age = age;
student->score = score;
return student;
}
struct ListNode* createListNode(struct Student* student) {
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
node->data = student;
node->next = NULL;
return node;
}
struct LinkedList* createLinkedList() {
struct LinkedList* list = (struct LinkedList*)malloc(sizeof(struct LinkedList));
list->head = NULL;
list->tail = NULL;
return list;
}
void addListNode(struct LinkedList* list, struct ListNode* node) {
if (list->head == NULL) {
list->head = node;
list->tail = node;
} else {
list->tail->next = node;
list->tail = node;
}
}
void removeListNode(struct LinkedList* list, struct ListNode* node) {
if (list->head == node) {
list->head = node->next;
} else {
struct ListNode* prev = list->head;
while (prev->next != node) {
prev = prev->next;
}
prev->next = node->next;
}
if (list->tail == node) {
list->tail = NULL;
}
free(node->data);
free(node);
}
void traverseLinkedList(struct LinkedList* list) {
struct ListNode* node = list->head;
while (node != NULL) {
printf("id: %d, name: %s, age: %d, score: %.2f\n", node->data->id, node->data->name, node->data->age, node->data->score);
node = node->next;
}
}
int main() {
struct LinkedList* list = createLinkedList();
struct Student* student1 = createStudent(1, "Tom", 18, 90.5);
struct Student* student2 = createStudent(2, "Jerry", 19, 85.0);
struct Student* student3 = createStudent(3, "Alice", 20, 95.0);
struct ListNode* node1 = createListNode(student1);
struct ListNode* node2 = createListNode(student2);
struct ListNode* node3 = createListNode(student3);
addListNode(list, node1);
addListNode(list, node2);
addListNode(list, node3);
traverseLinkedList(list);
removeListNode(list, node2);
traverseLinkedList(list);
return 0;
}
```