m0_67890191 2022-03-23 16:00 采纳率: 66.7%
浏览 103
已结题

建立某链表的有关问题

‎建立一个链表,链表节点用于存储如下结构体:


‎ struct Student


‎ { long num; //学号


‎ char name[20]; //姓名


‎ char sex; //性别


‎ float score; //成绩


‎ };


‎要求:使用函数和指针进行合理的编程,


‎ 建立链表


struct SLink


{ struct Student s;


struct SLink *next;


} *np;


‎ ,并设计如下函数:


‎ 1)createLink()生成一个空链表;


‎ 2)insertData(struct Student s)按学生学号由小到大的顺序,将s插入到链表合适的位置;


‎ 提示:使用np=(struct SLink *) malloc(sizeof(struct SLink)开辟内存以建立新节点。


‎ 3)deleteData(long num)删除学号为num的节点。


‎ 提示:使用free(np)释放被删除节点的内存;


‎ 4)printLink()顺序打印输出链表各节点的内容。

  • 写回答

3条回答 默认 最新

  • stone_wangzx 2022-03-24 17:39
    关注
    
    #include<iostream>
    using namespace std;
    
    struct Student
    {
        long num; //学号
        char name[20]; //姓名
        char sex; //性别
        float score; //成绩
    };
    
    struct SLink
    {
        struct Student s;
        struct SLink* next;
    } *np;
    
    void createLink()
    {
        np = nullptr;
    }
    
    void insertData(struct Student s) 
    {
        if (np == nullptr){
            np = (struct SLink*)malloc(sizeof(struct SLink));
            np->s = s;
            np->next = nullptr;
            return;
        }
        SLink* pPrevNode = nullptr;
        SLink* pNode = np;
        while (pNode) {
            if (s.num <= pNode->s.num) {
                if (pPrevNode == nullptr) {
                    np = (struct SLink*)malloc(sizeof(struct SLink));
                    np->s = s;
                    np->next = pNode;
                }
                else {
                    pPrevNode->next = (struct SLink*)malloc(sizeof(struct SLink));
                    pPrevNode->next->s = s;
                    pPrevNode->next->next = pNode;
                }
                break;
            }
            if (pNode->next == nullptr){
                pNode->next = (struct SLink*)malloc(sizeof(struct SLink));
                pNode->next->s = s;
                pNode->next->next = nullptr;
                break;
            }
            pPrevNode = pNode;
            pNode = pNode->next;
        }
    }
    
    void deleteData(long num)
    {
        SLink* pPrevNode = nullptr;
        SLink* pNode = np;
        while (pNode){
            if (num == pNode->s.num){
                if (pPrevNode == nullptr){
                    np = pNode->next;
                }
                else{
                    pPrevNode->next = pNode->next;
                }
                //SLink* pNext = pNode->next;
                free(pNode);
                break;
               // pNode = pNext;
            }
            else{
                pPrevNode = pNode;
                pNode = pNode->next;
            }
        }
    }
    
    void printStudent(const Student& stud)
    {
        cout << stud.num << "\t" << stud.name << "\t" << (stud.sex ? "male" : "female") << "\t" << stud.score << endl;
    }
    
    void printLink() 
    {
        cout << "num\t" << "name\t" << "sex\t" << "score" << endl;
        SLink* pNode = np;
        while (pNode) {
            printStudent(pNode->s);
            pNode = pNode->next;
        }
    }
    
    int main(void)
    {
        createLink();
    
        struct Student a {0, "a", true,  100};
        struct Student b {10, "b", true,  90};
        struct Student c {2, "c", true,  80};
        struct Student d {3, "d", true,  100};
        struct Student e {43, "e", true,  99};
        struct Student f {5, "f", true,  60};
    
        insertData(a);
        insertData(b);
        insertData(c);
        insertData(d);
        insertData(e);
        insertData(f);
    
        printLink();
        deleteData(10);
        printLink();
    
        int i;
        cin >> i;
    
        return 0;
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月13日
  • 已采纳回答 4月5日
  • 创建了问题 3月23日

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败