Showball. 2022-03-15 23:07 采纳率: 100%
浏览 52
已结题

C++ 链表无法删除头节点

做题时发现单链表删除函数调用后无法删除头结点,其他节点可正常删除。

#include<iostream>
using namespace std;
struct Student {
    int sno;
    char sname[20];
    int ssocre;
    Student* next;
};
int n;
Student* creat() {//创建链表
    Student* head, * p1, * p2;
    head = NULL;
    p1 = p2 = new Student;
    n = 0;
    cin >> p1->sno >> p1->sname >> p1->ssocre;
    while (p1->sno != -1) {
        n = n + 1;
        if (n == 1)head = p1;
        else p2->next = p1;
        p2 = p1;
        p1 = new Student;
        cin >> p1->sno >> p1->sname >> p1->ssocre;
    }
    p2->next = NULL;
    return head;
}
void Stuprint(Student* head) {//输出链表
    Student* p;
    p = head;
    while (p != NULL) {
        cout << p->sno << " " << p->sname << " " << p->ssocre << endl;
        p = p->next;
    }
}
Student* Sinsert(Student* head) {//插入函数(由于不排序,将插入链表放在最后一个结点)
    Student* p0 = NULL, * p1, * p2, * stu;
    p1 = head;
    stu = new Student;
    p0 = stu;
    while (p1->next != NULL) {
        p2 = p1;
        p1 = p1->next;
    }
    p1->next = p0; p0->next = NULL;
    cout << "请输入要插入学生的信息" << endl;
    cin >> p0->sno >> p0->sname >> p0->ssocre;

    n = n + 1;
    return head;

}
struct Student* del(struct Student* head, int num)
{
    struct Student* p1, * p2 = NULL;
    if (head == NULL)
    {
        cout << "list null\n"; return NULL;
    }
    p1 = head;
    while (num != p1->sno && p1->next != NULL)
    {
        p2 = p1; p1 = p1->next;
    }
    if (num == p1->sno)
    {
        if (num == head->sno)head = p1->next;
        else p2->next = p1->next;
        n--;
    }
    else cout << "无此人\n";
    return head;
}

int main() {
    Student* head;
    cout << "请输入需要录入的学生信息(学号,姓名,成绩,全部输入-1时停止录入)" << endl;
    head = creat();
    cout << "***************************************************" << endl;
    Stuprint(head);
    int ans = -1, sno1 = 0;
    while (ans) {
        cout << "***************************************************" << endl;
        cout << "请输入您需要使用的功能(1.删除 2.插入 0.退出程序)" << endl;

        cin >> ans;
        switch (ans) {
        case 1:cout << "请输入需要删除学生的学号" << endl; cin >> sno1; del(head, sno1);
            cout << "***************************************************" << endl;
            Stuprint(head); break;
        case 2:Sinsert(head);   cout << "***************************************************" << endl;
            Stuprint(head); break;
        }
    }
    return 0;

}

运行结果及报错内容 可正常运行
我的解答思路和尝试过的方法

和模板进行了对比,也问了学长和同学

我想要达到的结果

删除函数可以删除所选节点

  • 写回答

2条回答 默认 最新

  • 关注

    del函数中删除头结点时修改了del函数中head指针指向的地址 , 你要接收del函数的返回值重新赋值给主函数的head
    del(head, sno1) 前面加上 head =

            switch (ans) {
            case 1:cout << "请输入需要删除学生的学号" << endl; cin >> sno1;
                head = del(head, sno1); //前面加上 head =
                cout << "***************************************************" << endl;
                Stuprint(head); break;
            case 2:Sinsert(head);   cout << "***************************************************" << endl;
                Stuprint(head); break;
            }
     
    

    img

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
  • 有问必答小助手 2022-03-16 10:14
    关注
    您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
    PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月24日
  • 已采纳回答 3月16日
  • 创建了问题 3月15日

悬赏问题

  • ¥15 问题描述:给定一个算术表达式,通过程序求出最后的结果
  • ¥15 spyder运行重复
  • ¥15 我用C语言easyx图形库绘制了一个3d游戏方框透视,但进入游戏时候鼠标准星对准方框边缘 鼠标光标就会弹出来这是啥情况怎样让光标对准绘制的方框点击鼠标不弹出光标好烦这样
  • ¥20 用Power Query整合的问题
  • ¥20 基于python进行多背包问题的多值编码
  • ¥15 相同型号电脑与配置,发现主板有一台貌似缺少了好多元器件似的,会影响稳定性和使用寿命吗?
  • ¥15 C语言:数据子序列基础版
  • ¥20 powerbulider 导入excel文件,显示不完整
  • ¥15 paddle训练自己的数据loss降不下去
  • ¥20 用matlab的pdetool解决以下三个问题