1个回答
展开全部
蛇前进,就是根据蛇运动的方向,在前面添加一个head:addToHead();
,再把尾巴去掉deleteFromTail();
public void addToTail() {
Node node = null;
switch(tail.dir) {
case L :
node = new Node(tail.row, tail.col + 1, tail.dir);
break;
case U :
node = new Node(tail.row + 1, tail.col, tail.dir);
break;
case R :
node = new Node(tail.row, tail.col - 1, tail.dir);
break;
case D :
node = new Node(tail.row - 1, tail.col, tail.dir);
break;
}
tail.next = node;
node.prev = tail;
tail = node;
size ++;
}
public void addToHead() {
Node node = null;
switch(head.dir) {
case L :
node = new Node(head.row, head.col - 1, head.dir);
break;
case U :
node = new Node(head.row - 1, head.col, head.dir);
break;
case R :
node = new Node(head.row, head.col + 1, head.dir);
break;
case D :
node = new Node(head.row + 1, head.col, head.dir);
break;
}
node.next = head;
head.prev = node;
head = node;
size ++;
}
,再把尾巴去掉deleteFromTail();
public void addToTail() {
Node node = null;
switch(tail.dir) {
case L :
node = new Node(tail.row, tail.col + 1, tail.dir);
break;
case U :
node = new Node(tail.row + 1, tail.col, tail.dir);
break;
case R :
node = new Node(tail.row, tail.col - 1, tail.dir);
break;
case D :
node = new Node(tail.row - 1, tail.col, tail.dir);
break;
}
tail.next = node;
node.prev = tail;
tail = node;
size ++;
}
public void addToHead() {
Node node = null;
switch(head.dir) {
case L :
node = new Node(head.row, head.col - 1, head.dir);
break;
case U :
node = new Node(head.row - 1, head.col, head.dir);
break;
case R :
node = new Node(head.row, head.col + 1, head.dir);
break;
case D :
node = new Node(head.row + 1, head.col, head.dir);
break;
}
node.next = head;
head.prev = node;
head = node;
size ++;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询