用学生成绩信息作为节点,完成单链表节点结构创建,并完成单链表基本操作:创建、显示和删除,链表排序和插入操作等。
希望采纳
#include <stdio.h>
#include <stdlib.h>
// 定义学生成绩信息结构体
typedef struct {
int id; // 学号
char name[20];// 姓名
float score; // 成绩
} ScoreInfo;
// 定义链表节点结构体
typedef struct ListNode {
ScoreInfo data; // 数据域
struct ListNode* next; // 指针域,指向下一个节点
} ListNode;
// 创建链表节点
ListNode* createNode(ScoreInfo data) {
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
if (newNode == NULL) {
printf("Error: Failed to allocate memory for new node.\n");
exit(-1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// 在链表尾部添加新节点
void appendNode(ListNode** pHead, ScoreInfo data) {
ListNode* newNode = createNode(data);
if (*pHead == NULL) { // 如果链表为空,将新节点作为头节点
*pHead = newNode;
}
else {
ListNode* p = *pHead;
while (p->next != NULL) { // 找到链表尾部
p = p->next;
}
p->next = newNode; // 将新节点接在链表尾部
}
}
// 显示链表中的所有节点
void printList(ListNode* pHead) {
printf("ID\tName\tScore\n");
while (pHead != NULL) {
ScoreInfo data = pHead->data;
printf("%d\t%s\t%.1f\n", data.id, data.name, data.score);
pHead = pHead->next;
}
}
// 删除链表中第一个匹配的节点
void deleteNode(ListNode** pHead, ScoreInfo data) {
if (*pHead == NULL) {
printf("Error: The list is empty.\n");
return;
}
ListNode* p = *pHead;
if (p->data.id == data.id) { // 如果要删除的是头节点
*pHead = p->next;
free(p);
return;
}
while (p->next != NULL && p->next->data.id != data.id) { // 找到要删除的节点
p = p->next;
}
if (p->next == NULL) {
printf("Error: No matching node found.\n");
return;
}
ListNode* toDelete = p->next;
p->next = toDelete->next;
free(toDelete);
}
然后插入操作和链表排序
#include <stdio.h>
#include <stdlib.h>
// 定义学生成绩信息结构体
struct student {
int id; // 学号
char name[20]; // 姓名
float score; // 分数
};
// 定义单链表节点结构体
struct node {
struct student stu; // 学生信息
struct node* next; // 下一个节点指针
};
// 定义链表排序函数
void sort_list(struct node* head) {
struct node *p, *q;
struct student temp;
for (p = head; p != NULL; p = p->next) {
for (q = p->next; q != NULL; q = q->next) {
if (p->stu.score < q->stu.score) {
temp = p->stu;
p->stu = q->stu;
q->stu = temp;
}
}
}
}
// 定义链表插入函数
void insert_node(struct node** head, struct student stu) {
struct node* p = (struct node*)malloc(sizeof(struct node));
p->stu = stu;
p->next = NULL;
if (*head == NULL) {
*head = p;
} else {
struct node* q = *head;
while (q->next != NULL) {
q = q->next;
}
q->next = p;
}
}
int main() {
struct node* head = NULL;
int n, i;
printf("请输入学生人数:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
struct student stu;
printf("请输入第%d个学生的学号、姓名和分数:", i);
scanf("%d %s %f", &stu.id, stu.name, &stu.score);
insert_node(&head, stu);
}
sort_list(head);
printf("学号\t姓名\t分数\n");
struct node* p;
for (p = head; p != NULL; p = p->next) {
printf("%d\t%s\t%.2f\n", p->stu.id, p->stu.name, p->stu.score);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// 创建链表节点结构体
struct node {
char name[20]; // 学生姓名
int number; // 学号
int math; // 数学成绩
int english; // 英语成绩
int total; // 总分
struct node *next; // 指向下一个节点的指针
};
// 创建链表
struct node *create_list() {
struct node *head, *new_node, *prev;
head = prev = NULL;
do {
new_node = (struct node *)malloc(sizeof(struct node)); // 为节点分配内存
if (new_node == NULL) {
printf("Allocation failed!");
}
printf("Please enter the student's name, number, math score, and English score (separated by spaces):\n");
scanf("%s %d %d %d", new_node->name, &(new_node->number), &(new_node->math), &(new_node->english));
new_node->total = new_node->math + new_node->english; // 计算总分
new_node->next = NULL; // 新节点的next指针初始值为NULL
if (head == NULL) {
head = new_node; // 如果head为空,则将head指向新节点
} else {
prev->next = new_node; // 否则将上一个节点的next指针指向新节点
}
prev = new_node; // 更新上一个节点为当前节点
printf("Do you want to continue? (y/n)\n");
} while (getchar() != 'n'); // 根据用户的选择来决定是否继续创建节点
return head; // 返回链表头结点的指针
}
// 打印链表节点信息
void print_node(struct node *p) {
printf("%d %s (Math: %d, English: %d, Total: %d)\n", p->number, p->name, p->math, p->english, p->total);
}
// 显示链表
void display_list(struct node *head) {
struct node *p;
p = head;
printf("The student grade list is:\n");
while (p != NULL) { // 遍历全部节点并输出
print_node(p);
p = p->next;
}
}
// 删除链表
void delete_list(struct node *head) {
struct node *p, *q;
p = head;
while (p != NULL) {
q = p->next;
free(p); // 释放节点内存
p = q;
}
}
// 链表排序
void sort_list(struct node *head) {
int swp;
char swn[20];
struct node *p, *q;
for (p = head; p != NULL; p = p->next) {
for (q = p->next; q != NULL; q = q->next) {
if (p->total < q->total) { // 按照总分进行排序
swp = p->total;
p->total = q->total;
q->total = swp;
swp = p->math;
p->math = q->math;
q->math = swp;
swp = p->english;
p->english = q->english;
q->english = swp;
swp = p->number;
p->number = q->number;
q->number = swp;
strncpy(swn, p->name, sizeof(swn)-1); // 执行字符串拷贝并且确保拷贝后的字符串以'\0'终止
strncpy(p->name, q->name, sizeof(p->name)-1);
strncpy(q->name, swn, sizeof(q->name)-1);
}
}
}
}
参考代码
#include<stdio.h>
void main(){
int a[5]={2,1,5,4,3};
int i,j,k,temp;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
}