定义一个如下的结构体,动态申请内存,用表头添加或表尾添加学生,创建一个链表,要求完成链表显示、查找、删除功能。 struct Student { char name[20]; int num; int score; struct Student *next; };
时间: 2023-03-19 15:20:52 浏览: 79
此题可以使用链表实现,首先需要定义一个头结点,然后根据需求,用表头添加或表尾添加学生,创建一个链表,要求完成链表显示、查找、删除功能。可以设置三个函数,分别实现链表显示、查找、删除功能。链表显示函数可以遍历链表,显示学生的姓名、学号和成绩;查找函数可以查找指定学生的学号;删除函数可以删除指定学生的学号。
相关问题
用一个结构体储存学生信息,在用链表访问学生结构体
可以定义一个包含学生信息的结构体,例如:
```
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;
}
```
定义一个链表以及维持链表状况的结构体C语言代码示例
链表是一种常用的数据结构,它由一个个节点组成,每个节点包含数据和指向下一个节点的指针。以下是一个定义链表以及维护链表状态的结构体的C语言代码示例:
```c
//定义链表节点结构体
struct ListNode {
int val; //节点数据
struct ListNode *next; //指向下一个节点的指针
};
//定义链表结构体
struct LinkedList {
struct ListNode *head; //指向链表头节点的指针
struct ListNode *tail; //指向链表尾节点的指针
int size; //链表大小
};
//初始化链表
void initList(struct LinkedList *list) {
list->head = NULL;
list->tail = NULL;
list->size = 0;
}
//在链表尾部插入一个节点
void addNode(struct LinkedList *list, int val) {
struct ListNode *newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = val;
newNode->next = NULL;
if(list->tail == NULL) {
list->head = newNode;
list->tail = newNode;
} else {
list->tail->next = newNode;
list->tail = newNode;
}
list->size++;
}
//遍历链表
void traverseList(struct LinkedList *list) {
struct ListNode *node = list->head;
while(node != NULL) {
printf("%d ", node->val);
node = node->next;
}
}
//删除链表中某个节点
void deleteNode(struct LinkedList *list, int val) {
struct ListNode *node = list->head;
struct ListNode *prev = NULL;
while(node != NULL) {
if(node->val == val) {
if(prev == NULL) {
list->head = node->next;
} else {
prev->next = node->next;
}
if(list->tail == node) {
list->tail = prev;
}
free(node);
node = NULL;
list->size--;
break;
} else {
prev = node;
node = node->next;
}
}
}
```
以上是链表及其维护状态的C语言代码示例,包括链表节点结构体、链表结构体、初始化链表、在链表尾部插入一个节点、遍历链表和删除链表中某个节点。