在C語言中,鏈表是一種非常常用的數據結構,它能夠動態地管理數據,適用于需要頻繁插入和刪除操作的場景。本文將詳細介紹如何使用鏈表來實現一個簡單的學生籍貫管理系統。通過這個系統,我們可以實現學生信息的添加、刪除、修改、查詢和顯示等功能。
鏈表是一種線性數據結構,它由一系列節點組成,每個節點包含數據和指向下一個節點的指針。鏈表的主要優點是插入和刪除操作非常高效,時間復雜度為O(1)。鏈表可以分為單向鏈表、雙向鏈表和循環鏈表等類型。本文將使用單向鏈表來實現學生籍貫管理系統。
學生籍貫管理系統的主要功能包括: 1. 添加學生信息:包括學生的學號、姓名、籍貫等信息。 2. 刪除學生信息:根據學號刪除學生的信息。 3. 修改學生信息:根據學號修改學生的信息。 4. 查詢學生信息:根據學號查詢學生的信息。 5. 顯示所有學生信息:顯示系統中所有學生的信息。
首先,我們需要定義一個結構體來表示鏈表中的節點。每個節點包含學生的學號、姓名、籍貫以及指向下一個節點的指針。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 50
#define MAX_HOMETOWN_LEN 100
typedef struct Student {
int id; // 學號
char name[MAX_NAME_LEN]; // 姓名
char hometown[MAX_HOMETOWN_LEN]; // 籍貫
struct Student* next; // 指向下一個節點的指針
} Student;
在鏈表中,通常使用一個頭節點來表示鏈表的起始位置。頭節點不存儲實際的數據,它的next指針指向鏈表的第一個節點。我們可以通過以下代碼來創建和初始化鏈表。
Student* createList() {
Student* head = (Student*)malloc(sizeof(Student));
if (head == NULL) {
printf("內存分配失??!\n");
exit(1);
}
head->next = NULL;
return head;
}
添加學生信息時,我們需要創建一個新的節點,并將其插入到鏈表的末尾。以下是添加學生信息的代碼實現。
void addStudent(Student* head, int id, char* name, char* hometown) {
Student* newStudent = (Student*)malloc(sizeof(Student));
if (newStudent == NULL) {
printf("內存分配失??!\n");
exit(1);
}
newStudent->id = id;
strncpy(newStudent->name, name, MAX_NAME_LEN);
strncpy(newStudent->hometown, hometown, MAX_HOMETOWN_LEN);
newStudent->next = NULL;
Student* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newStudent;
printf("學生信息添加成功!\n");
}
刪除學生信息時,我們需要根據學號找到對應的節點,并將其從鏈表中移除。以下是刪除學生信息的代碼實現。
void deleteStudent(Student* head, int id) {
Student* current = head;
while (current->next != NULL && current->next->id != id) {
current = current->next;
}
if (current->next == NULL) {
printf("未找到學號為%d的學生!\n", id);
return;
}
Student* temp = current->next;
current->next = temp->next;
free(temp);
printf("學生信息刪除成功!\n");
}
修改學生信息時,我們需要根據學號找到對應的節點,并更新其信息。以下是修改學生信息的代碼實現。
void modifyStudent(Student* head, int id, char* name, char* hometown) {
Student* current = head->next;
while (current != NULL && current->id != id) {
current = current->next;
}
if (current == NULL) {
printf("未找到學號為%d的學生!\n", id);
return;
}
strncpy(current->name, name, MAX_NAME_LEN);
strncpy(current->hometown, hometown, MAX_HOMETOWN_LEN);
printf("學生信息修改成功!\n");
}
查詢學生信息時,我們需要根據學號找到對應的節點,并輸出其信息。以下是查詢學生信息的代碼實現。
void queryStudent(Student* head, int id) {
Student* current = head->next;
while (current != NULL && current->id != id) {
current = current->next;
}
if (current == NULL) {
printf("未找到學號為%d的學生!\n", id);
return;
}
printf("學號: %d\n", current->id);
printf("姓名: %s\n", current->name);
printf("籍貫: %s\n", current->hometown);
}
顯示所有學生信息時,我們需要遍歷整個鏈表,并輸出每個節點的信息。以下是顯示所有學生信息的代碼實現。
void displayAllStudents(Student* head) {
Student* current = head->next;
if (current == NULL) {
printf("系統中沒有學生信息!\n");
return;
}
while (current != NULL) {
printf("學號: %d\n", current->id);
printf("姓名: %s\n", current->name);
printf("籍貫: %s\n", current->hometown);
printf("----------------------------\n");
current = current->next;
}
}
在程序結束時,我們需要釋放鏈表占用的內存,以避免內存泄漏。以下是銷毀鏈表的代碼實現。
void destroyList(Student* head) {
Student* current = head;
while (current != NULL) {
Student* temp = current;
current = current->next;
free(temp);
}
printf("鏈表已銷毀!\n");
}
以下是學生籍貫管理系統的完整代碼示例。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 50
#define MAX_HOMETOWN_LEN 100
typedef struct Student {
int id; // 學號
char name[MAX_NAME_LEN]; // 姓名
char hometown[MAX_HOMETOWN_LEN]; // 籍貫
struct Student* next; // 指向下一個節點的指針
} Student;
Student* createList() {
Student* head = (Student*)malloc(sizeof(Student));
if (head == NULL) {
printf("內存分配失??!\n");
exit(1);
}
head->next = NULL;
return head;
}
void addStudent(Student* head, int id, char* name, char* hometown) {
Student* newStudent = (Student*)malloc(sizeof(Student));
if (newStudent == NULL) {
printf("內存分配失??!\n");
exit(1);
}
newStudent->id = id;
strncpy(newStudent->name, name, MAX_NAME_LEN);
strncpy(newStudent->hometown, hometown, MAX_HOMETOWN_LEN);
newStudent->next = NULL;
Student* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newStudent;
printf("學生信息添加成功!\n");
}
void deleteStudent(Student* head, int id) {
Student* current = head;
while (current->next != NULL && current->next->id != id) {
current = current->next;
}
if (current->next == NULL) {
printf("未找到學號為%d的學生!\n", id);
return;
}
Student* temp = current->next;
current->next = temp->next;
free(temp);
printf("學生信息刪除成功!\n");
}
void modifyStudent(Student* head, int id, char* name, char* hometown) {
Student* current = head->next;
while (current != NULL && current->id != id) {
current = current->next;
}
if (current == NULL) {
printf("未找到學號為%d的學生!\n", id);
return;
}
strncpy(current->name, name, MAX_NAME_LEN);
strncpy(current->hometown, hometown, MAX_HOMETOWN_LEN);
printf("學生信息修改成功!\n");
}
void queryStudent(Student* head, int id) {
Student* current = head->next;
while (current != NULL && current->id != id) {
current = current->next;
}
if (current == NULL) {
printf("未找到學號為%d的學生!\n", id);
return;
}
printf("學號: %d\n", current->id);
printf("姓名: %s\n", current->name);
printf("籍貫: %s\n", current->hometown);
}
void displayAllStudents(Student* head) {
Student* current = head->next;
if (current == NULL) {
printf("系統中沒有學生信息!\n");
return;
}
while (current != NULL) {
printf("學號: %d\n", current->id);
printf("姓名: %s\n", current->name);
printf("籍貫: %s\n", current->hometown);
printf("----------------------------\n");
current = current->next;
}
}
void destroyList(Student* head) {
Student* current = head;
while (current != NULL) {
Student* temp = current;
current = current->next;
free(temp);
}
printf("鏈表已銷毀!\n");
}
int main() {
Student* head = createList();
int choice;
int id;
char name[MAX_NAME_LEN];
char hometown[MAX_HOMETOWN_LEN];
while (1) {
printf("\n學生籍貫管理系統\n");
printf("1. 添加學生信息\n");
printf("2. 刪除學生信息\n");
printf("3. 修改學生信息\n");
printf("4. 查詢學生信息\n");
printf("5. 顯示所有學生信息\n");
printf("6. 退出系統\n");
printf("請選擇操作: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("請輸入學號: ");
scanf("%d", &id);
printf("請輸入姓名: ");
scanf("%s", name);
printf("請輸入籍貫: ");
scanf("%s", hometown);
addStudent(head, id, name, hometown);
break;
case 2:
printf("請輸入要刪除的學生學號: ");
scanf("%d", &id);
deleteStudent(head, id);
break;
case 3:
printf("請輸入要修改的學生學號: ");
scanf("%d", &id);
printf("請輸入新的姓名: ");
scanf("%s", name);
printf("請輸入新的籍貫: ");
scanf("%s", hometown);
modifyStudent(head, id, name, hometown);
break;
case 4:
printf("請輸入要查詢的學生學號: ");
scanf("%d", &id);
queryStudent(head, id);
break;
case 5:
displayAllStudents(head);
break;
case 6:
destroyList(head);
return 0;
default:
printf("無效的選擇,請重新輸入!\n");
}
}
return 0;
}
通過本文的介紹,我們了解了如何使用C語言中的鏈表來實現一個簡單的學生籍貫管理系統。鏈表作為一種動態數據結構,非常適合用于需要頻繁插入和刪除操作的場景。通過這個系統,我們可以方便地管理學生的籍貫信息,并實現添加、刪除、修改、查詢和顯示等功能。希望本文能夠幫助讀者更好地理解鏈表的使用方法,并在實際編程中靈活運用。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。