java怎么写链表( 四 )


return head==null;
}
//从链表头部添加结点————此处的函数的传递参数是一个数值,也就是info
public void addToHead(IntNode some){
some.next=head;
head=some;
if(tail==null)
tail=head;
}
//从链表的尾部添加结点————同上
public void addToTail(IntNode some){
if(!isEmpty())
{
tail.next=some;
tail=tail.next;
}
else
head=tail=some;
}
//从链表头开始删除
public void deleteFromHead(){
if(head==tail)
head=tail=null;
else{
head=head.next;
}
}
}
5.如何使用Java实现单链表首先构建节点类: package com 。
fzw 。sf; public class Node { private Object data; private Node next; Node(Object data,Node next){ this 。
data = http://www.xuexi88.com/zhishi/data; this 。next = next; } public Object getData() { return data; } public void setData(Object data) { this 。
data = http://www.xuexi88.com/zhishi/data; } public Node getNext() { return next; } public void setNext(Node next) { this 。next = next; } } 其次,构建单链表类: package com 。
fzw 。sf; public class List4j { private Node head ; private int length; public List4j(){ head = new Node(null,null); length = 0; } public void addhead(Object item){ Node node = new Node(item,null); node 。
setNext(head 。getNext()); head 。
setNext(node); length++; } public void addtail(Object item){ Node node = new Node(item,null); Node temp = head; while(null != temp 。getNext()){ temp = temp 。
getNext(); } temp 。setNext(node); length++; } public void addindex(Object item,int index){ Node node = new Node(item,null) 。
6.帮用JAVA编个程序 运用到链表public class Player {
private Player previous;
private Player next;
private int number;
private static int instanceCount;
public Player() {
number = ++instanceCount;
}
public int getNumber() {
return number;
}
private void detach() {
if (this.previous == this.next) {
this.previous.next = null;
this.next.previous = null;
return;
}
if (this.previous != null)
this.previous.next = this.next;
if (this.next != null)
this.next.previous = this.previous;
}
public Player nextPlayer() {
Player player = new Player();
player.previous = this;
this.next = player;
return player;
}
public int post(int position) {
if (position == 7) {
position = 0;
detach();
}
if (next != null) {
return next.post(position + 1);
} else {
return number;
}
}
public static void main(String[] args) {
Player first = new Player();
Player cur = first;
for (int i = 0; i
7.用JAVA编写链表类,要求编写能够从头部添加节点public class ZLinkedList {
private int size;
private Node head;
public ZLinkedList(){
size = 0;
}
public void headInsert(Object obj){
//if(null== obj) {// do something}
Node temp = new Node(obj);
if(size ==0){
head = temp;
}else{
temp.setNext(head);
head = temp;
}
size++;
}
【java怎么写链表】public void preOrder(){
int length = size;
Node temp = head;
for(int i= 0;i