java 什么是单向链表 和 双向链表 ? 20
这个代码哪里有体现出链表?importjava.awt.Color;importjava.awt.Graphics;importjava.awt.event.KeyEve...
这个代码哪里有体现出链表?
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
public class Snake {
Node head = null;
Node tail = null;
int size = 0;
Node nodes = new Node(25,25, Dir.L);
public Snake() {
head = nodes;
tail = nodes;
size = 1;
}
public void AddToHead() {
Node nodes = null;
switch(head.dir) {
case L :
nodes = new Node(head.row, head.col - 1, head.dir );
break;
case U :
nodes = new Node(head.row - 1, head.col, head.dir );
break;
case R :
nodes = new Node(head.row, head.col + 1, head.dir );
break;
case D :
nodes = new Node(head.row + 1, head.col, head.dir );
}
nodes.next = head;
head.prev = nodes; // ???
head = nodes;
size ++;
}
public void drwa(Graphics g) {
if(size <=0) return;
for(Node n=head; n!=null; n = n.next) {
n.draw(g);
}
move();
}
public void move() {
AddToHead();
DleToTail();
}
public void DleToTail() { //???
if(size ==0) return;
tail = tail.prev;
tail.next = null;
}
public class Node {
int w = Yard.BOX_SIZE;
int h = Yard.BOX_SIZE;
Node next = null;
int row, col;
Dir dir;
Node prev; //???
public Node(int row, int col, Dir dir) {
this.row = row;
this.col = col;
this.dir = dir;
}
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
//g.fillRect( row * Yard.BOX_SIZE, col * Yard.BOX_SIZE, w, h);///????
g.fillRect( col * Yard.BOX_SIZE, row * Yard.BOX_SIZE, w, h);
g.setColor(c);
}
}
public void KeyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_LEFT :
head.dir = Dir.L;
break;
case KeyEvent.VK_UP :
head.dir = Dir.U;
break;
case KeyEvent.VK_RIGHT :
head.dir = Dir.R;
break;
case KeyEvent.VK_DOWN :
head.dir = Dir.D;
break;
}
}
} 展开
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
public class Snake {
Node head = null;
Node tail = null;
int size = 0;
Node nodes = new Node(25,25, Dir.L);
public Snake() {
head = nodes;
tail = nodes;
size = 1;
}
public void AddToHead() {
Node nodes = null;
switch(head.dir) {
case L :
nodes = new Node(head.row, head.col - 1, head.dir );
break;
case U :
nodes = new Node(head.row - 1, head.col, head.dir );
break;
case R :
nodes = new Node(head.row, head.col + 1, head.dir );
break;
case D :
nodes = new Node(head.row + 1, head.col, head.dir );
}
nodes.next = head;
head.prev = nodes; // ???
head = nodes;
size ++;
}
public void drwa(Graphics g) {
if(size <=0) return;
for(Node n=head; n!=null; n = n.next) {
n.draw(g);
}
move();
}
public void move() {
AddToHead();
DleToTail();
}
public void DleToTail() { //???
if(size ==0) return;
tail = tail.prev;
tail.next = null;
}
public class Node {
int w = Yard.BOX_SIZE;
int h = Yard.BOX_SIZE;
Node next = null;
int row, col;
Dir dir;
Node prev; //???
public Node(int row, int col, Dir dir) {
this.row = row;
this.col = col;
this.dir = dir;
}
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
//g.fillRect( row * Yard.BOX_SIZE, col * Yard.BOX_SIZE, w, h);///????
g.fillRect( col * Yard.BOX_SIZE, row * Yard.BOX_SIZE, w, h);
g.setColor(c);
}
}
public void KeyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_LEFT :
head.dir = Dir.L;
break;
case KeyEvent.VK_UP :
head.dir = Dir.U;
break;
case KeyEvent.VK_RIGHT :
head.dir = Dir.R;
break;
case KeyEvent.VK_DOWN :
head.dir = Dir.D;
break;
}
}
} 展开
展开全部
public Snake() {
head = nodes;
tail = nodes;
size = 1;
}
public void AddToHead() {
Node nodes = null;
switch(head.dir) {
case L :
nodes = new Node(head.row, head.col - 1, head.dir );
break;
case U :
nodes = new Node(head.row - 1, head.col, head.dir );
break;
case R :
nodes = new Node(head.row, head.col + 1, head.dir );
break;
case D :
nodes = new Node(head.row + 1, head.col, head.dir );
}
nodes.next = head;
head.prev = nodes; // ???
head = nodes;
size ++;
}
public void drwa(Graphics g) {
if(size <=0) return;
for(Node n=head; n!=null; n = n.next) {
n.draw(g);
}
move();
}
public void move() {
AddToHead();
DleToTail();
}
public void DleToTail() { //???
if(size ==0) return;
tail = tail.prev;
tail.next = null;
}
public class Node {
int w = Yard.BOX_SIZE;
int h = Yard.BOX_SIZE;
Node next = null;
int row, col;
Dir dir;
Node prev; //???
public Node(int row, int col, Dir dir) {
this.row = row;
this.col = col;
this.dir = dir;
}
你这些的操作中都涉及到了链表。链表的操作是通过指针的移动实现的。
head = nodes;
tail = nodes;
size = 1;
}
public void AddToHead() {
Node nodes = null;
switch(head.dir) {
case L :
nodes = new Node(head.row, head.col - 1, head.dir );
break;
case U :
nodes = new Node(head.row - 1, head.col, head.dir );
break;
case R :
nodes = new Node(head.row, head.col + 1, head.dir );
break;
case D :
nodes = new Node(head.row + 1, head.col, head.dir );
}
nodes.next = head;
head.prev = nodes; // ???
head = nodes;
size ++;
}
public void drwa(Graphics g) {
if(size <=0) return;
for(Node n=head; n!=null; n = n.next) {
n.draw(g);
}
move();
}
public void move() {
AddToHead();
DleToTail();
}
public void DleToTail() { //???
if(size ==0) return;
tail = tail.prev;
tail.next = null;
}
public class Node {
int w = Yard.BOX_SIZE;
int h = Yard.BOX_SIZE;
Node next = null;
int row, col;
Dir dir;
Node prev; //???
public Node(int row, int col, Dir dir) {
this.row = row;
this.col = col;
this.dir = dir;
}
你这些的操作中都涉及到了链表。链表的操作是通过指针的移动实现的。
追问
不是很明白,能说的详细些吗?
追答
单向链表:只有个头指针; 双向链表:有个头指针和尾指针。
两个链表在进行添删改查操作时,指针的移动是不相同的,但是原理都一样,看下关于数据结构的书就明白了。我也好久没接触了,有点忘记了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
http://zhidao.baidu.com/question/122288261.html
其实我也不是很懂。。。我帮你找了一下 你看看吧
其实我也不是很懂。。。我帮你找了一下 你看看吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
那种东西在java里体现的并不明显,你只需要知道其原理就可以了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |