问题描述:在初始化中没有问题,但是初始化完成后敲回车即程序定时器触发后直接跳掉了,招呼都不打一声耶
注:代码部分是规规矩矩的。可以跳掉额。1、在头文件escapeDlg.h中的public:
int num_now; //当前障碍物数量
int num_sum; //障碍物总数量
struct BALL //定义障碍物的结构体相关属性,也可以用class
{
//省略部分代码
struct BALL *next; //指针指向下一个障碍物的结构体
}*ball, *firstball, *temp;2、在escapeDlg.cpp中的OnInitDialog()写了
num_sum = 10;
///////////////////////////////////////////这是动态分配内存,可不看。
for (int i=0; i<num_sum; i++)
{
temp = (BALL*)malloc(sizeof(BALL)); //分配空间,目前ball在链表尾端并且是空地址无正常空间
if (i == 0)
{
firstball = temp; //链表的链首指向ball的内存地址
ball = temp;
}
temp->x = rand()%800 +2;
temp->y = 11;
temp->cx = rand()%(num_sum - 4) - 3;
temp->cy = rand()%(num_sum - 4) - 3;
ball->next = temp;
ball = ball->next;
}
ball = NULL;
ball = firstball;
for (i=0; i<num_sum; i++) //这是测试有没有分配好,结果正确。
{ CString a;
a.Format("%d", ball->cy);
AfxMessageBox(a);
ball = ball->next;
}3、问题来了,注意啊
ball = firstball; //恢复ball指针位置
num_now = 0; //初始num_now
AfxMessageBox("1");
//////////////////////////////问题就是下面的while,谁能告诉我为什么?
while(ball != NULL) //这个while是用来读取ball的数量并存储到num_now
{
num_now++;
ball = ball->next;
}
下面附上程序源代码,是未加链表和加了链表的。http://d.1tpan.com/tp0556210166
帮个忙啊,纠结到要挂了。
注:代码部分是规规矩矩的。可以跳掉额。1、在头文件escapeDlg.h中的public:
int num_now; //当前障碍物数量
int num_sum; //障碍物总数量
struct BALL //定义障碍物的结构体相关属性,也可以用class
{
//省略部分代码
struct BALL *next; //指针指向下一个障碍物的结构体
}*ball, *firstball, *temp;2、在escapeDlg.cpp中的OnInitDialog()写了
num_sum = 10;
///////////////////////////////////////////这是动态分配内存,可不看。
for (int i=0; i<num_sum; i++)
{
temp = (BALL*)malloc(sizeof(BALL)); //分配空间,目前ball在链表尾端并且是空地址无正常空间
if (i == 0)
{
firstball = temp; //链表的链首指向ball的内存地址
ball = temp;
}
temp->x = rand()%800 +2;
temp->y = 11;
temp->cx = rand()%(num_sum - 4) - 3;
temp->cy = rand()%(num_sum - 4) - 3;
ball->next = temp;
ball = ball->next;
}
ball = NULL;
ball = firstball;
for (i=0; i<num_sum; i++) //这是测试有没有分配好,结果正确。
{ CString a;
a.Format("%d", ball->cy);
AfxMessageBox(a);
ball = ball->next;
}3、问题来了,注意啊
ball = firstball; //恢复ball指针位置
num_now = 0; //初始num_now
AfxMessageBox("1");
//////////////////////////////问题就是下面的while,谁能告诉我为什么?
while(ball != NULL) //这个while是用来读取ball的数量并存储到num_now
{
num_now++;
ball = ball->next;
}
下面附上程序源代码,是未加链表和加了链表的。http://d.1tpan.com/tp0556210166
帮个忙啊,纠结到要挂了。
{
temp = (BALL*)malloc(sizeof(BALL)); //分配空间,目前ball在链表尾端并且是空地址无正常空间
memset(temp,0,sizeof(BALL));temp->x = rand()%800 +2;
temp->y = 11;
temp->cx = rand()%(num_sum - 4) - 3;
temp->cy = rand()%(num_sum - 4) - 3;
if (i == 0)
{
firstball = temp; //链表的链首指向ball的内存地址
ball = temp;
}
else
{
ball->next = temp;
ball = ball->next;
}
}
ball = firstball;这两句有问题,没有提供链表的终点。for (i=0; i<num_sum; i++) //这是测试有没有分配好,结果正确。这里loop次数有限制,正确。但while循环就会有问题。可能应该是
ball->next = NULL;//表示链表终点。
ball = firstball;//可能会破坏链表的,如果是测试,应该另外提供一个指针。
ball = ball->next;
-----------
你的ball一直都指向你插入的元素,你采用的是尾部插入的方式
re:VisualEleven:我想做个单链表,就是最普通的链表,所以用顺序的方法插入。
帮个忙啊,纠结到要挂了。最主要的错误是分配
temp = (BALL*)malloc(sizeof(BALL));结构内存后
没有temp->next = NULL;赋值,所以
while(ball != NULL) //条件测试就会出错
解决方法及就是
....
temp->next = NULL;//增加这句
还有一个隐含问题
if (i == 0)
{
firstball = temp; //链表的链首指向ball的内存地址
ball = temp;
}
else
{ //移到这里,如果不移动到这里,当num_sum =1 时就是循环链表了
ball->next = temp;
ball = ball->next;
}