C++链表输出有问题,有两个数字乱码,请求大神说明原因

#include<iostream>usingnamespacestd;typedefstructSnake1{intxlefts;intytops;intxrights... #include <iostream>
using namespace std;

typedef struct Snake1
{
int xlefts;
int ytops;
int xrights;
int ybottoms;
static int node;
int direction;
int life;
Snake1 *next;
}Snake,*ListSnake;

int Snake::node=0;

ListSnake head;

void ShowNode(ListSnake head)
{
ListSnake p;
p=head;
cout<<p->node<<endl;
while (p!=NULL)
{
cout<<p->xlefts<<","<<p->ytops;
cout<<","<<p->xrights<<","<<p->ybottoms<<endl;
p=p->next;
}

}

ListSnake CreateSnake(ListSnake head)
{
ListSnake p,q;
head=new Snake;
head->xlefts=320;
head->ytops=60;
head->xrights=head->xlefts+10;
head->ybottoms=head->ytops+10;
++(head->node);
p=new Snake;
cout<<head->xlefts<<endl;
p->xlefts=head->xlefts+10;
cout<<p->xlefts<<endl;
p->ytops=head->ytops+10;
p->xrights=p->xrights+10;
p->ybottoms=p->ytops+10;
head->next=p;
q=p;
++(head->node);
p=new Snake;
p->xlefts=q->xlefts+10;
p->ytops=q->ytops+10;
p->xrights=p->xrights+10;
p->ybottoms=p->ytops+10;
q->next=p;
p->next=NULL;
++(head->node);
return head;
}

int main()
{
head=CreateSnake(head);
ShowNode(head);
return 0;
}
展开
 我来答
香草and水果
2010-11-18 · 超过25用户采纳过TA的回答
知道答主
回答量:50
采纳率:100%
帮助的人:35.2万
展开全部
#include <iostream>
using namespace std;

typedef struct Snake1
{
int xlefts;
int ytops;
int xrights;
int ybottoms;
static int node;
int direction;
int life;
Snake1 *next;
}Snake,*ListSnake;

int Snake::node=0;

ListSnake head;

void ShowNode(ListSnake head)
{
ListSnake p;
p=head;
cout<<p->node<<endl;
while (p!=NULL)
{
cout<<p->xlefts<<","<<p->ytops;
cout<<","<<p->xrights<<","<<p->ybottoms<<endl;
p=p->next;
}
}

ListSnake CreateSnake(ListSnake head)
{
ListSnake p,q;

head=new Snake;
head->xlefts=320;
head->ytops=60;
head->xrights=head->xlefts+10;
head->ybottoms=head->ytops+10;

++(head->node);

p=new Snake;
cout<<head->xlefts<<endl;
p->xlefts=head->xlefts+10;
cout<<p->xlefts<<endl;
p->ytops=head->ytops+10;
p->xrights=p->xlefts+10; //这里写错了,你把p->xlefts写成p->xrights了
p->ybottoms=p->ytops+10;
head->next=p;

q=p;
++(head->node);

p=new Snake;
p->xlefts=q->xlefts+10;
p->ytops=q->ytops+10;
p->xrights=p->xlefts+10; //这也一样
p->ybottoms=p->ytops+10;
q->next=p;
p->next=NULL;

++(head->node);

return head;
}

int main()
{
head=CreateSnake(head);
ShowNode(head);
return 0;
}
dragoo1
2010-11-18 · TA获得超过519个赞
知道小有建树答主
回答量:404
采纳率:0%
帮助的人:120万
展开全部
p=new Snake;
cout<<head->xlefts<<endl;
p->xlefts=head->xlefts+10;
//p->xrights=0;先给个初值
cout<<p->xlefts<<endl;
p->ytops=head->ytops+10;
p->xrights=p->xrights+10;//这里p->xrights未设置初值,编译器会给个随机值。。。

p=new Snake;
p->xlefts=q->xlefts+10;
p->ytops=q->ytops+10;
p->xrights=p->xrights+10;//同上
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式