[数据结构]循环链表(c++/类模板)用例JosephRing

来源:互联网 发布:淘宝logo设计哪家好 编辑:程序博客网 时间:2024/06/05 18:21
/***约瑟夫环编号为1,2,3,……,n的n个人按顺时针方向围坐一圈。任选一个正整数作为报数上限m,从第一个人开始按顺时针方向自1开始顺序报数,报到m时停止报数。报m的人出列,从他在顺时针方向上的下一个人开始重新从1报数,如此下去,直至所有人全部出列为止。设计程序输出出列顺序。***//**输入人数n 报数上限m人员记录1 (格式为:姓名 学号 性别 年龄 班级 健康状况)人员记录2…人员记录n输出第1次报数出列的人员记录第2次报数出列的人员记录…第n次报数出列的人员记录**/#include <iostream>#include <cstdlib>#include <stdio.h>#include <string>using namespace std;struct StudentNode{    string Sname;    long Sno;    string Ssex;    int Sage;    string Sclass;    string Shealth;    StudentNode* next;    StudentNode(const string& StudentName, const long& StudentNo, const string& StudentSex,                const int& StudentAge, const string& StudentClass, const string& StudentHealth,                StudentNode* ptr=NULL)//用于初始化    {        Sname = StudentName;        Sno = StudentNo;        Ssex = StudentSex;        Sage = StudentAge;        Sclass = StudentClass;        Shealth = StudentHealth;        next = NULL;    }};class StudentList{private:    StudentNode* first;//头指针public:    StudentList()    {        first = NULL;    }    StudentList(const StudentNode& L)    {        first = NULL;        CopyStudentList(L);    }    StudentList operator=(const StudentList& L)    {        if(this == &L)            return *this;        MakeEmpty();        CopyStudentList(L);        return *this;    }    ~StudentList()    {        //头删法        /*StudentNode* p;        while(p)        {            p=first;            first = p->next;            //last = p->next;            delete p;        }*/        MakeEmpty();    }    void InputFront(const string& StuName, const long& StuNo, const string& StuSex,                    const int& StuAge, const string& StuClass, const string& StuHealth);    void InputRear(const string& StuName, const long& StuNo, const string& StuSex,                   const int& StuAge, const string& StuClass, const string& StuHealth);    bool Insert(const int i, const string& StuName, const long& StuNo, const string& StuSex,                const int& StuAge, const string& StuClass, const string& StuHealth);    bool Search(const string& StuName, const long& StuNo, const string& StuSex,                const int& StuAge, const string& StuClass, const string& StuHealth);    bool Remove(int i, string& StuName, long& StuNo, string& StuSex,                int& StuAge, string& StuClass, string& StuHealth);    void CopyStudentList(const StudentList& L);    void MakeEmpty();    StudentNode* LocateRear()    {        StudentNode* iter = first;        do        {            iter = iter->next;        }        while(iter->next != first);        return iter;    }    int Length() const    {        StudentNode* s=first;        int count=1;        while(s->next==first)        {            count++;            s = s->next;        }        return count;    }    bool IsEmpty() const    {        return first==NULL;    }    bool IsFull() const    {        return false;    }    /***        void Output(const int num, const int count)        {            StudentNode* pre = first;            StudentNode* p = NULL;            int n = 0;            while(n != num)            {                if(n == num-1)                {                    cout << pre->Sname << " " << pre->Sno << " "                         << pre->Ssex << " " << pre->Sage << " "                         << pre->Sclass << " " << pre->Shealth << " "<< endl;                }                else                {                    for(int i = 1; i<count; i++)                    {                        p = pre;                        pre = pre->next;                        //a = i;                    }                    cout << pre->Sname << " " << pre->Sno << " "                         << pre->Ssex << " " << pre->Sage << " "                         << pre->Sclass << " " << pre->Shealth << " "<< endl;                    p->next = pre->next;                    delete pre;                    pre = p->next;                    //string stuName, stuSex, stuClass, stuHealth;                    //long stuNo;                    //int stuAge;                    //Remove(a, stuName, stuNo, stuSex, stuAge, stuClass, stuHealth);                    n++;                }            }        }    **/    void Output(const int num, const int count)    {        StudentNode* pre = first;        StudentNode* p = NULL;        int n = 0;        for(int i = 0; i < num; i++)        {            if(n == num-1)            {                cout << pre->Sname << " " << pre->Sno << " "                     << pre->Ssex << " " << pre->Sage << " "                     << pre->Sclass << " " << pre->Shealth;            }            else            {                for(int j = 1; j<count; j++)                {                    p = pre;                    pre = pre->next;                    //a = i;                }                cout << pre->Sname << " " << pre->Sno << " "                     << pre->Ssex << " " << pre->Sage << " "                     << pre->Sclass << " " << pre->Shealth << endl;                p->next = pre->next;                delete pre;                pre = p->next;                n++;            }        }        pre = NULL;        p = NULL;        return;    }    friend ostream& operator<<(ostream& out, const StudentList& Stu)    {        StudentNode* iter = Stu.first;        while(iter)        {            out << iter->Sname << " " << iter->Sno << " "                << iter->Ssex << " " << iter->Sage << " "                << iter->Sclass << " " << iter->Shealth << endl;            iter = iter->next;        }        return out;    }    friend istream& operator>>(istream& in, StudentList& L)    {        return in;    }};void StudentList::CopyStudentList(const StudentList& L){    if(L.first==NULL)    {        MakeEmpty();    }    else    {        StudentNode* iter = first;        do        {            InputRear(iter->Sname, iter->Sno, iter->Ssex,                      iter->Sage, iter->Sclass, iter->Shealth);            iter = iter->next;        }        while(iter==first);    }    return;}void StudentList::MakeEmpty(){    /*StudentNode* del;    while(first)    {        //尾删法        StudentNode* item = first;        del = item->next;        while(del->next!=first)        {            item = item->next;            del = del->next;        }        item->next = first;        delete del;    }    return;*/}void StudentList::InputFront(const string& StuName, const long& StuNo, const string& StuSex,                             const int& StuAge, const string& StuClass, const string& StuHealth){    StudentNode* newNode = new StudentNode(StuName, StuNo, StuSex, StuAge, StuClass, StuHealth);    newNode->next = first;    first = newNode;    StudentNode* pre = LocateRear();    pre->next = first;}void StudentList::InputRear(const string& StuName, const long& StuNo, const string& StuSex,                            const int& StuAge, const string& StuClass, const string& StuHealth){    StudentNode* newNode = new StudentNode(StuName, StuNo, StuSex, StuAge, StuClass, StuHealth);    if(first == NULL)    {        first = newNode;    }    else    {        StudentNode* iter = first;        do        {            iter = iter->next;        }        while(iter->next != first);        iter->next = newNode;        newNode->next = first;    }}bool StudentList::Insert(int i,const string& StuName, const long& StuNo, const string& StuSex,                         const int& StuAge, const string& StuClass, const string& StuHealth){    StudentNode* newNode = new StudentNode(StuName, StuNo, StuSex, StuAge, StuClass, StuHealth);    if(i < 1)    {        cout << "Location error" << endl;        return false;    }    if(i == 1)    {        first = newNode;        newNode->next = first;        return true;    }    else    {        StudentNode* pre = first;        for(int j=1; j<i-1; j++)        {            pre = pre->next;        }        newNode->next = pre->next;        pre->next = newNode;        return true;    }}bool StudentList::Remove(int i, string& StuName, long& StuNo, string& StuSex,                         int& StuAge, string& StuClass, string& StuHealth){    StudentNode* del;    StudentNode* pre;    if(IsEmpty())    {        cout << "Empty" << endl;        return false;    }    if(i < 1)    {        cout << "Location error" << endl;        return false;    }    if(i == 1)    {        del = first;        first = del->next;        StuName = del->Sname;        StuNo = del->Sno;        StuSex = del->Ssex;        StuAge = del->Sage;        StuClass = del->Sclass;        StuHealth = del->Shealth;        StudentNode* iter = LocateRear();        iter->next = del->next;        delete del;        return true;    }    else    {        pre = first;        if(pre->next == NULL)        {            cout << "Location error" << endl;            return false;        }        pre = pre->next;        for(int j = 1; j < i-2; j++)        {            if(pre->next == first)            {                cout << "Location error" << endl;                return false;            }            pre = pre->next;        }        del = pre->next;        pre->next = del->next;        StuName = del->Sname;        StuNo = del->Sno;        StuSex = del->Ssex;        StuAge = del->Sage;        StuClass = del->Sclass;        StuHealth = del->Shealth;        delete del;        return true;    }}int main(){    StudentList Stu;    int n, m;//n为人数,m为报数上限    cin >> n >> m;    for(int i = 0; i < n; i++)    {        string Sname;        long Sno;        string Ssex;        int Sage;        string Sclass;        string Shealth;        cin >> Sname >> Sno >> Ssex >> Sage >> Sclass >> Shealth;        Stu.Insert(i+1, Sname, Sno, Ssex, Sage, Sclass, Shealth);    }    Stu.Output(n, m);    return 0;}

此程序析构函数部分被打上注释,该类编写不完整,具体错误问题还未发现。

去掉析构函数注释会出现无法结束程序的问题,输出结果正确。

具体错误原因待排查。

阅读全文
0 0
原创粉丝点击