当前位置: 首页 > news >正文

c语言-链表习题

1.尾插法 Q6544

涉及:
(1)创建链表

struct stu* createList() {
    struct stu *head = NULL, *tail = NULL, *newNode;
    char choice;
    char name[20];
    float price;
    do {
        printf("请输入书名 价格:\n");
        scanf("%s %f", name, &price);
        newNode = (struct stu*)malloc(sizeof(struct stu));
        if (newNode == NULL) {
            printf("内存分配失败\n");
            return NULL;
        }
        strcpy(newNode->name, name);
        newNode->price = price;
        newNode->next = NULL;
        if (head == NULL) {
            head = newNode;
            tail = newNode;
        } else {
            tail->next = newNode;
            tail = newNode;
        }
        printf("是否继续输入,按Y键继续输入,其他键就结束.\n");
        scanf(" %c", &choice);
    } while (choice == 'Y' || choice == 'y');
    return head;
}

往链表里一个个添加节点同理,需要注意如果是在主函数中定义的LinkList,且创建链表函数不返回结点指针的话(为void),需要传头结点的地址(尽量不用,用createList返回指针是更容易实现的。),如test4: Q6502

int main()
{
    int choice;
    struct stu *head=NULL,*tail=NULL;
    do{
        printf("1  增加数据\n");
        printf("2  退出\n");
        printf("选择:");
        scanf("%d",&choice);
        if(choice==1){
            Append(&head,&tail);
        }
    }while(choice!=2);
    DisLink(head);
    DeleteMemory(head);
    return 0;
}
void Append(struct stu **head,struct stu **tail){
    struct stu* newNode;
    newNode=(struct stu*)malloc(sizeof(struct stu));
    if(newNode==NULL){
        printf("内存分配失败\n");
        return;
    }
    printf("请输入学号:");
    scanf("%s",newNode->ID);
    printf("请输入名字:");
    scanf("%s",newNode->name);
    printf("请依次输入语文,数学,外语成绩:");
    scanf("%d %d %d",&newNode->c1,&newNode->c2,&newNode->c3);
    newNode->next=NULL;
    if((*head)==NULL){
        (*head)=newNode;
        (*tail)=newNode;
    }
    else{
        (*tail)->next=newNode;
        (*tail)=newNode;
    }
}

(2)查找最大值

void printMostExpensive(struct stu *head) {
    if (head == NULL) {
        return;
    }
    struct stu *max = head;
    struct stu *current = head->next;
    while (current!= NULL) {
        if (current->price > max->price) {
            max = current;
        }
        current = current->next;
    }
    printf("result:\n");
    printf("%s %.2f\n", max->name, max->price);
}

(3)释放内存

void freeList(struct stu *head) {
    struct stu *temp;
    while (head!= NULL) {
        temp = head;
        head = head->next;
        free(temp);
    }
}

在这里插入图片描述
全部代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 定义结构体
struct stu {
    char name[20];
    float price;
    struct stu *next;
};

// 创建链表函数
struct stu* createList() {
    struct stu *head = NULL, *tail = NULL, *newNode;
    char choice;
    char name[20];
    float price;
    do {
        printf("请输入书名 价格:\n");
        scanf("%s %f", name, &price);
        newNode = (struct stu*)malloc(sizeof(struct stu));
        if (newNode == NULL) {
            printf("内存分配失败\n");
            return NULL;
        }
        strcpy(newNode->name, name);
        newNode->price = price;
        newNode->next = NULL;
        if (head == NULL) {
            head = newNode;
            tail = newNode;
        } else {
            tail->next = newNode;
            tail = newNode;
        }
        printf("是否继续输入,按Y键继续输入,其他键就结束.\n");
        scanf(" %c", &choice);
    } while (choice == 'Y' || choice == 'y');
    return head;
}

// 输出最贵书籍信息函数
void printMostExpensive(struct stu *head) {
    if (head == NULL) {
        return;
    }
    struct stu *max = head;
    struct stu *current = head->next;
    while (current!= NULL) {
        if (current->price > max->price) {
            max = current;
        }
        current = current->next;
    }
    printf("result:\n");
    printf("%s %.2f\n", max->name, max->price);
}

// 释放链表内存函数
void freeList(struct stu *head) {
    struct stu *temp;
    while (head!= NULL) {
        temp = head;
        head = head->next;
        free(temp);
    }
}

int main() {
    struct stu *head = createList();
    printMostExpensive(head);
    freeList(head);
    return 0;
}
/*
Algorithms 105
Y
高等数学 31.5
Y
C语言 35
J

*/

2.排序 test3

在这里插入图片描述

(1)利用选择排序的方法,降序排序

void descLinkList(Student* head){
    Student *i,*j;
    int tempID;
    char tempName[10];
    float tempScore;
    for(i=head;i!=NULL;i=i->pNextNode){
        for(j=i->pNextNode;j!=NULL;j=j->pNextNode){
            if(i->score<j->score){
                tempID=i->ID;
                i->ID=j->ID;
                j->ID=tempID;
                strcpy(tempName,i->name);
                strcpy(i->name,j->name);
                strcpy(j->name,tempName);
                tempScore=i->score;
                i->score=j->score;
                j->score=tempScore;
            }
        }
    }
}

3.

相关文章:

  • 【C】链表算法题7 -- 环形链表||
  • matlab汽车动力学半车垂向振动模型
  • Mac 部署Ollama + OpenWebUI完全指南
  • 第三十三周学习周报
  • 洛谷 P2894 USACO08FEB Hotel 题解
  • C语言----共用体
  • 1、云原生写在前面
  • 高并发系统-性能指标的判断
  • prompt技术结合大模型 生成测试用例
  • transformer(4):FFN 编码器块
  • Hutool - Cron:强大的定时任务模块
  • 装饰器模式
  • 双指针-三数之和
  • 【YOLOv11改进- 主干网络】YOLOv11+CSWinTransformer: 交叉窗口注意力Transformer助力YOLOv11有效涨点;
  • MongoDB:记一次数据迁移经验
  • JavaSE的基础语法(5)
  • PostgreSQL如何关闭自动commit
  • 基于Python的Flask微博话题舆情分析可视化系统
  • SaaS 平台开发要点
  • javascript-es6 (四)
  • 受天气等影响SC8041航班三次备降延误超12小时,山航致歉
  • 两部门调度部署“五一”假期安全防范工作,要求抓好旅游安全
  • “五一”逃离城市计划:带上帐篷去大自然里充电
  • 五一“大车流”来了,今日午后G40沪陕高速开始迎来出沪高峰
  • 临港迎来鸿蒙智行“尚界”整车及电池配套项目,首款车型今秋上市
  • 金科服务:大股东博裕资本提出无条件强制性现金要约收购,总代价约17.86亿港元