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;
} 展开
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;
} 展开
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;
}
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;
}
展开全部
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;//同上
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;//同上
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询