class Link{
class Node{
private Object data;
private Node next;
Node(Object data){
this.data = data;
}
//第一次调用 this = root data (第二个节点添加完成)
//第二次调用 this =root->next data (第三个节点添加完成)
//第三次调用 this =root->next->next data (第四个节点添加完成)
public void addNode(Node newNode){
if(this.next == null){
this.next = newNode;
} else {
this.next.addNode(newNode);
}
}
//第一次调用 this = root data
//第二次调用 this = root->next data
//第二次调用 this = root->next->next data
public boolean containsNode(Object data){
if(this.data.equals(data)){
return true;
} else {
if(this.next == null)
return false;
else
return this.next.containsNode(data);
}
}
//第一次调用 this = root foot = 0
//第二次调用 this = root->next foot = 1
//第三次调用 this = root->next->next foot = 2
public Object getNode(int index){
if(Link.this.foot++ == index){
return this.data;
}
else{
return this.next.getNode(index);
}
}
//第一次调用 this = root foot = 0
//第二次调用 this = root->next foot = 1
//第三次调用 this = root->next->next foot = 2
public void setNode(int index, Object data){
if(Link.this.foot ++ == index){
this.data = data;
}
else{
this.next.setNode(index, data);
}
}
//第一次调用this = root->next pre = root
//第二次调用this = root->next->next pre = root->next
//第三次调用this = root->next->next->next pre = root->next->next
public void removeNode(Node pre, Object data){
if(this.data.equals(data)){
pre.next = this.next;
} else {
this.next.removeNode(this, data);
}
}
public void toArrayNode(){
Link.this.array[Link.this.foot++] = this.data;
if(this.next == null){
return ;
}else {
this.next.toArrayNode();
}
}
}
//以上为内部类
private Node root;
private int count = 0;//用于记录节点个数
private int foot = 0;
private Object[] array;
//添加节点
public void add(Object data){
if (data == null) return ;//此链表内容不能为空
Node newNode = new Node(data);
if(this.root == null){
this.root = newNode;//第一个结点添加完成
}
else{
this.root.addNode(newNode);
}
this.count++;
}
//返回链表中结点的个数
public int size(){
return this.count;
}
//判断链表是否为空
public boolean isEmpty(){
return this.count == 0;
}
//判断某一个数据是否在链表中存在
public boolean contains(Object data){
if(this.root == null||data == null)
return false;
else
return this.root.containsNode(data);
}
//根据索引取得对应值
public Object get(int index){
if(index > this.count)
return null;
this.foot = 0;
return this.root.getNode(index);
}
//使用新内容替换索引位置
public void set(int index, Object data){
if(index > this.count){
return ;
}
this.foot = 0;
this.root.setNode(index, data);
}
//删除指定数据
public void remove(Object data){
if(contains(data)){
if(this.root.data.equals(data)){
this.root = this.root.next;
} else {
this.root.next.removeNode(this.root, data);
}
}
this.count--;
}
public Object[] toArray(){
if(this.root == null)
return null;
this.foot = 0;
this.array = new Object[this.count];
this.root.toArrayNode();
return this.array;
}
}
public class LinkDemo2 {
public static void main(String[] args) {
Link all = new Link();
all.add(1);
all.add(2);
all.add(3);
System.out.println(all.size());
Object data[] = all.toArray();
for(int i = 0; i < data.length; i++){
int str = Integer.parseInt(data[i].toString());
System.out.println(str);
}
}
}
class Node{
private Object data;
private Node next;
Node(Object data){
this.data = data;
}
public Object getDate(){
return this.data;
}
public Node getNext(){
return this.next;
}
public void addNode(Node newnode){
if(this.next == null){
this.next = newnode;
}
else
this.next.addNode(newnode);
}
public void printNode(){
System.out.println((String)this.data);
if(this.next!=null)
this.next.printNode();
else
return ;
}
public boolean searchNode(Object data){
if(this.data.equals(data))
return true;
if(this.next == null)
return false;
else
return this.next.searchNode(data);
}
public boolean deleteNode(Node node,Object data){
if(this.data.equals(data)){
node.next = this.next;
return true;
}else{
return this.next.deleteNode(this, data);
}
}
}
class Link{
private Node root;
public void add(Object data){
Node newnode = new Node(data);
if(root == null){
root = newnode;
}
else {
root.addNode(newnode);
}
}
public void print(){
if(this.root!=null){
this.root.printNode();
}
}
public boolean search(Object data){
if(this.root==null)
return false;
else
return this.root.searchNode(data);
}
public boolean delete(Object data){
if(this.root==null)
return false;
if(!this.search(data))
return false;
else{
if(this.root.getDate().equals(data)){
this.root = this.root.getNext();
return true;
}
else
return this.root.getNext().deleteNode(this.root, data);
}
}
}
public class waibuclass {
public static void main(String[] args) {
Link link = new Link();
link.add("1");
link.add("2");
link.add("3");
link.print();
link.delete("1");
link.print();
}
}