使用鏈表實現學生信息的存儲和管理,可以按照以下步驟進行操作:
typedef struct {
int id;
char name[20];
int age;
} Student;
typedef struct Node {
Student student;
struct Node *next;
} Node;
Node *head = NULL;
Node *tail = NULL;
void addStudent(Student student) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->student = student;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
void printStudents() {
Node *node = head;
while (node != NULL) {
printf("學號:%d, 姓名:%s, 年齡:%d\n", node->student.id, node->student.name, node->student.age);
node = node->next;
}
}
使用以上步驟,可以使用鏈表來存儲和管理學生信息。