有人能帮我把这个程序最后的删除函数解释一下吗?
#include<iostream>using namespace std;
struct Node
{
int data;
Node *next;
};
Node *CreateList(Node *L,int &n);
void Joseph(Node *L,int n,int m);
Node *DeleteList(Node *&L,int i,Node *q);
int main()
{
Node *L;
L=NULL;
int n,m;
cout<<"请输入人数N:";
cin>>n;
if(n<1) {cout<<"请输入正整数!";}
else
{
cout<<"请输入密码M:";
cin>>m;
if(m<1) {cout<<"请输入正整数!";}
else
{
L=CreateList(L,n);
Joseph(L,n,m);
}
}
system("pause");
return 0;
}
Node *CreateList(Node *L,int &n)
{
Node *q;
for(int i=1;i<=n;i++)
{
Node *p;
p=new Node;
p->data=i;
p->next=NULL;
if(i==1)
L=q=p;
else
{
q->next=p;
q=q->next;
}
}
q->next=L;
if(L!=NULL)
return(L);
else
cout<<"尾指针异常!"<<endl;
}
void Joseph(Node *L,int n,int m)
{
cout<<"\n出列顺序:\n";
for(int i=1;i<n;i++)
{
Node *q=new Node;
q=DeleteList(L,m,q);
cout<<"号数:"<<q->data<<endl;
delete(q);
}
cout<<"号数:"<<L->data<<endl;
delete L;
}
Node *DeleteList(Node *&L,int i,Node *q)
{
Node *p,*m;
p=L;
if(i==1)
{
m=p->next;
while (m->next!=L)
m=m->next;
m->next=p->next;
q=L;
L=m->next;
return q;
}
else
{
int j=1;
while(j<i-1)
{
p=p->next;
j++;
}
q=p->next;
p->next=p->next->next;
L=p->next;
return(q);
}
}