下面展示一些 内联代码片
。
部分源码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//企鹅联系-----------------3765140556
// 学生数据类型定义
typedef struct Student {
int id;
char name[50];
char course[50];
} Student;
// 顺序存储
Student students[100];
int studentCount = 0;
// 链式存储
typedef struct Node {
Student data;
struct Node *next;
struct Node* left;
struct Node* right;
} Node;
Node *head = NULL;
// 增加数据
void addStudent(int id, char name[], char course[]) {
Student newStudent;
newStudent.id = id;
strcpy(newStudent.name, name); // 使用strcpy将name字符串复制到newStudent.name中
strcpy(newStudent.course, course); // 使用strcpy将course字符串复制到newStudent.course中
students[studentCount++] = newStudent;
// 链式存储
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = newStudent;
newNode->next = head;
head = newNode;
}
// 显示数据
void displayStudents() {
printf("顺序存储:\n");
for (int i = 0; i < studentCount; i++) {
printf("ID: %d, Name: %s, Course: %s\n", students[i].id, students[i].name, students[i].course);
}
printf("\n链式存储:\n");
Node *current = head;
while (current != NULL) {
printf("ID: %d, Name: %s, Course: %s\n", current->data.id, current->data.name, current->data.course);
current = current->next;
}
}
// 删除数据
void deleteStudent(int id) {
for (int i = 0; i < studentCount; i++) {
if (students[i].id == id) {
for (int j = i; j < studentCount - 1; j++) {
students[j] = students[j + 1];
}
studentCount--;
break;
}
}
// 链式存储
Node *current = head;
Node *prev = NULL;
while (current != NULL) {
if (current->data.id == id) {
if (prev == NULL) {
head = current->next;
} else {
prev->next = current->next;
}
free(current);
break;
}
prev = current;
current = current->next;
}
}
// 修改数据
void modifyStudent(int id, char name[], char course[]) {
for (int i = 0; i < studentCount; i++) {
if (students[i].id == id) {
strcpy(students[i].name, name);
strcpy(students[i].course, course);
break;
}
}
// 链式存储
Node *current = head;
while (current != NULL) {
if (current->data.id == id) {
strcpy(current->data.name, name);
strcpy(current->data.course, course);
break;
}
current = current->next;
}
}
// 快速排序
int partition(Student arr[], int low, int high) {
Student pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j].id < pivot.id) {
i++;
Student temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
Student temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
void quickSort(Student arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
// 冒泡排序算法
void customSort(Student arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j].id > arr[j + 1].id) {
Student temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// 二叉排序树查找
Node* insertBST(Node* root, Student data) {
if (root == NULL) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
if (data.id < root->data.id) {
root->left = insertBST(root->left, data);
} else if (data.id > root->data.id) {
root->right = insertBST(root->right, data);
}