链表指针求助。。。。
  • 板块学术版
  • 楼主DaiwaIssho
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/3/22 19:24
  • 上次更新2023/11/5 01:44:01
查看原帖
链表指针求助。。。。
446288
DaiwaIssho楼主2021/3/22 19:24
#include <iostream>
using namespace std;

struct person
{
    int ID;
    person* next;
};

class link
{
    public:

    person* head;
    person* now;

    link()
    {
        head=new person;
        head->next=head;
        head->ID=-1;
    }

    ~link()
    {
        person* p=head;
        person* q=head->next;
        person* headtemp=head;
        while (q!=headtemp)
        {
            delete p;
            p=q;
            q=q->next;
        }
        delete p;
    }

    void create(int n)
    {
        person* q,*p;
        p=head;
        for (int i=0;i<n;i++)
        {
            q=new person;
            cout<<q<<" -> ";
            q->ID=i+1;
            p->next=q;
            q->next=head;
            p=q;
        }
        cout<<endl;
    }

    person* out(person* &a)
    {
        person* p,*q;
        q=head;
        p=head->next;
        while (p!=a)
        {
            q=q->next;
            p=p->next;
        }
        q->next=p->next;
        cout<<p->ID<<" ";
        cout<<a<<"出函数前p"<<endl;
        cout<<a->next<<"出函数前p->next"<<endl;
        delete p;
        return a;
    }

    bool isnone()
    {
        return (head->next==head);
    }
};

int main()
{
    int n,m;
    cin>>n>>m;
    link line;
    line.create(n);
    person* p=line.head;
    person* q;
    while (!line.isnone())
    {
        for (int i=1;i<=m;i++)
        {
            p=p->next;
            cout<<p<<endl;
            if (p->ID==-1)
            {
                p=p->next;
            }
        }
        q=p;
        cout<<q<<"进函数前p"<<endl;
        cout<<q->next<<" 进函数前p->next "<<endl;
        line.out(p);
        p=q;
        cout<<p<<"出函数后p"<<endl;
        cout<<p->next<<" 出函数后p->next "<<endl;
    }
    return 0;
}

//1h+...

输入10 3,为什么p->next 会在函数前后变化。

2021/3/22 19:24
加载中...