for语句条件成立也没有执行
CreateList(L,N);
temp = L;
while(temp->next != temp && temp)
{
for(i=M-1;i>0;i--)
{
if(temp->next == NULL)
temp->next = L->next;
else
temp = temp->next;
}
cout<next->nData<
temp->next = temp->next->next;
}
主要的代码如上,可是for语句根本没有执行,想不通
------解决思路----------------------
你调试过了吗,确定没有执行,最好把完整的代码贴出来
------解决思路----------------------
单步调试还看不出问题?
------解决思路----------------------
在for语句内外都打印循环变量不就清楚了吗?
------解决思路----------------------
M的值是多少
------解决思路----------------------
这样可好:
#include
#include
using namespace std;
typedef struct SNode
{
int nData;
struct SNode *next;
}SNode, *LinkList;
void CreateList(LinkList &L, int n)
{
//int i = n;
int i = 2;
L = (SNode*)malloc(sizeof(SNode));
L->nData = 1;
L->next = NULL;
LinkList p = L;
while (i <= n)
{
p->next = (LinkList)malloc(sizeof(SNode));
p = p->next; //zx
p->nData = i;
//p->next = L->next;
//L->next = p;
i++;
}
p->next = L; //构成环
}
int main()
{
int M, N;
int i, j = 0;
LinkList L, temp;
cout <
cin >> N >> M;
cout <
CreateList(L, N);
temp = L;
//while (temp->next != temp && temp)
//{
//for (i = M - 1; i>0; i--)
//{
//if (temp->next == NULL)
//temp->next = L->next;
//else
//temp = temp->next;
//}
//cout <next->nData <
//temp->next = temp->next->next;
//}
i = 1;
while (i
{
j = 0;
while (1)
{
if (0 != temp->nData)
{
if (++j == M) break;
}
temp = temp->next;
}
cout <nData;
temp->nData = 0;
i++;
}
while (0 == temp->nData) temp = temp->next;
cout <nData <
return 0;
}