作业
完成单链表操作,要求节点构造类型。
1、建立学生结构体(学号,姓名,成绩)
2、循环调用头插法创建整表
3、遍历单链表
4、任意位置插入一个完整的学生信息
5、任意位置删除一个学生。
6、单链表逆置
7、单链表按照学生成绩排序。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student {
int id; // 学号
char name[50]; // 姓名
float score; // 成绩
struct Student* next; // 指向下一个节点
} Student;
// 创建新节点
Student* createStudent(int id, const char* name, float score) {
Student* newStudent = (Student*)malloc(sizeof(Student));
newStudent->id = id;
strcpy(newStudent->name, name);
newStudent->score = score;
newStudent->next = NULL;
return newStudent;
}
// 头插法创建链表
Student* createList() {
Student* head = NULL;
int n, id;
char name[50];
float score;
printf("请输入学生数量: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("输入学号: ");
scanf("%d", &id);
printf("输入姓名: ");
scanf("%s", name);
printf("输入成绩: ");
scanf("%f", &score);
// 头插法
Student* newStudent = createStudent(id, name, score);
newStudent->next = head;
head = newStudent;
}
return head;
}
// 遍历单链表
void traverseList(Student* head) {
Student* current = head;
while (current != NULL) {
printf("学号: %d, 姓名: %s, 成绩: %.2f\n", current->id, current->name, current->score);
current = current->next;
}
}
// 在任意位置插入节点
void insertAtPosition(Student** head, int position, int id, const char* name, float score) {
if (position < 0) return;
Student* newStudent = createStudent(id, name, score);
if (position == 0) {
newStudent->next = *head;
*head = newStudent;
return;
}
Student* current = *head;
for (int i = 0; current != NULL && i < position - 1; i++) {
current = current->next;
}
if (current == NULL) {
printf("位置超出链表范围\n");
free(newStudent);
return;
}
newStudent->next = current->next;
current->next = newStudent;
}
// 在任意位置删除节点
void deleteAtPosition(Student** head, int position) {
if (*head == NULL || position < 0) return;
Student* temp = *head;
if (position == 0) {
*head = temp->next;
free(temp);
return;
}
for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) {
printf("位置超出链表范围\n");
return;
}
Student* next = temp->next->next;
free(temp->next);
temp->next = next;
}
// 逆置单链表
void reverseList(Student** head) {
Student* prev = NULL;
Student* current = *head;
Student* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
// 按照成绩排序
void sortList(Student** head) {
if (*head == NULL) return;
int swapped;
Student *ptr1;
Student *lptr = NULL;
do {
swapped = 0;
ptr1 = *head;
while (ptr1->next != lptr) {
if (ptr1->score > ptr1->next->score) {
int tempId = ptr1->id;
char tempName[50];
float tempScore = ptr1->score;
ptr1->id = ptr1->next->id;
strcpy(ptr1->name, ptr1->next->name);
ptr1->score = ptr1->next->score;
ptr1->next->id = tempId;
strcpy(ptr1->next->name, tempName);
ptr1->next->score = tempScore;
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
} while (swapped);
}
// 主函数,测试所有功能
int main() {
Student* head = NULL;
// 创建链表
head = createList();
printf("\n遍历列表:\n");
traverseList(head);
// 在指定位置插入
insertAtPosition(&head, 2, 1001, "张三", 85.0);
printf("\n插入后列表:\n");
traverseList(head);
// 删除指定位置
deleteAtPosition(&head, 1);
printf("\n删除后列表:\n");
traverseList(head);
// 逆置链表
reverseList(&head);
printf("\n逆置后列表:\n");
traverseList(head);
// 排序链表
sortList(&head);
printf("\n排序后列表:\n");
traverseList(head);
// 释放内存
while (head != NULL) {
Student* temp = head;
head = head->next;
free(temp);
}
return 0;
}