| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1474 人关注过本帖
标题:删除链表第一个结点后,为什么会出现乱码和第二结点和后面的东西
只看楼主 加入收藏
吴彬东
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2018-12-3
收藏
 问题点数:0 回复次数:0 
删除链表第一个结点后,为什么会出现乱码和第二结点和后面的东西
#include <iostream>
#include<cstring>
#include <fstream>
using namespace std;
struct Student
{
    char sex[10]; char ID[25];
    int age = 0;
    char name[50];
    double scoreE;
    double scoreM;
    double scoreC;
    Student *next;
}; Student *p, *q, *head;

void input(Student  *&head)
{
    char ch;
    cout << "输入数据(y/n):";
    cin >> ch;
    if (ch == 'y'||ch=='Y')
    {
        p = new Student;//动态分配空间
        cout << "input ID:" << endl;
        cin >> p->ID; ///将学号录入p指针指向的结构体里的ID变量  

        cout << " input sex(m/w):" << endl;
        cin >> p->sex;//将性别录入p指针指向的结构体里的sex变量

        cout << " input age:" << endl;
        cin >> p->age;//将年龄录入p指针指向的结构体里的age变量

        cout << "输入学生的名字:" << endl;
        cin >> p->name;//将姓名录入p指针指向的结构体里的name变量

        cout << " 输入学生的英语、数学、程序设计的成绩:" << endl;
        cin >> p->scoreE >> p->scoreM >> p->scoreC;
    }
    else goto L0;                        //若输入n,则返回菜单栏
    while (ch == 'y' || ch == 'Y')            //输入下一个同学的信息
    {
        if (head == NULL)     head = p;
        else
            q->next = p;
        q = p; cout << "输入数据(y/n):" << endl;
        cin >> ch;
        if (ch == 'y' || ch == 'Y')
        {
            p = new Student;
            cout << " input ID:" << endl; cin >> p->ID;                //将学号录入p指针指向的结构体里的ID变量
            cout << " input sex:" << endl; cin >> p->sex;            //将性别录入p指针指向的结构体里的sex变量
            cout << "input age:" << endl; cin >> p->age;            //将年龄录入p指针指向的结构体里的age变量
            cout << "输入学生的名字:" << endl; cin >> p->name;        //将姓名录入p指针指向的结构体里的name变量
            cout << " 输入学生的英语、数学、程序设计的成绩:" << endl;
            cin >> p->scoreE >> p->scoreM >> p->scoreC;                //将各科成绩录入p指针指向的结构体里的scoreE、scoreM、scoreC变量
        }

    }
    q->next = NULL;
L0:return;
}

void show(Student * head)
{
    if (!head) { cout << "\t空链表!" << endl; goto L1; }
    cout << "\t链表中的数据是: \n";
    while (head)
    {
        cout << "student's ID:\t" << head->ID << endl;
        cout << "student's sex:\t" << head->sex << endl;
        cout << "student's age:\t" << head->age << endl;
        cout << "student's name':" <<  head->name <<endl;
        cout << " 输入学生的英语、数学、程序设计的成绩:" << endl;
        cout << head->scoreE << '\t' << head->scoreM << '\t' << head->scoreC<<endl;
        head = head->next;            //head指针往下一个结构体指
    }
    cout << endl;
L1:return;
}

//**************************************************************删除函数,问题所在,求救
void deleting(Student *head)
{
    char sname[20];
    cout << "input the deleted student's name" << endl; cin >> sname;/*输入要删除的学生的姓名*/
    Student *p;
    if (!head)
    {
        cout << "List null!\n"; return;
    }
    if (strcmp(head->name, sname) == 0)/*如果链表头的学生信息即为所求
                                     则将p指向链表头,head指针指向链表第二个结构体,使其成为新的链表头
                                     然后释放原来链表头的空*/
    {
        p = head; head = head->next; delete p; p = NULL;
        cout << sname << "'s'information have been deleted.\n"; return;
    }
    for (Student *pg = head; pg->next; pg = pg->next)
    {
        if (strcmp(pg->next->name, sname) == 0)                        //找到了对应的姓名
        {
            /*将p指向对应姓名的结构体,同时将该结构体的上一个结构体的next指针指向该结构体的下一个结构体
              将该结构体的空间释放,则该学生的信息已被删除 */
            p = pg->next; pg->next = p->next; delete p; p = NULL;
            cout << sname << "'s information have been deleted.\n"; return;
        }
    }
    cout << sname << " She/He is not in this class:" << endl; return;
}


void search(Student*head) {
    char sname[20];
    cout << "input the deleted student's name" << endl; cin >> sname;//输入想要查询的学生的姓名
    Student *p;
    if (!head) {
        cout << "List nulll!\n"; return;
    }
    if (strcmp(head->name, sname) == 0)//若链表头的学生信息即为所求,则直接显示出该学生的信息即可
    {
        cout << "student's ID:\t" << head->ID << endl;
        cout << "student's sex:\t" << head->sex << endl;
        cout << "student's age:\t" << head->age << endl;
        cout << "student's name':" <<  head->name<<endl;
        cout << " 输入学生的英语、数学、程序设计的成绩:" << endl;
        cout << head->scoreE << '\t' << head->scoreM << '\t' << head->scoreC<<endl;
        return;
    }
    for (Student *pg = head; pg->next; pg = pg->next)
    {
        if (strcmp(pg->next->name, sname) == 0)    //若pg指向的结构体的下一个结构体的信息即为所求,直接显示即可,
        {
            cout << "student's ID:\t" << head->ID << endl;
            cout << "student's sex:\t" << head->sex << endl;
            cout << "student's age:\t" << head->age << endl;
            cout << "student's name':" <<  head->name<<endl;
            cout << " 输入学生的英语、数学、程序设计的成绩:" << endl;
            cout << head->scoreE << '\t' << head->scoreM << '\t' << head->scoreC<<endl;
            return;
        }
    }
    cout << sname << " :She/He is not in this class:" << endl; return;
}

void reserve(Student*head)//保存在文本文件 ,文本文件是顺序存取文件,以默认的方式打开
{
    char fileName[30];
    if (!head) { cout << "\t空链表!" << endl; return; }
    ofstream outstuf;                   //建立输出文件流对象
    cout << "Please input the name of the students'file:" << endl; cin >> fileName;
    //输入文件名格式,例如: d:\student.txt
    outstuf.open(fileName, ios::out);//连接文件,指定打开方式为写方式
    if (!outstuf) {//调用重载算符函数测试流,判断文件是否能打开
        cerr << "File couldn't be open!" << endl; abort(); return;
    }
    outstuf << "\t链表中的数据是: \n";//写入一个标题
    while (head)
    {       //把链表的信息按顺序写入流中
        outstuf << "student's ID:\t" << head->ID << endl;
        outstuf << "student's sex:\t" << head->sex << endl;
        outstuf << "student's age:\t" << head->age << endl;
        outstuf << "student's name':" <<  head->name<<endl;
        outstuf << " 输入学生的英语、数学、程序设计的成绩:" << endl;
        outstuf << head->scoreE << '\t' << head->scoreM << '\t' << head->scoreC<<endl;
        head = head->next;            //head指针往下一个结构体指 针
    }
    outstuf << endl;
    outstuf.close();//关闭文件
    return;
}


int main()
{
    Student *head = NULL; int key;
    int choice, bh;
L:
    cout << "\n\n\n\n\n" << endl;
    cout << "\t\t|\t==========student==========\t|\n" << endl;
    cout << "\t\t|\t 1. insert information     \t|\n" << endl;
    cout << "\t\t|\t 2. demonstrate information\t|\n" << endl;
    cout << "\t\t|\t 3. delete information     \t|\n" << endl;
    cout << "\t\t|\t 4. search information     \t|\n" << endl;
    cout << "\t\t|\t 5. conserve information   \t|\n" << endl;
    cout << "\t\t|\t 0. exit                   \t|\n" << endl;
    cout << "\t\t|\t===========================\t|\n\n" << endl;
    cout << "\t\t select(0~5):";
    cin >> choice;
    switch (choice)
    {
    case 1: input(head); goto L;
    case 2: show(head);  goto L;
    case 3:
        deleting(head);
        goto L;
    case 4:search(head); goto L;
    case 5:reserve(head); goto L;
    case 0: cout << " \t退出程序的运行!\n" << endl; break;
    default: cout << "\t输入错误,请重新输入!\n" << endl; goto L;
    }
}

搜索更多相关主题的帖子: name Student head cout 结构体 
2019-07-05 11:28
快速回复:删除链表第一个结点后,为什么会出现乱码和第二结点和后面的东西
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017004 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved